in reply to Re: Re: Re: Regex Capturing: Is this a bug or a feature?
in thread Regex Capturing: Is this a bug or a feature?

"Effectively that's the same thing as the end of the imaginary block. "

To be honest, I am confused by this. Doesn't the foreach block constitute it's own scope? I would interpret it like so...
{ # Your loop: foreach my $symbol (@syms){ local ($1, $2) = ($1, $2); # OK, so you can't actually local $test; $symbol=~m/(\w+)\.(\d+)/; print "symbol: $symbol\t\$1: $1\t\$2:$2\n"; print "test is ",$test++,"\n"; my ($ts,$te) = ($1,$2); } }
which is what I'd expect from local() scoping. I understand it's the regex engine so I don't have a problem with it not working the same way. I do think the documentation is very confusing and reminds me of my $x if 0 "feature"

-Lee

"To be civilized is to deny one's nature."

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Regex Capturing: Is this a bug or a feature?
by Anonymous Monk on Sep 29, 2002 at 14:17 UTC
    Sigh. Yes, the foreach block constitues its own scope. I'm trying to write code that simulates what Perl does for you. Let me repeat what I said earlier. There's nothing declared in the block. Something is declared for the block. And the way to do that manually is by adding an outer block and do something before you enter the other block. Otherwise you'd do the thing on each iteration of the block, and that's not what you want.

    You have to let go of the idea that perl puts a local() in your block.

    -Anomo
      If local() doesn't work that way, why does this example localize every time through the block?

      When in doubt, parse it out. ;)
      #!/usr/bin/perl use strict; use warnings; use vars qw ($PackageVar); use B::Deparse; my $deparse = B::Deparse->new("-p", "-sC"); my $body = $deparse->coderef2text(\&ltest); print $body; ########################## sub ltest { $PackageVar = 0; for(1..10){ local $PackageVar = 0; print ++$PackageVar,"\n"; } } __END__ # Says B:Deparse { ($PackageVar = 0); foreach $_ (1 .. 10) { (local $PackageVar = 0); print((++$PackageVar), "\n"); } }


      -Lee

      "To be civilized is to deny one's nature."
        If local() doesn't work that way, why does this example localize every time through the block?

        What? When did I start disagreeing on how local() works? I even said "Otherwise you'd do the thing [= local()ization] on each iteration of the block". The whole point of doing the local()ization outside the block is to circumvent the relocal()ization -- because you don't want the relocal()ization. With the relocal()ization it wouldn't be equivalent to what perl does with the regex-variables. Again, I'm trying to manually do what Perl does for you.

        There seems to be a lot of confusion here. It all started with me stating how I'd interpret how perl works in pure-Perl. That was because if you know Perl you also understand the behaviour you can deduce from the code. So if you saw a "picture" of how it works it could help you (and other reading this) to remember the behaviour.

        I know that the regex-variables are highly magical and I don't really understand how they work internally myself. My interpretation comes from the documentation, and I can't recall me having any other interpretation of it. I've always pictured me the regex-variables working something like what I wrote above, and it seems like it works alright. I haven't found any occation where my picture gives the wrong results -- but I haven't look so I don't claim it to be perfect.

        -Anomo