Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
No such thing as a small change
 
PerlMonks  

Re: "last expression" quiz

by Aristotle (Chancellor)
on Oct 19, 2005 at 18:38 UTC ( [id://501459]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to "last expression" quiz

Check Re^8: Unhappy returns (just updated) for some tests as to what gets returned when. Apparently:

  • If the last thing that happened was an iteration of a for loop, you will get back !1, alias scalar false (it is 0 and an empty string at the same time).
  • If the last thing that happened was an iteration of a while loop, you get back nothing at all.
  • If the last thing that happened was an if block, you get back the value of the last expression evaluated – that would be either the condition, if it evaluated to false, or the last expression from the body of the conditional block.

In other words, what perlsub says is deceptive and incomplete.

Update: oh goodness, it gets weirder still:

  • If the last thing that happened was an iteration of the do {} while $cond or do {} until $cond pseudo-loop, you get back a code reference (whether the condition turned out true of false).

It’s probably safe to say that Perl simply has no rule here, and that what you get back from a sub depends on implementation details of perl.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: "last expression" quiz
by Anonymous Monk on Oct 19, 2005 at 19:38 UTC
    It’s probably safe to say that Perl simply has no rule here, and that what you get back from a sub depends on implementation details of perl.
    Wouldn't it be more accurate to say that returning the value from the last expression in subs works fine. But statements (things like if, for, while) don't have values and return pretty much garbage?

      Maybe, but how do you reconcile that with the behaviour with if blocks, which work precisely as documented?

      Makeshifts last the longest.

        My point is mostly that the problem resides with how "for" is implemented, and not a problem with "sub" returning an incorrect value.
      But statements (things like if, for, while) don't have values and return pretty much garbage?

      Well, if works in that it returns the result of the last expression evaluated. It seems that's what looping constructs should do too. Wouldn't it make sense for this sub x { $_ for 10 } to return 10?

      -sauoq
      "My two cents aren't worth a dime.";
      
        Well, if works in that it returns the result of the last expression evaluated. It seems that's what looping constructs should do too. Wouldn't it make sense for this sub x { $_ for 10 } to return 10?
        Not really. If you're asking my opinion I'd say it should be an error if the sub is called in anything other than a void context. Note the following are also compilation errors...
        my $what = (if(1){print "true"}else{print "false"}); sub foo { return (for (1..3) { print $_ }); }
        You're not arguing that they shouldn't be, are you?
Re^2: "last expression" quiz
by robin (Chaplain) on Oct 21, 2005 at 06:51 UTC
    If the last thing that happened was an iteration of the do {} while $cond or do {} until $cond pseudo-loop, you get back a code reference (whether the condition turned out true of false).

    That sounds really strange, but sadly I can't reproduce it. I tried

    perl -e 'print sub {my $x=5; do {print "."} while $x--}->()'
    but the sub just seems to return 0. Can you give an example? (Also, what version of Perl are you using?)

      As the node you’re replying to already says: check Re^8: Unhappy returns for code.

      $ perl -v
      
      This is perl, v5.8.7 built for i486-linux

      Makeshifts last the longest.

        There is a bug in your code. You've missed out the ->() in the last two cases, so it's printing the sub rather than the result of calling the sub.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://501459]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.