Perl FAQ

Jan 01, 2019 18:39

Most common ways to execute external commands from Perl are:
my $files = `ls -la` - captures the output of the command in $files
system "touch ~/foo" - if you don't want to capture the command's output
exec "vim ~/foo" - if you don't want to return to the script after executing the command
open(my $file, '|-', "grep foo"); print $file "foo\nbar" - if you want to pipe input into the command

Other way:
backticks or qx//
my $output = `script.sh --option`;
my $output = qx/script.sh --option/;

The backtick operator and it's equivalent qx// excute the command and options inside the operator and return that commands output to STDOUT when it finishes.

There are also ways to run external applications through creative use of open, but this is advanced use; read the documentation for more.

perl, draft

Previous post Next post
Up