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

Is there a way to set my own return code back to the shell?
####################################################
#main program "junk.pl"
$STATUS = "FAILED";

print "just a return code test to the shell\n";

if ( $STATUS eq "PASSED" ){ return 0; }
elsif ( $STATUS eq "FAILED" ){ return 1; }
elsif ( $STATUS eq "ABORTED" ){ return 2; }
else { return 3; }
##################################################

I would like it to return the return code back to the shell. Then do a echo $? to get the number I specified.
This is the output of the above program:
$ perl junk.pl
just a return code test to the shell
Can't return outside a subroutine at junk.pl line 8.

Thanks.

Replies are listed 'Best First'.
Re: Getting return code to shell
by Zaxo (Archbishop) on Jun 15, 2004 at 01:34 UTC

    You want exit. I'd do it like this,

    my %exit_code = { PASSED => 0, FAILED => 1, ABORTED => 2 ); # ... exit $exit_code{$STATUS};

    After Compline,
    Zaxo

      or even:

      use constant { PASSED => 0, FAILED => 1, ABORTED => 2, }; # then use: exit PASSED; exit FAILED; #etc