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

I'm intimately familar with using the following to gracefully exit a program when an error is encountered:
someFunction() or die "$!";

However, in the current project I don't want to die on error. I want to return a value.
someFunction() or return(1);
This, however, doesn't work. An error is still printed to stdout and the program colapses on itself. I've also tried this:
my $value; someFunction() or $value = 1; return($value) if $value;
with no luck either.

Any ideas?

Replies are listed 'Best First'.
Re: function or something
by philcrow (Priest) on Jul 20, 2006 at 15:35 UTC
    If something is dying, try wrapping it in an eval {}; block. After the block any error will be in $@.

    Phil

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: function or something
by bobf (Monsignor) on Jul 20, 2006 at 15:43 UTC

    If I understand you correctly, you want to set $!. From the docs (emphasis mine):

    If used numerically, ($!) yields the current value of the C errno variable, with all the usual caveats. (This means that you shouldn't depend on the value of $! to be anything in particular unless you've gotten a specific error return indicating a system error.) If used an a string, yields the corresponding system error string. You can assign a number to $! to set errno if, for instance, you want "$!" to return the string for error n, or you want to set the exit value for the die() operator.

    Update: Now that I reread the question, I can't tell if you're trying to return an exit value from a program (as I initially thought) or simply trap a fatal error within the same program. Please elaborate if I misunderstood.

Re: function or something
by ysth (Canon) on Jul 20, 2006 at 16:25 UTC
    This, however, doesn't work. An error is still printed to stdout and the program colapses on itself.
    "An error"? What error? Providing details like that can really help others help you. Also, I suspect the error went to stderr, not stdout.
Re: function or something
by imp (Priest) on Jul 20, 2006 at 16:47 UTC
    One thing to keep in mind if you use eval {} is that a return inside the eval block returns from that block - not from the outer function.
    sub example { my $number = shift; eval { if (2 / $number > 3) { return 1; } }; print "Still here\n"; }
    To capture the value returned, you can do this:
    my $value = eval { # Do stuff; return 4; };

    Alternately:

    my $value; eval { # Do stuff; $value = 4; };
Re: function or something
by mrbbking (Hermit) on Jul 20, 2006 at 16:03 UTC
    I'm not sure if you want your *program* to return a value, or you want the *subroutine* to return a value. If the program, do
    someFunction() or exit(1);
    If the function, you'll have to catch the value the sub returns and do your thing with it.
    my $ret = someFunction(); if( $ret ){ do_your_thing(); maybe_return_here(); }
    /me needs a new .sig
Re: function or something
by shmem (Chancellor) on Jul 20, 2006 at 16:50 UTC
    Depending on what modules you are using in your program, you might want to set up signal handlers
    { local $SIG{__DIE__}; local $SIG{__WARN__}; $ret = eval { someFunction() }; warn $@ if $@; }
    to mask some other modules handlers, which might choose to just exit($!) or tread on $@ or $!.

    --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}
Re: function or something
by sgifford (Prior) on Jul 21, 2006 at 03:02 UTC
    The syntax you describe seems to work fine for me. This example works, at least:
    my $ret = test(); print "Test returned $ret\n"; sub test { unlink("t64.test") or return 0; print "Test completed.\n"; return 1; }

    Maybe you need to look elsewhere for your problem?