Perl execute shell command line – Quick Tutorial

There are several ways to do this, please check the following code to see what is the different:


use strict;

print "*** backtick ***\n";
my $result = `ls`;
print "$result\n";

print "*** system ***\n";
my $result2 = system ("ls");
print "system result: $result2\n";

print "\n*** readpipe ***\n";
my $result3 = readpipe ("ls");
print "$result3\n";

print "*** exec ***\n";
exec ("ls");

In a nutshell:

  • exec: does not return anything, it simply executes the command
  • system: creates a fork process and waits to see if the command succeeds or fails – returning a value
  • backtick and readpipe: used to capture the output of a system call

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s