The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calling a C-function froma Perl script jisha Shell Programming and Scripting 9 05-07-2008 09:59 PM
Calling Winzip from perl script MobileUser Shell Programming and Scripting 5 04-04-2007 12:51 AM
calling a shell script from perl gurukottur Shell Programming and Scripting 3 10-05-2006 08:48 AM
Box A's perl script calling box B's shell script new2ss Shell Programming and Scripting 1 09-13-2006 03:17 AM
Calling CGI Perl in Shell script [urgent] DeepakXavier Shell Programming and Scripting 0 10-08-2005 10:51 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-06-2007
Registered User
 

Join Date: Jan 2006
Posts: 125
Stumble this Post!
Calling a perl script from a perl script

Code:
printf(”Going to call another script... \n”);
system(”/my_dir/B.pl”); # call another perl script B.pl
exit;
Hi everyone, above is an example that i am using to call another perl script from the current perl script.

I have two concerns :
1) This there a better way of achieving the same purpose ( ie call another perl script from a perl script)?

2) Notice there is an exit command in my calling script. Will the exit command be executed only after B.pl is completed OR it will be executed immediately B.pl is called?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-06-2007
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,428
Stumble this Post!
Quote:
Originally Posted by new2ss
1) This there a better way of achieving the same purpose ( ie call another perl script from a perl script)?
Yes. Use require().

Quote:
Originally Posted by new2ss
2) Notice there is an exit command in my calling script. Will the exit command be executed only after B.pl is completed OR it will be executed immediately B.pl is called?
I think unless your B.pl runs itself in background mode (such as that of daemons, for instance), the perl process for B.pl will not exit before it completes, and hence system() in your calling script will not return (note that system() just executes a perl process in the same way as other processes). If the exit() occurs after system(), you can assume it will only be executed when B.pl exits for whatever reason.

Did you try to find out whether that is the case?
Reply With Quote
  #3 (permalink)  
Old 02-06-2007
Registered User
 

Join Date: Jan 2006
Posts: 125
Stumble this Post!
Quote:
Originally Posted by cbkihong
Yes. Use require().

I think unless your B.pl runs itself in background mode (such as that of daemons, for instance), the perl process for B.pl will not exit before it completes, and hence system() in your calling script will not return (note that system() just executes a perl process in the same way as other processes). If the exit() occurs after system(), you can assume it will only be executed when B.pl exits for whatever reason.

Did you try to find out whether that is the case?
B.pl is not a background mode, its a normal user script.

i qoute from perldoc for system "...Does exactly the same thing as exec LIST , except that a fork is done first, and the parent process waits for the child process to complete...." So i can take it that my first script will wait for B.pl to finish before exiting.

the perldoc entry for exec says " ..The exec function executes a system command and never returns--". Therefore i can assume that after it calls B.pl, the calling script will exit.

My intention is not to wait for B.pl to finish, ( ie, runs B.pl and the caller script itself exit ) therefore i should use exec? Any draw backs?
Reply With Quote
  #4 (permalink)  
Old 02-06-2007
Registered User
 

Join Date: Feb 2007
Posts: 6
Stumble this Post!
I like to require any other files at the beginning of the main script then just call the routines in them as needed. This may or may not be the best way, but it works pretty well for me. By the way, if I remember correctly, I believe the file you're calling does not necessarily need the standard perl script header, does not have to end in "pl" or "cgi", and does not need executable permissions.

(main_file.pl)

#!/usr/bin/perl
require "other_file.pl";
&do_something;
&do_something_else;
exit;

(other_file.pl)

sub do_something{
print "Content-type: text/html\n\n";
print "ok";
}
sub do_something_else{
print "Content-type: text/html\n\n";
print "ok again";
}
1; # return true



I didn't test that script, but I know it's real close to what I do.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0