Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Does redo create a new dynamic scope?

by neosamuri (Friar)
on Jul 18, 2007 at 19:35 UTC ( [id://627346]=note: print w/replies, xml ) Need Help??


in reply to Does redo create a new dynamic scope?

The $& is not set on the redone block. Look at perlre

The numbered match variables ($1, $2, $3, etc.) and the related punctuation set ($+ , $& , $` , $' , and $^N ) are all dynamically scoped until the end of the enclosing block or until the next successful match, whichever comes first.

This block should give the behavior you were expecting, by storing the $& from the previous run.

#!/usr/bin/perl use strict; use warnings; my $b; for( $_ = "fred"; s/(.)//; ) { $b = $& if $&; print "I saw the character [$b]\n"; redo if $b eq 'e'; }

Replies are listed 'Best First'.
Re^2: Does redo create a new dynamic scope?
by ikegami (Patriarch) on Jul 18, 2007 at 21:17 UTC

    Probably so, but why was the enclosing block exited? Keep in mind the block enclosing the regexp is NOT the curlies, it's a larger one that includes the whole for loop.

    According to what you are saying, the output of the loops below should both be the same ([a][]). They should both be the same ([a][a]), but the second is producing the wrong ouput. The answer to "why do these two loops have different outputs" is answer to the OP.

    print("norm: "); for (my $i; $i ? $i<2 : 'a'=~/./g; ++$i) { print("[$&]"); } print("\n"); print("next: "); for (my $i; $i ? $i<2 : 'a'=~/./g; ++$i) { print("[$&]"); next; } print("\n");
    norm: [a][a] next: [a][]
Re^2: Does redo create a new dynamic scope?
by TGI (Parson) on Jul 18, 2007 at 21:07 UTC

    Update: As ikegami illustrates below, neosamurai and I were in agreement about the answer to the wrong question. It did seem a bit odd that someone as knowledgeable as brian_d_foy would be asking a question with an answer that seemed so clear to me...

    My original post remains below.


    neosamurai is right. $& goes out of scope when redo moves code execution back to the top of the block. redo doesn't reevaluate the loop's conditional (where we set $&), so a new value for $& is never set.

    There doesn't seem to be anything special about a for loop, as the same behavior can be seen in a while loop.

    for( $_ = 'fred'; print "loop\n" and s/(.)//; ) { print qq'I saw [$&]\n'; redo if $& eq 'e'; } __END__ loop I saw [f] loop I saw [r] loop I saw [e] I saw [] loop I saw [d] loop
    $_ = 'fred'; while( print "loop\n" and s/(.)// ) { print qq'I saw [$&]\n'; redo if $& eq 'e'; } __END__ loop I saw [f] loop I saw [r] loop I saw [e] I saw [] loop I saw [d] loop


    TGI says moo

Re^2: Does redo create a new dynamic scope?
by Anno (Deacon) on Jul 18, 2007 at 21:04 UTC
    Yes, but redo bypasses the conditional. There is no match and execution stays in the block. You shouldn't *have* to save $&.

    Anno

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-26 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found