sz has asked for the wisdom of the Perl Monks concerning the following question:

Desperately seeking wisdom...

I've been banging my head against a simple problem all day. I have a simple cgi script that takes three parameters from a form, creates a file and then is supposed to run a seperate script (indexer.pl, located in the same directory) which updates an index file with some elements of the new file. All works well until it's time for the indexer script to run.

Since I'm not interested in capturing the output of indexer.pl, I first tried:

my $status = system("perl /path/to/indexer.pl"); error("Index update failed") unless ($status == 0);

This resulted in the error being called. Running the script from the command line it looked like indexer.pl thought it was located in a different directory. I put a chdir statement at the top of indexer.pl but this did not help.

On the chance that I was using the system call incorrectly, I also tried back ticks despite the inefficiency of ignoring their return values:

`perl /path/to/indexer.pl`;

Again, no luck. The results where the same whether I use relative or full paths. From what read in the Perl Cookbook and Programming Perl I think I'm using the functions correctly. I even wrote a three line script that called another which then printed its current directory to a file. Those two files worked as expected; they recognized they were in the same directory. Can anyone point me in the right direction?

Thanks, sz

Replies are listed 'Best First'.
Re (tilly) 1: Problem calling one script from another
by tilly (Archbishop) on Dec 11, 2000 at 10:09 UTC
    Put $! in your exit error message and you will almost certainly be told exactly what your problem is. In fact I would take a tip from perlstyle and write it like this:
    my $cmd = "perl /path/to/indexer.pl"; system($cmd) or die "Cannot run '$cmd': $!";
    (Unless of course I took a tip from my understanding of Perl and arranged to do or require the script. Actually I would probably put it's functions into a module then use that.)

    UPDATE
    Oops.

    The code snippet as written is useless. See comments below and the fact that the return for success is 0.

    But the reference to perlstyle is good, as well the advice on not doing system commands is likewise.

      Unfortunately, system() and backticks don't set $!. They do set $?, however, with the exit status << 8 or'ed with the signal that terminated the subprocess, if any. Here's an example from system on checking the value of $?:
      You can check all the failure possibilities by inspecting $? like this: $exit_value = $? >> 8; $signal_num = $? & 127; $dumped_core = $? & 128;
        Oops.

        That is why I test code before I put it into production.

        (As you can see I really do take my advice about modules. And I don't often use system. But I should have known better on that one. Well I did but forgot.)

Re: Problem calling one script from another
by chipmunk (Parson) on Dec 11, 2000 at 09:59 UTC
    Two things to check:

    Is perl in the path for the user that CGI scripts run as (usually the user nobody)?

    What are the permissions on indexer.pl? Does the aforementioned user have read access?

Re: Problem calling one script from another
by markwild (Sexton) on Dec 11, 2000 at 12:23 UTC
    I think what you'd be better off doing is "requiring" or "use"ing the indexer.pl script, rather than running it from a new shell. So, I'm guessing that if the current dir while running your cgi script is the cgi directory, you should just be able to put require "indexer.pl"; into your script. I think you'd need to monkey a bit with the indexer script, making it return a 'true' (i.e. non-zero) value at the end. Thoughts, anyone?
      Well, i think a DUM way to do that would be:

      use LWP::Simple;

      get "http://domain.com/cgidir/etc...etc...etc";

      # jpsama
Re: Problem calling one script from another
by Dragonfly (Priest) on Dec 11, 2000 at 20:24 UTC
    This is a shot-in-the-dark, but it worked for me when I had to call a Perl script once.

    Instead of

    `perl /path/to/indexer.pl`;

    try

    `/usr/bin/perl /path/to/indexer.pl`;

    where you specify the perl executable's path in your statement...

    As I said before, it worked once for me; then again, I'm a total newbie and should be treated thusly. ;-)

    -Dragonfly

Re: Problem calling one script from another
by InfiniteSilence (Curate) on Dec 11, 2000 at 22:25 UTC
    try this:
    #!/usr/bin/perl -w
    do 'indexer.pl';
    1;
    Celebrate Intellectual Diversity
Re: Problem calling one script from another
by sz (Friar) on Dec 11, 2000 at 23:30 UTC
    Thanks everyone

    Needing a quick fix I made the indexer.pl script a part of the primary one. There were some permissions issues that were straightend out in the process. I'll be trying out your suggestions tonight to find a real explanation for the original problem. I'll post the results.

    Thanks again... sz