in reply to calling a perl script within a perl script

Is this really what you want to do?

Is the script you're calling big and unchangeable? If it's only a small one wouldn't it be better to include its code into your script?

If it's a big one, can you clone it and make a copy that's got this loop around its functionality?

If you're running an external command using backticks `` like that - firstly as sk pointed out you have put the filename outside the backticks, so it won't be provided as an argument to the script you're running, but I wouldn't recommend using backticks in the first place.

If you really must shell out to an external program you're better to use:

system("...") && die "Can't run ...: $!\n";
or
system("...") == 0 || die "Can't run '...' [$?]: $!\n"
or
open("... |") || die "Can't run ...: $1\n";
if you want the output from it - this way the operating system is going to report back to your script whether the program you shelled out to returned an error or a success.

`` is easy to use but generally sloppy in that it says something like "go away and try and run this, I don't really care if it works or not, but just give me back whatever came back from STDOUT (I don't care about STDERR either)" - which probably isn't what you want your program to do!

Update:

Thanks sauoq for pointing out the obvious mistake - NB system() unlike open() returns the output of the program that you ran modified by some rules (which sauoq has expanded below)

Sorry I rushed out my answer as I was racing out the door running late for a conference!

Replies are listed 'Best First'.
Re^2: calling a perl script within a perl script
by sauoq (Abbot) on Nov 27, 2005 at 12:55 UTC
    system("...") || die "Can't run ...: $!\n";

    No, that's almost never what you would want because system(), unlike qx(), returns the exit status which is conventionally 0 on success.

    It usually better to be explicit with your system() calls:

    if (system( ... ) == 0) { # it succeeded. } else { # it failed. my $exit_val = $? >> 8; my $signal = $? & 127; my $coredump = $? & 128; }

    I should note that some people much prefer to write that clause as if (0 == system( ... )) and will go into violent spasms if they see it written as above. Of course, they've probably fallen out of their chairs by now, so we don't have to worry about them too much.

    -sauoq
    "My two cents aren't worth a dime.";