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

In other languages with which I've worked, "return 1" from functions meant the function's return status would be "1" and could be tested accordingly. I was under the assumption (I know, I know, ASSuME :-)) that you could do the same thing in Perl. However, a "return 1" from a home-grown Perl module (Verify_vars.pm) does not automagically assign $?. I can do that by manually assigning it before returning, but I'd rather understand what's wrong with my thinking and do it the best/preferred way. Thanks in advance for any prayers ;-) Brother Beau

Replies are listed 'Best First'.
Re: Return and .pm
by fruiture (Curate) on Nov 27, 2002 at 18:53 UTC

    Don't mix "return status" of a program and "return value" of a function. In C you can perhaps confuse them, because main() return()s the program's exit code.

    That's different in Perl: return() returns a value from a subroutine. exit() returns a value(better "status" or "exit code") from a program.

    $? contains the last "exit code" from a child of the current program. $? is set after qx//,system, (...). It has nothing to do with modules, for the loading of modules doesn't invoke a new process, it happens in the perl process your program runs in.

    As lready mentioned, `perldoc perlsub` should be read, also perlmod, as well as `perldoc -f return` and `perldoc -f exit`.

    --
    http://fruiture.de
Re: Return and .pm
by particle (Vicar) on Nov 27, 2002 at 18:43 UTC

    you need to assign the return value to something, as in my $status = my_sub( $my, $args );

    read perlsub for more info.

    Update: here's an example (adapted from perlsub)...

    package return_test; sub max { my $max = shift @_; foreach my $foo (@_) { $max = $foo if $max < $foo; } return $max; } package main; $bestday = return_test::max( $mon, $tue, $wed, $thu, $fri );

    ~Particle *accelerates*

Re: Return and .pm
by chromatic (Archbishop) on Nov 27, 2002 at 19:47 UTC

    A module is not a function. A function is not a child process. What are you trying to accomplish?