Native operating system command in Perl (`backticks`)

Home > Search
  by

Backticks can be used to run a command in perl that is native to the underlying operation system. For example, let's say a Perl script is running on a Linux server. The following script is an example of how backticks can be used to run native Linux commands in Perl. Running this Perl script will print Hello World to the console.

#!/usr/bin/perl
`echo "Hello World"`;

 

The prior example was not very practical, as you could print Hello World to the console in native Perl. However, there are some scenarios where a native operating system command is either just a heck of a lot easier, or there is no Perl command to run the native operating system command. As an example, let's say you want to view a certificate in a Java keystore. This certainly would be best done using a native operating system command.

my $foo = `keytool -list -v -keystore /path/to/example.jks -storepass secret`;
print $foo;

 

Sometimes, it makes sense to store the native operating system command you want to run as a Perl variable. 

my $file = "cat /path/to/example.txt";

 

Then, you can run the command using backticks and pipe native operating system commands.

my $bar = `$foo | grep Hello`;

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments