in reply to What's the opposite of // (err) operator?

If you want to explicitly return undef you rather not specify it and return the right thing in all contexts:

defined $x or return; return morefoo ($x);

In scalar context, that will return undef, in list context, that will return the empty list. If $x is defined, the context is propagated to morefoo.


Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: What's the opposite of // (err) operator?
by John M. Dlugosz (Monsignor) on May 11, 2011 at 06:46 UTC
    Nice. I always forget about the low-precidence variations. Combining that with GrandFather's reply gives:
    my $x= blahblah... err return; ... continue using $x
    That reads with the proper "flow" and connotations: if I find an undef after this computation, quit now.

    Now what version of Perl introduced err?

    later... Apparently not the one I'm using! The construct above gives an error, "missing operator before err?", but the syntax works with or so it's not the precedence, but rather it seems not to know what err means!

    The perldoc perlop (I'm reading for Perl 5.10.1 since that's what I'm using) has a section called “Logical or, Defined or, and Exclusive Or”, and it discusses the low-precedence or and xor but doesn't actually mention any kind of "defined or" keyword. How odd.

      Do not use the err keyword. It was introduced in the 5.9.x development series, and made available to all the 5.8.x builds by patches (maintained by me).

      The community however decided that err did not have enough good reasons to stay and was removed later on. That made me feel sad, but I am not the one that makes the final decision. The bottom line is that err, which was a special type of operator that could be overruled by a local function with the same name, is not available in 98% perl. Do not use it.


      Enjoy, Have FUN! H.Merijn
        Because it conflicted with functions people had named? That's always the problem with adding a keyword.

        The docs still show some vestiges. That ought to be fixed.

      There isn't one. But then again, you don't need one. A low-precedence variation would cause a needless assignment to be performed.

      my $x = EXPR // return; ... continue using $x
        Very interesting. So the precedence of // is low enough so it uses everything except the assignment as the left arg. It seems odd to me to return in the middle of a value-bearing expression, but I'm too used to C++. Thanks for pointing that out.