![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages 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-08-2008 01:59 AM |
| Calling Winzip from perl script | MobileUser | Shell Programming and Scripting | 5 | 04-04-2007 04:51 AM |
| calling a shell script from perl | gurukottur | Shell Programming and Scripting | 3 | 10-05-2006 12:48 PM |
| Box A's perl script calling box B's shell script | new2ss | Shell Programming and Scripting | 1 | 09-13-2006 07:17 AM |
| Calling CGI Perl in Shell script [urgent] | DeepakXavier | Shell Programming and Scripting | 0 | 10-09-2005 02:51 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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? |
|
||||
|
Quote:
Quote:
Did you try to find out whether that is the case? |
|
||||
|
Quote:
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. |
|
||||
|
Quote:
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! ![]() |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|