Calling a perl script from a perl script

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?

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?

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.

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! :slight_smile:

Picking Up Perl and Robert's Perl Tutorial. Online books, both with a very thorough coverage of the topic for newbs to the language.

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)


#!/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)

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