Calling a perl script from a perl script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Calling a perl script from a perl script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-06-2007
new2ss new2ss is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 23 January 2014, 8:51 PM EST
Posts: 134
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #2  
Old 02-06-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
 
Join Date: Sep 2002
Last Activity: 4 May 2014, 6:22 AM EDT
Location: Hong Kong, China
Posts: 1,622
Thanks: 0
Thanked 9 Times in 8 Posts
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?
Sponsored Links
    #3  
Old 02-06-2007
new2ss new2ss is offline
Registered User
 
Join Date: Jan 2006
Last Activity: 23 January 2014, 8:51 PM EST
Posts: 134
Thanks: 0
Thanked 0 Times in 0 Posts
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?
    #4  
Old 02-06-2007
karlsworld karlsworld is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 27 February 2007, 12:22 AM EST
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #5  
Old 05-23-2009
lbailey lbailey is offline
Registered User
 
Join Date: May 2009
Last Activity: 24 September 2009, 7:11 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by karlsworld View Post
I didn't test that script, but I know it's real close to what I do.
Thanks karlsworld,
I ran the code and it worked, but I need to pass arguments, and simply adding them (in the same way as you do with a subroutine) doesn't work. This is my first experience with perl, and the inane roadblocks are so frustrating!!

Sorry - I looked everywhere and tried a bunch of approaches, with no success. Any help would be appreciated!
Sponsored Links
    #6  
Old 05-24-2009
curleb curleb is offline Forum Advisor  
Registered User
 
Join Date: Mar 2008
Last Activity: 23 September 2013, 4:29 PM EDT
Location: Surrounded...
Posts: 383
Thanks: 1
Thanked 28 Times in 27 Posts
Picking Up Perl and Robert's Perl Tutorial. Online books, both with a very thorough coverage of the topic for newbs to the language.
Sponsored Links
    #7  
Old 05-24-2009
lbailey lbailey is offline
Registered User
 
Join Date: May 2009
Last Activity: 24 September 2009, 7:11 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Actually "Picking Up Perl" doesn't even mention calling other perl scripts, and "Robert's Perl Tutorial" is for Windows and it only mentions doing it with modules. Nevertheless, I managed to use it and karlsworld's script to piece together something that works.

Also, I forgot to mention that I don't want to use system() because I need to pass multiple file names and want to be able to accept spaces without the headache of figuring out which pieces go together in the target method.

Anyway. For those reading this in the future, this works:

(main_file.pl)

Code:
#!/usr/bin/perl
use strict;
use warnings;
require "other_file.pl";

my $string = "Hello";
my $string2 = "Hello again";

do_something($string);
my ($s1, $s2) = do_something_else($string2);
print "Received: $s1 $s2\n";

exit;

(other_file.pl)

Code:
sub do_something($){
        my $string = $_[0];
        print "Printing: $string\n";
}

sub do_something_else($){
        my $string = $_[0];
        print "Now printing: $string\n";
        my $yyyy = 2009;
        return ($string, $yyyy);
}

1; # return true

And the output is:
Printing: Hello
Now printing: Hello again
Received: Hello again 2009
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How we can pass the argument when calling shell script from perl script anuragpgtgerman Shell Programming and Scripting 5 03-21-2012 03:57 PM
calling a perl script with arguments from a parent perl script grajp002 Shell Programming and Scripting 1 02-02-2012 11:57 PM
Calling a Perl script in a Bash script -Odd Situation leepet01 Shell Programming and Scripting 5 09-15-2011 04:56 PM
Calling a shell script from a perl script siba.s.nayak Shell Programming and Scripting 1 07-23-2009 09:45 AM
Box A's perl script calling box B's shell script new2ss Shell Programming and Scripting 1 09-13-2006 06:17 AM



All times are GMT -4. The time now is 11:52 AM.