Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to make a perl script run from another perl script

Posted on 2001-06-06
6
239 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PERL get the value for query 4 155
Perl script to parse xml file 2 125
Excel to CSV conversion with specific columns 5 84
html form to write data to csv 24 134
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

840 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