Running commands on your system using

There are a number of ways to run commands on your system from a Running Commands on your System using Perl script: Perl system(“program info”); (“program info”); backticks $output = `programinfo`; open(FH, “| program info ”); **not covered further

Issues to consider Bela Tiwari [email protected] Do you want to collect the output of the command into a variable? Do you want your perl script to continue after you invoke the system command? If so, do you want the program to while the system command runs? Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk

Running commands on your system using Perl Backticks

The order of events:

1)the string enclosed by backticks undergoes variable interpolation On this course we will cover only the most simple ways to call programs. 2)the result is interpreted as a command by the system

e.g. @allfiles = `ls *.seq` $myprot = `transeq myfile.seq –stdout -auto`; If you plan on making your scripts widely available, and especially if you plan to write CGI scripts that will be available over the web, you will need to read further on security and good programming practice. Results can be collected in a scalar or a list variable

System Exec

system( ) is used to execute a command on your computer. Exec( ) is much like system( ) except that your script will when executed.

For example: For example:

system("blastall -d swiss -p blastp -i $seqfile –o blast.out"); exec("blastall -d /usr/db/blast/swiss -p blastp -i $seqfile –o blast.out");

Notes: will run a blast job on your computer and exit from your perl script.

The results of the blast search are put into a file called blast.out. If there was already a file with that name in your working directory, it would be overwritten unless you program checks into your script.

1 Final notes… Command Overall

•can be run with a list of arguments system •useful when you want to run a system command and carry out further steps in your perl script All of the functions introduced in this section are useful. More info at: http://www.perl.com/doc/manual/html/pod/perlfunc/system.html Choose the one that suits what you need to accomplish. •can be run with a list of arguments exec •useful when you want to execute a system command and do nothing Security is an issue for scripts being made widely available, especially over further in your script the web! You will need to read further on this subject to understand the security implications of the choices you make and to learn to program More info at: http://www.perl.com/doc/manual/html/pod/perlfunc/exec.html defensively. •easily overused (often used when in fact it would be better to use a built-in backticks perl function) •quick and dirty – which can be good or bad!

More info at: http://www.perl.com/doc/manual/html/pod/perlop.html#Regexp_Quote_Like_Operators

2