Backticks is a Perl module that turns backticks into full objects which you can query in interesting ways.
my $results = `ls -a /`; # Assign a Backticks object to $results
print $results->stdout; # Get the command's STDOUT
print $results->stderr; # Get the command's STDERR
print $results->success; # Will be true when the command exited clean
print $results; # Get the command's STDOUT... the object
# stringifies to the command's output so you
# can use it most places you use normal
You can have failed commands automatically die your perl script
$Backticks::autodie = 1;
`perl -e 'print STDERR "OUCH!\n"; exit 1'`;
Which dies with the following message:
Error executing `perl -e 'print STDERR "OUCH!\n"; exit 1'`:
Failed with non-zero exit code 1
You can automatically chomp output:
$Backticks::chomped = 1;
my $chomped = `perl -e 'print "Hello!\n"'`;
You can even access parameters instantly in object mode by calling methods immediately after the backticks!
say `echo foo`->stdout; # Shows 'foo'
say `perl -e 'print STDERR "Hello world!"'`->stderr; # Shows 'Hello world'
say `perl -e 'exit 1'`->exitcode; # Shows '1'
You can also use the classical perl object-oriented interface instead of using the backticks to create objects, the following command is the same as the first one above:
my $results = Backticks->run("ls -la /");
Alternately, you can create a command and run it later:
my $command = Backticks->new("ls -la /");
# ... do some stuff
Creating commands as an object affords you the opportunity to override Backticks package settings, by passing them as hash-style params:
$Backticks::chomped = 0;
my $chomped_out = Backticks->run(
'echo "Hello there!"',
'chomped' => 1,