X BOL wishing you a very and Happy New year

Alternative content

Run bash script in Perl program !

  • Public
By Neel 1394 days ago

BioPerl is a compilation of Perl modules that can be used to build bioinformatics-related Perl scripts. It is used, for example, in the development of source codes, standalone software/tools, and algorithms in bioinformatics programmes. Different modules are easy to instal and include, making it easier to perform different functions. Despite the fact that Python is commonly favoured over Perl, some bioinformatics software, such as the standalone version of 'alienomics', is written in Perl. Often it's a major problem for beginners to execute certain Unix/shell commands in Perl script, so it's hard to determine which feature is unique to a situation.

Perl provides various features and operators for the execution of external commands (described as follows), which are unique in their own way.
 
They vary slightly from one another, making it difficult for Perl beginners to choose between them.
 
1. IPC::Open2

More at https://bioinformaticsonline.com/snippets/view/42919/perl-ipcopen2-module

2. exec””

 syntax: exec "command";

It's a Perl function (perlfunc) that executes a command but never returns, similar to a function's return statement.
While running the command, it keeps processing the script and does not wait until it finishes first, returns false when the command is not found, but never returns true.

3. Backticks “ or qx//

syntax: `command`;

syntax: qx/command/;

It's a Perl operator (perlop) that executes a command and then resumes the Perl script once the command has ended, but the return value is the command's STDOUT.

4. IPC::Open3

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

It is very similar to IPC::Open2 with the 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 $o ut, my $in, 'command arg1 arg2');

OR without using the shell

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

5.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:

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

More help at https://bioinformaticsonline.com/snippets/view/42920/perl-script-to-run-awk-inside-perl

6. system()

syntax: system( "command" );

It is also a Perl function (perlfunc) that 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" );

HELP

Here are some useful Perl cheat sheets that can be used as a quick reference guide.-- https://www.pcwdld.com/perl-cheat-sheet