Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Short-circuiting a map list. (This works! But ...)

by BrowserUk (Pope)
on Oct 08, 2011 at 15:21 UTC ( #930361=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Short-circuiting a map list. (This works! But ...)
in thread Short-circuiting a map list.

Okay. If what you are telling me is that I am utilising unspecified behaviour, then how about this version which produces identical output but, as far as I can tell, does nothing that is either prohibited or unspecified?

sub a{ my $x = shift; return do{ { print 'a'; map { print 'b'; last if $_ == $x; $_; } @_; print 'c'; } }; } my @data = 0 .. 9; print for a( 5, @data );

Does that mean uncovered an obscure bug?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re^3: Short-circuiting a map list. (This works! But ...)
Download Code
Re^4: Short-circuiting a map list. (This works! But ...)
by Anonymous Monk on Oct 08, 2011 at 15:33 UTC

    Does that mean uncovered an obscure bug?

    Probability is high :)

    do does say "do BLOCK" does *not* count as a loop, so the loop control statements "next", "last", or "redo" cannot be used to leave or restart the block. See perlsyn for alternative strategies.

      do does say "do BLOCK" does *not* count as a loop,

      I don't think that is relevant as it is the anonymous block inside the do block that is the target of the last?

      I can also get the same output with the same weirdness whilst avoiding the do block:

      sub b{ my $x = shift; goto sub { { print 'ba'; map { print 'bb'; last if $_ == $x; $_; } @_; print 'bc'; } }; } my @data = 0 .. 9; print for b( 5, @data );

      I am completely at a loss to explain the return values being stacked on top of the passes values?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I don't think that is relevant as it is the anonymous block inside the do block that is the target of the last?

        I am completely at a loss to explain the return values being stacked on top of the passes values?

        Me too , except the aforementioned warning regarding unspecified behavior with loop control structure -- if you can last it, its a loop

Re^4: Short-circuiting a map list. (This works! But ...)
by ikegami (Pope) on Oct 08, 2011 at 18:15 UTC
    The behaviour is just as unspecified for a bare loop ({ ... }) as it is for a while loop (while (...) { ... }).
      If the last statement is a loop control structure like a foreach or a while , the returned value is unspecified.

      But the bare block is not the last statement.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        It's not the last statement of the sub, but it is the last statement of the do. What do you think do returns? The value to which the loop evaluates, which isn't defined.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930361]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-07-13 17:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (250 votes), past polls