Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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/");
`perl /cgi-bin/`;

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

The script I am trying to call ( simply opens a log file and adds a line. I'm just trying to see if the 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/");

        I think you missed what jethro was saying. /cgi-bin/ 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 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/");

    This is assuming that your 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 as a cgi script, through the web browser?

    Insert debugging print messages into the 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 (Abbot) 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:

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

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

      hi all, I need to execute perl script( 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'"); }; };

        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?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-07-25 17:06 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (174 votes), past polls