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

Hi, I've decided to implement three methods in such a way that if there is an error within the module the function will return a string representing what went wrong and if a no error occurs then the function will return undef. My question is, in my script that calls the method, can I save the returned string somehow without saving it directly to a $variable?
I want to call a function called rollback() if the methodfunction() returns a defined string value (this string represents the error to be logged).
ex. rollback(string returned from methodfunction) if defined methodfunction();
where I can pass the string returned from the methodfunction() into the function called rollback()

Replies are listed 'Best First'.
Re: how to return info from functions? Die.
by Narveson (Chaplain) on Aug 18, 2008 at 07:21 UTC

    Don't return your

    string representing what went wrong.

    Code that reports an error should die.

    This is called throwing an exception.

    sub methodfunction { # take a spoonful die 'too hot' if $temp > $hottest_allowed; die 'too cold' if $temp < $coldest_allowed; }

    You ask:

    in my script that calls the method, can I save the returned string somehow without saving it directly to a $variable?

    Yes, if your script wraps error-prone statements in an eval block, it can find the error in $@ (or $EVAL_ERROR if you use English). Admittedly this is a variable, but at least you didn't have to declare it.

    sub rollback { my $undesired = shift; print "That was $undesired. I shall try another bowl.\n"; } # and later ... eval {methodfunction()}; rollback($EVAL_ERROR) if $EVAL_ERROR;

    The advantage of working in this way is that everyone who reads your code will understand that the string in question is an error string. It's confusing to have the only return value of a function be an error.

Re: how to return info from functions
by repellent (Priest) on Aug 18, 2008 at 04:10 UTC
    If you can guarantee methodfunction() will not return either "0", 0, or the empty string "", then you can do:
    { rollback( methodfunction() || last ) }
    But seriously, declaring a variable up-front
    my $variable = methodfunction(); rollback($variable) if defined $variable;
    is a good thing because it is clear.
Re: how to return info from functions
by kyle (Abbot) on Aug 18, 2008 at 03:19 UTC

    I'd say just put this at the top of rollback():

    return if ! defined $_[0];

    I don't recommend this, but it almost works:

    rollback( methodfunction( 'foo' ) or goto NO_ROLLBACK ); NO_ROLLBACK: print "done\n"; sub rollback { my $s = shift; print "rollback('$s') called\n"; } sub methodfunction { return $_[0]; }

    Try it with undef instead of 'foo', and you can see it skips the call to rollback. However, it also does this for other false values (0, '0', or the empty string).

Re: how to return info from functions
by Skeeve (Parson) on Aug 18, 2008 at 06:26 UTC

    Maybe an other approach is interesting for you too. See Best Practices for Exception Handling


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: how to return info from functions
by GrandFather (Saint) on Aug 18, 2008 at 02:04 UTC

    You could use the default variable:

    rollback($_) if defined ($_ = methodfunction());

    Perl reduces RSI - it saves typing
Re: how to return info from functions
by JavaFan (Canon) on Aug 18, 2008 at 10:30 UTC
    Well, I wouldn't use your methods, but what you could do is:
    rollback (methodfunction ())
    and start your rollback function with
    sub rollback { my $reason = shift // return; ... }