Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Just another Perl shrine
 
PerlMonks  

Call perl script from within another perl script

by huklberry (Novice)
on Sep 12, 2008 at 01:34 UTC ( #710774=perlquestion: print w/ replies, xml ) Need Help??
huklberry has asked for the wisdom of the Perl Monks concerning the following question:

I've got a perl script that gets executed from an html form. Within that perl script I want to call another perl script.

I've tried the following with no success.

system("perl /cgi-bin/test1.pl");
`perl /cgi-bin/test1.pl`;

-rwxr-xr-x 1 root root 191 2008-09-11 21:14 test1.pl

The script I am trying to call (test1.pl) simply opens a log file and adds a line. I'm just trying to see if the test1.pl script gets run and thus far it doesn't seem to be since the log file doesn't change.

Any suggestions on what I might be doing wrong?

Comment on Call perl script from within another perl script
Re: Call perl script from within another perl script
by jethro (Monsignor) on Sep 12, 2008 at 01:52 UTC

    system() needs file system paths, not urls. If your cgi-bin directory is really at /var/www/cgi-bin (for example), then you have to use that path to call the script.

    apache might also have problems finding the perl interpreter. You might use the complete path to perl to find out whether that is the problem

      I tried this as well with no luck....

      system("/usr/bin/perl /cgi-bin/test1.pl");

        I think you missed what jethro was saying. /cgi-bin/test1.pl is very unlikely to be a file path to the executable. It implies (though guarantees nothing) that / is your webroot. When you're on the command line, where is test1.pl? That's what you're after. You should also be checking the system return and using the multi-arg call. See perldoc -f system or search in here for more.

Re: Call perl script from within another perl script
by pjf (Curate) on Sep 12, 2008 at 04:14 UTC

    I'd try using the FindBin module. This means that even if there's weird things going on with chroot and paths being rewritten, you should hopefully get the other script invoked:

    use FindBin qw($Bin); system($^X, "$Bin/test.pl");

    This is assuming that your test1.pl is in the same directory as your calling script. If you're wondering what $^X is, it's the full path to your perl interpreter, which means the above line should work, even if Perl is not in your path.

    I'm using the two-argument version of system, which means that if your host is a Unix-flavoured system, it should avoid the shell (and hence any problems where your script may not have a shell to run).

    It's also recommended that you check the return value from system(), so if something does go wrong, you know about it. I personally use IPC::System::Simple for this task, which automates most of the checking and diagnostics, but being Perl there's more than one way to do it.

    All the best,

Re: Call perl script from within another perl script
by juster (Friar) on Sep 12, 2008 at 06:28 UTC

    I'll play devil's advocate and guess that the logfile does not have write access given to the user which the webserver runs as (http, apache?). Have you run test1.pl as a cgi script, through the web browser?

    Insert debugging print messages into the test1.pl script. This would verify if the script is not being executed.

Re: Call perl script from within another perl script
by cdarke (Prior) on Sep 12, 2008 at 07:58 UTC
    How do you know it does not work? Do you get an error back from system?my $stat=system('mumble mumble');Returned errors are your friends ;-)

    You could also try doing a -e filename on the script, and on perl, since we don't seem to know which one is causing the problem.
Re: Call perl script from within another perl script
by dHarry (Monsignor) on Sep 12, 2008 at 11:26 UTC

    To call/invoke/run a Perl script from another Perl script you can also do something like:

    # call_another_script.pl use strict; use warnings; do { local @ARGV; @ARGV = ("Hello", "World!"); eval { require "script_to_call.pl" }; }; print "\nOriginal args:\n"; for my $arg (@ARGV) { print "$arg\n"; }

    # script_to_cal.pl use strict; use warnings; print "script_to_call.pl args:\n"; for my $arg (@ARGV) { print "$arg\n"; }

      hi all, I need to execute perl script(script_to_call.pl) on my remote location can I do it in same way (with @ARGV)? This below one not parse @ARGV:
      do { local @ARGV; @ARGV = ("$OldTime","$NewTime","$WLSP/CDSServer11.log"); eval { system("ssh -o stricthostkeychecking=no $WLS './script_to_c +all.pl'"); }; };

        Instead of asking a question in a 4 year old thread, you should open a new thread. Your problem is also different to the problem addressed in this thread. See safely passing args through ssh for ideas.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://710774]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2014-04-16 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (418 votes), past polls