in reply to looking for a good idiom: return this if this is true

The fact that this question has to be asked at all and the contortions necessary to answer it points to a deficiency in Perl's semantics. The following code
thatroutine() and return;
would be simple and effective were it not for the fact that a return with no argurments is equivalent to return undef (or return () in list context). By making the undef or () mandatory in order to clear the "last expression evaluated" this whole discussion could have been avoided.

Replies are listed 'Best First'.
Re^2: looking for a good idiom: return this if this is true
by adamk (Chaplain) on Mar 07, 2005 at 13:55 UTC
    Hardly a deficiancy... especially when return; doesn't return undef _OR_ (), it returns both undef _AND_ () at the same time (dependending on context of course).

    We would have to replace all existing return; with return wantarray ? () : undef;

    ugh...
Re^2: looking for a good idiom: return this if this is true
by DentArthurDent (Monk) on Mar 07, 2005 at 17:58 UTC
    Doesn't the following bit of code work? It's only slightly more obtuse than what you propose.
    thatroutine() and return 1;
    UPDATE: I just tested this to prevent ignorance, and it does in fact work.
    UPDATE AGAIN: Ah, I missed the point. My code does not return the value returned by thatroutine(). If there are no side effects I suppose you could do:
    thatroutine() and return thatroutine();
    :-)
    ----
    My mission: To boldy split infinitives that have never been split before!
Re^2: looking for a good idiom: return this if this is true
by anjoschu (Sexton) on Mar 07, 2005 at 16:27 UTC
    This reminds me of how Applescript does it. When a sub is called in void context, the variable result gets assigned the return value. Sort of like this (pseudo):
    thatroutine() and return @result;
    Sounds like a Perl 6 feature request...