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

Hi All, I tried calling a perl prg with options from within another like:
eval { require "run_gql.pl -h -p "}; if ($? == -1) { print "Failed to execute: $!\n"; } elsif ($? & 127) { printf "\nChild died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; } else { printf "\nChild exited with value %d\n", $? >> 8; }
Here -h and -p are my prg options. Now is this correct?? Please help.... Thanks for your help.

Replies are listed 'Best First'.
Re: How to call a perl prg with options from within another ?
by Corion (Patriarch) on Oct 27, 2006 at 09:19 UTC

    You must have missed my answer from the Chatterbox. Have you read the documentation?

    Typing perldoc -f system and perldoc -f require at your command prompt shows you the documentation about the two functions. They can also be found on the web, as system and require.

    If you read the documentation for require closely, you will find that it says:

    ... require demands that a library file be included if it hasn't already been included. The file is included via the do-FILE mechanism, which is essentially just a variety of eval. Has semantics similar to the following subroutine: ...

    Nowhere the documentation mentions that passing of parameters is possible, and require also mentions that it will load a file only once.

Re: How to call a perl prg with options from within another ?
by Smaug (Pilgrim) on Oct 27, 2006 at 11:22 UTC
    Hi Bucchi,

    Your last question (Call one Perl Script from another) was very similar to this one.
    Once again, is this on Windows or Unix. On Win32 you could use Win32::Process

    Code from Scrat:
    #!/usr/bin/perl use warnings; use strict; use Win32; use Win32::Process; my $ProcessObj; Win32::Process::Create($ProcessObj, "C:\\perl\\bin\\perl.exe", "perl c:\\script2.pl", 0, NORMAL_PRIORITY_CLASS, ".")|| die ErrorReport(); exit; sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); }

    Update:Of course you would parse the options to the second program at the "perl c:\\script2.pl" and in that you could use something like Getopt
Re: How to call a perl prg with options from within another ?
by davorg (Chancellor) on Oct 27, 2006 at 11:29 UTC

    You seem to have ignored all the advice you got the last time you asked this question. Why would we spend time answering your questions if you're just going to ignore all our help?

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: How to call a perl prg with options from within another ?
by bart (Canon) on Oct 28, 2006 at 09:07 UTC
    You are confusing the keywords system and require. As you seem to want to call another perl script as a program, and system requires a valid command line, you could prepend the path to perl in it. And luckily for you, the special variable $^X contains it. So, try:
    system "$^X run_gql.pl -h -p";

    You may want to quote the perl path, using platform native quotes, if it contains spaces (or other special characters). Alternatively,

    system $^X, 'run_gql.pl', '-h', '-p';
    could work too — though you still may have to quote $^X in a string, on Windows, especially on slightly older perls, for example like this:
    qq("$^X")