in reply to Re: "last expression" quiz
in thread "last expression" quiz

You see, you are wondering about some stuff and learn a lot by side effect. Thank you very much for your answers.

With regard to the very "last expression", I guess we could conclude perlsub is not accurate there. It is true that some value is always returned, but in the subroutine

sub foo { 1 for @_ }
there are only TWO expressions, namely "1", and "@_". The "last expression" by definition should be one of them, but the return value is none of them.

Does anybody disagree with that conclusion?

Replies are listed 'Best First'.
Re^3: "last expression" quiz
by sauoq (Abbot) on Oct 20, 2005 at 09:38 UTC
    Does anybody disagree with that conclusion?

    I certainly don't. An even better example:

    sub foo { 1 for 1 }
    Now there's only one expression which always evaluates to the same thing, 1, and yet the sub unintuitively returns !1. Certainly a patch is needed. Whether it should just be a doc patch to perlsub or different behavior should be forced, I don't know.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Okay, I figured it was some peephole optimization thing. This is not what I would have predicted for deparse output, but it kind of makes sense.
      $ perl -MO=Deparse -e 'sub foo { 1 for 1 } foo' sub foo { foreach $_ (1) { '???'; } } foo ; -e syntax OK
      The '???' appears for other constants, with which it would generate "useless use of constant in void context" warnings. This warning is suppressed for a few common constants. So I guess the opcode optimizer just forgets WHAT constant was there.

      --
      [ e d @ h a l l e y . c c ]