Solved

How to make a perl script run from another perl script

Posted on 2001-06-06
6
212 Views
Last Modified: 2010-03-05
Is it possible to get a perl script to run another perl script when it has finished running through its own loop?
Basically I have two scripts that work independently but when I patch the two together they stop working.
0
Comment
Question by:j1mlondon
  • 3
  • 3
6 Comments
 
LVL 5

Accepted Solution

by:
Sapa earned 25 total points
Comment Utility
There are several ways to implement it:

1. Run another script in the same copy of Perl-interpreter:

--------- script1.pl ------------
....
....
do "script2.pl";
------------- EOF ----------------

but all global variables and opened file descriptors will be inherited by script2.pl. It may cause the side effects.
If you cannot change the body of script1.pl use this command line: perl -e "do 'script1.pl'; do 'script2.pl';"

2. Launch the new interpreter and wait when it finished:

--------- script1.pl ------------
....
....
system "/usr/bin/perl script2.pl";
------------- EOF ----------------
or

perl -e "do 'script1.pl'; system '/usr/bin/perl script2.pl';"

3. Substitute the current Perl interpreter by the new one:

--------- script1.pl ------------
....
....
exec "/usr/bin/perl script2.pl";
------------- EOF ----------------

or

perl -e "do 'script1.pl'; exec '/usr/bin/perl script2.pl';"

On most of OS's you can omit the interpreter path from system or exec if execution bit is set and #!<path_to_perl> line exists (on Unix) or suffix .pl is registered as interpreted by Perl (Windows).

- Sapa

P.S. "Basically I have two scripts that work independently". If they are really independent, may be better to launch it simultaneously?

0
 

Author Comment

by:j1mlondon
Comment Utility
The scripts are not independent totally.
I actually wish to carry variables over from one script ie script1 into script2 exactly as they are.
Will this work using your first part of the answer?
0
 

Author Comment

by:j1mlondon
Comment Utility
The scripts are not independent totally.
I actually wish to carry variables over from one script ie script1 into script2 exactly as they are.
Will this work using your first part of the answer?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 5

Expert Comment

by:Sapa
Comment Utility
yes. the first method only is acceptable in this case.
0
 

Author Comment

by:j1mlondon
Comment Utility
Thanks. It worked , but I am getting error messages, even though it works how I want it to.
The error is..
Statement unlikely to be reached at *.pl line 18
       (Maybe you meant system() when you said exec()?
Can't exec "
Starting ":No such file or directory at *.pl  line 17, <FP> chunk 3.


What is the bit about chunk3 ????
0
 
LVL 5

Expert Comment

by:Sapa
Comment Utility
It means you have exec(...) function and the part of code after it. But function exec(...) does never return, except error is occured (as in your case).

So "Statement unlikely to be reached at *.pl line 18" is warning about this code cannot be executed because exec(...) will substitute current program by the new one. And may be you want to use system(...) which execute external program and continue execution after new program will be finished.

Can't exec "
Starting ":No such file or directory at *.pl  line 17, <FP> chunk 3

function exec(...) failed because there is no such program "\nStarting" (may be you need to remove \n ?)
"chunk 3" usually means an iterration number while reading from file FP.

- Sapa
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now