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

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

Replies are listed 'Best First'.
Re(6): Regex Capturing: Is this a bug or a feature?
by shotgunefx (Parson) on Sep 29, 2002 at 23:51 UTC
    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
        I think the confusion resulted from this statement.
        What's declared in that block? Something is declared for the block rather than in it.

        I have no problem remembering the behavoir now that I'm aware of it, my issue is that I've never read anything that would indicate it would work that way. Everything I've read says "Dynamic scoping" which is often used interchangably with local().
        Elian has a good explanation of what's going on under the magic carpet here

        -Lee

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