How To Combine Several Perl Scripts Into One?
3
0
Entering edit mode
10.8 years ago
biolab ★ 1.4k

Dear all, I write several small scripts for sequence analysis, s1.pl, s2.pl s3.pl ect. I have to run them one-by-one using commands: perl s1.pl in.fa > out1.txt, followed by perl s2.pl out1.txt > out2.txt, and then perl s3.pl out2.txt > out3.txt ... I want to combine them, but trying the following structure fails.

open IN, 'in.fa'; 
open OUT, '> out1.txt'
 .....
close IN; close OUT;  

open IN2, 'out1.txt'; 
open OUT2, '> out2.txt'
 .....
close IN2; close OUT2;

So could anyone give me a solution? Thank you very much!

perl • 9.0k views
ADD COMMENT
3
Entering edit mode

don't open/close files but use STDIN && STDOUT: perl s1.pl < in.fa | perl s2.pl | perl s3.pl > out3.txt

ADD REPLY
3
Entering edit mode

I don't see any relevance to bioinformatics in this question so I'll make this a comment. The solution would be to create separate functions for each script which would be reusable and testable. The other approach would be to use pipes, as Pierre suggested, but consider whether you need 3 or more scripts when 1 would probably suffice (in other words, why close a file just to open it again and continue processing?). In either case, take a look at the Perl docs for open to see how to open a file in Perl (also see the examples on the Perl Maven site). From the command line, you can type perldoc perlfunc to see the best practices for using open(), and the perlsub docs (perldoc perlsub) describe how to write subroutines (one example shows you how to get input from the command line).

ADD REPLY
2
Entering edit mode

Agree that as it stands, this is a pure Perl programming question. Please indicate relevance to a bioinformatics research problem.

The brief answer is that when you find yourself solving a problem using multiple small scripts, it is time to implement them as functions, or methods in a module.

ADD REPLY
3
Entering edit mode
10.8 years ago

Not sure why multiple scripts are needed in the first place, but you can use system() to run command line line functions.

In other words, you can do this in your own separate Perl script (I'll call it wrapper.pl):

my $command = "perl s1.pl in.fa > out1.txt";
system($command);

$command = "perl s2.pl out1.txt > out2.txt";
system($command);

$command = "perl s3.pl out2.txt > out3.txt";
system($command);

Then, just run one command for your Perl script

perl wrapper.pl
ADD COMMENT
0
Entering edit mode

Dear all, thanks a lot for your answers, all of which are helpful! It's really related to bioinformatics, but my problem is dealing with perl. Thanks again!

ADD REPLY
1
Entering edit mode
10.6 years ago
Prakki Rama ★ 2.7k

You can also use backticks and run a wrapper script as Charles Warden suggested above.

`perl s1.pl in.fa > out1.txt`;
`perl s2.pl out1.txt > out2.txt`;
`perl s3.pl out2.txt > out3.txt`;
ADD COMMENT
0
Entering edit mode
5.1 years ago

We can use several commands on the same command line. In Windows cmd you can do like this:

C:\>perl s1.pl & perl s2.pl & perl s3.pl <enter>

ADD COMMENT

Login before adding your answer.

Traffic: 2577 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6