Solved

How to make a perl script run from another perl script

Posted on 2001-06-06
6
230 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
ID: 6159013
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
ID: 6159032
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
ID: 6159044
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 5

Expert Comment

by:Sapa
ID: 6159716
yes. the first method only is acceptable in this case.
0
 

Author Comment

by:j1mlondon
ID: 6159752
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
ID: 6160093
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

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