Vitor Py
Vitor Py

Reputation: 5190

Trapping Perl backticks on non-zero return codes

I'd like know if it is possible to trap (call a subroutine) every time a Perl backticks (quote) operator returns a non zero error code instead of checking $? >> 8 after each call.

Is it possible?

Upvotes: 2

Views: 319

Answers (2)

ikegami
ikegami

Reputation: 385917

IPC::System::Simple's capture is a version of backticks that throws an exception on error.

Upvotes: 4

simbabque
simbabque

Reputation: 54333

You could wrap the backticks in a function that implements the error checking and call that instead. That would be implementing a hooking system yourself.

sub call_backticks {
  my ($command, $callback) = @_;

  my $output = `$command`;
  return $callback->($command, $?) if $? >> 8;
  return $output;
}

# later...

my $output = call_backticks('cat /var/log/messages', sub { print Dumper @_; });

That would give you basic handling on this. But there is no general hooking up to stuff in Perl.

Upvotes: 2

Related Questions