How to execute Unix/shell commands in a Perl script?

in Bioinformatics Programming by

BioPerl is a collection of Perl modules which is used to write Perl scripts applied in bioinformatics [1]. It is used in bioinformatics programmings such as in developing source codes, standalone software/tools, and algorithms. It’s easy to install and provide various modules which make it easier to execute different functions. However, Python is mostly preferred over the Perl language, still, some of the bioinformatics software is based on Perl such as the standalone version of I-TASSER. Sometimes, it’s big trouble to execute some Unix/shell commands in Perl script for the beginners, it’s difficult to decide which function would be specific to a condition. Therefore, this article is a guide for the execution of Unix/shell commands in Perl script.

Perl offers different functions and operators to execute external commands (described as follows), which are special in their own ways. They are slightly different from each other, which sometimes makes hard to choose for the beginners in Perl.

1. exec””

 syntax: exec "command";

It is a Perl function (perlfunc) which executes a command but never returns, as same as the return statement in a function. It keeps continuing processing the script while executing the command and doesn’t wait for it to finish first, it returns false when the command is not found but never returns true.

2. system()

syntax: system( "command" );

It is also a Perl function (perlfunc) which executes a command and waits for it to get finished first and then resume the Perl script. The return value is the exit status of the command. It can be called in two ways:

system( "command arg1 arg2" );

OR

system( "command", "arg1", "arg2" );

3. Backticks “ or qx//

syntax: `command`;

syntax: qx/command/;

It is a Perl operator (perlop) very similar to system(), execute a command and then resumes the Perl script after the execution has finished but the return value is STDOUT of the command.

4. IPC::Open2

syntax: $output = open2(\*CHLD_OUT, \*CHLD_IN, 'command arg1 arg2');

It runs a process for both reading and writing and creates a pipe to both STDIN and STDOUT. It can be used in both ways:

$output = open2(my $out, my $in, 'command arg1 arg2');

OR without using the shell

$output = open2(my $out, my $in, 'command', 'arg1', 'arg2');

You can read about it more in the documentation: IPC::Open2.

5. IPC::Open3

syntax: $output = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR'command arg1 arg2', 'optarg',...);

It is very similar to IPC::Open2 with capability to capture all three file handles of the process, i.e., STDIN, STDOUT, and STDERR. It can also be used with or without the shell. You can read about it more in the documentation: IPC::Open3.

$output = open3(my $out, my $in, 'command arg1 arg2');

OR without using the shell

$output = open3(my $out, my $in, 'command', 'arg1', 'arg2');

a2p

syntax: a2p [options] [awkscript]

There is a Perl utility known as a2p which translates awk command to Perl. It takes awk script as input and generates a comparable Perl script as the output. Suppose, you want to execute the following awk statement

awk '{$1 = ""; $2 = ""; print}' f1.txt

This statement gives error sometimes even after escaping the variables (\$1, \$2) but by using a2p it can be easily converted to Perl script:

#!/usr/local/bin/perl
eval 'exec /usr/local/bin/perl -S $0 ${1+"[email protected]"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches
$, = ' '; # set output field separator
$\ = "\n"; # set output record separator

while (<>) {
chomp; # strip record separator
@Fld = split(' ', $_, -1);
if ($awk) {
$Fld[(1)-1] = '';
$Fld[(2)-1] = '';
print join($,,@Fld);
}
print join($,,@Fld) if $f1 . . $txt;
}

For further information, you can read it’s documentation: a2p

References

  1. Stajich, J. E.; Block, D.; Boulez, K.; Brenner, S.; Chervitz, S.; Dagdigian, C.; Fuellen, G.; Gilbert, J.; Korf, I.; Lapp, H.; Lehväslaiho, H.; Matsalla, C.; Mungall, C. J.; Osborne, B. I.; Pocock, M. R.; Schattner, P.; Senger, M.; Stein, L. D.; Stupka, E.; Wilkinson, M. D.; Birney, E. (2002). “The BioPerl Toolkit: Perl Modules for the Life Sciences”. Genome Research12(10): 1611–1618.
Download PDF

Tariq's areas of expertise include algorithm design, phylogenetics, MicroArray, Plant Systematics and genome data analysis. He is looking for a suitable PhD position in Bioinformatics. If you want him in your lab, let him know here

Leave a Reply