Calling a perl script from a perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling a perl script from a perl script
# 1  
Old 02-06-2007
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?
# 2  
Old 02-06-2007
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?
# 3  
Old 02-06-2007
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
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.
# 5  
Old 05-23-2009
Quote:
Originally Posted by karlsworld
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! Smilie
# 6  
Old 05-24-2009
Picking Up Perl and Robert's Perl Tutorial. Online books, both with a very thorough coverage of the topic for newbs to the language.
# 7  
Old 05-24-2009
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling perl from shell script

I am calling a perl script from shell script. $ cat mah_appln_bkp_oln.ksh #!/bin/ksh . /udb/home/udbappln/.profile . /udb/home/udbappln/sqllib/db2profile Com=/udb/udbappln/utility/systemscripts/currentUMR perl $Com/backup.pl -d dbname --tsm --purgetsmcopies 21 --purgetsmlogs exit 0 ... (1 Reply)
Discussion started by: ilugopal
1 Replies

2. Shell Programming and Scripting

calling a shell script present on another server using perl script.

Hi, I am working on a sever A. I want to write a perl script to execute a shell script persent on the server B. please help me in this. thanks in advance. (3 Replies)
Discussion started by: anandgodse
3 Replies

3. Shell Programming and Scripting

How we can pass the argument when calling shell script from perl script

Can someone let me know how could I achieve this In one of per script I am calling the shell script but I need to so one thing that is one shell script call I need to pass pne argument.In below code I am calling my ftp script but here I want to pass one argument so how could I do this (e.g:... (5 Replies)
Discussion started by: anuragpgtgerman
5 Replies

4. Shell Programming and Scripting

calling a perl script with arguments from a parent perl script

I am trying to run a perl script which needs input arguments from a parent perl script, but doesn't seem to work. Appreciate your help in this regard. From parent.pl $input1=123; $input2=abc; I tried calling it with system("/usr/bin/perl child.pl $input1 $input2"); and `perl... (1 Reply)
Discussion started by: grajp002
1 Replies

5. Shell Programming and Scripting

Calling a Perl script in a Bash script -Odd Situation

I am creating a startup script for an application. This application's startup script is in bash. It will also need to call a perl script (which I will not be able to modify) for the application environment prior to calling the application. The problem is that this perl script creates a new shell... (5 Replies)
Discussion started by: leepet01
5 Replies

6. Shell Programming and Scripting

Calling 3 perl script from one

hi all, I have 3 perl scripts a.pl,b.pl and c.pl each of these work when i pass a date for eg: perl c.pl 2010-05-27 now i want to write a perl script that would call the 3 scripts and make it run all the 3 scripts (a.pl,b.pl,c.pl) parallelly rather than 1 after the other....... pls... (2 Replies)
Discussion started by: siva_nagarajan
2 Replies

7. Shell Programming and Scripting

Calling a shell script from a perl script

Hi, I have one shel script which returns some value and I am calling this shell script from a perl script which needs the out put/return value of shell script. But I don't know how to collect the output/return value of the shell script. Can any one give some idea on it? For example: The... (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

8. Shell Programming and Scripting

Calling Expect script in Perl...

I call a EXPECT script from my perl script with machine IP and a FIle. The script logins to the machine and exports the value. The values to be exported or stored in a file. I have close to 10 machines and I have created 10 files and pass the corresponding files in command line, Now I could like... (4 Replies)
Discussion started by: ramkriz
4 Replies

9. Shell Programming and Scripting

Calling Winzip from perl script

Hi, I would like to invoke "Winzip" utility from a perl script, input the name of zip file and provide output path for unzipped files. Any pointers will be appreciated. Thanks (5 Replies)
Discussion started by: MobileUser
5 Replies

10. Shell Programming and Scripting

calling a shell script from perl

Hi all, Not sure if this is the right forum to post query regarding perl script. I have a perl script which internally calls a shell script. My problem is that the shell script should be passed command line arguments. I call a shell script from perl using: system("sript.sh"); How do... (3 Replies)
Discussion started by: gurukottur
3 Replies
Login or Register to Ask a Question