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

Hi I want to retun the status of a perl script to perl module calling the script.

for Ex: I am having following Perl file:
test.pl & (test1.pm. , test_main.pl) they are present in different directories.

The Perl script test_main.pl call the module (sub abc()) defined in test.pm. This module intern calls test.pl using #*system ("perl test.pl");*# system call.

Now i want to return a variable back to test1.pm from test.pl how to do that? I was trying to pass an argument to system call to collect the return value but it did not worked.
I was using following call
#*system ("perl test.pl $returnVal");*#

Please suggest...

  • Comment on return a value from perl script to perl module call ing the script

Replies are listed 'Best First'.
Re: return a value from perl script to perl module call ing the script
by liverpole (Monsignor) on Feb 05, 2007 at 14:39 UTC
    Hi bsachin,

    If you look at the system documentation, you'll see that the exit status is returned in a kind of "encoded" format.  That is to say, the value of the exited process is returned in the variable $?, but shifted left 8 bits.

    What you need to do to get the exit status of the child is:

    my $exit_value = ($? >> 8);

    The lower 7 bits hold the number representing the signal (if any) that the child died with, and the 8th bit is set iff the child process dumped core.  ("iff" = if and only if).


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: return a value from perl script to perl module call ing the script
by kyle (Abbot) on Feb 05, 2007 at 14:40 UTC

    You can open a pipe and read from the child's output.

    my $child_pid = open my $child_fh, '-|', 'perl test.pl' or die "Can't run test.pl: $!"; my $first_line_of_output = <$child_fh>; close $child_fh;

    The close will wait for the child. If you want to pass back more fancy data structures, you could have the child produce Data::Dumper output which the parent would eval.

Re: return a value from perl script to perl module call ing the script
by Moron (Curate) on Feb 05, 2007 at 14:38 UTC
    Assuming there is a good reason why you can't move the contents of test.pl to a module, to avoid such shelling out...

    The test.pl can return a value to the shell as the argument of an exit() call. This will then be picked up by the system() call to run test.pl that occurs in test.pm.

    Caveat: Also see perlport for the impact of different platforms on the behaviour of the system() call.

    -M

    Free your mind

Re: return a value from perl script to perl module call ing the script
by BrowserUk (Patriarch) on Feb 05, 2007 at 14:58 UTC
Re: return a value from perl script to perl module call ing the script
by shmem (Chancellor) on Feb 05, 2007 at 23:35 UTC
    Hello bsachin,

    what reasons do you have to not call the test.pl script from within the current perl process (the one that is executing test_main.pl) ? Must it be that separate from your main script?

    You can always load and execute a script once via the do FILE mechanism. For example, having the main script

    #!/usr/bin/perl # script test_main.pl use test1; my $var = test1::abc('test.pl'); print $var,"\n";

    and the module file

    # file test1.pm package test1; sub abc { my $file = shift; my $variable = do $file; }

    which calls from sub this file:

    # file test.pl my $time = scalar localtime time; $time;

    then the script test_main.pl loads test1.pm and calls the subroutine abc with a filename as argument. The subroutine abc form test1.pm loads and executes the file. The last expression evaluated in that file is $time, so that value is returned by do and stuffed into the private $variable - which is passed to to the mainscript (it's the last expression of the subroutine), stored there into $var and printed out.

    See do. See also require, use, system, and read about the "quote and execute" qx operator in perlop.

    Calling perl from a perl script via system is almost always a bad idea.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}