Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: Re: Regex Capturing: Is this a bug or a feature?

by Anonymous Monk
on Sep 29, 2002 at 00:57 UTC ( #201505=note: print w/ replies, xml ) Need Help??


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

Why would you interperate it being scoped to the outer block?

I'd like to change "the outher block" to "an outer (imaginary) block". The outer block I used above isn't written by the coder.

It's declared in the foreach's block

What's declared in that block? Something is declared for the block rather than in it.

I interperate that as the foreach's block as that is the enclosing block in this example.

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

I thought it would be clear that everything but your code was "added" by Perl, especially since I wrote my own $DIGIT localizer.

Hope this makes it more clear what I meant.

Cheers,
-Anomo


Comment on Re: Re: Re: Regex Capturing: Is this a bug or a feature?
Download Code
Re: Re: Re: Re: Regex Capturing: Is this a bug or a feature?
by shotgunefx (Parson) on Sep 29, 2002 at 01:42 UTC
    "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."
      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."
Re: Re: Re: Re: Regex Capturing: Is this a bug or a feature?
by krusty (Hermit) on Sep 29, 2002 at 02:45 UTC
    I'd like to change "the outher block" to "an outer (imaginary) block". The outer block I used above isn't written by the coder.

    Seems a funky way to state it, but what I think you're getting at is that if a local variable isn't declared within any existing block, then it will treat the current file as a block.

    While it is true that the compilation unit itself (as in file or eval statement) can be the scope of a my'd (lexical) or local (dynamic) variable, that's not what's going on here.

    What's declared in that block? Something is declared for the block rather than in it.

    The first opening curly brace to the closing curly brace of a "for" loop is a block. Same thing with "foreach"... (for/foreach are synonyms)
    foreach $element @array {#code}
    Perfectly valid block...

    Finally, what's wrong with this: local ($1, $2) = ($1, $2); # OK, so you can't actually write this.
    Try it... It works.
    What you can't do is this:
    $1 = 50;
    With warnings turned on you'll get something along the lines of a "modification of read-only value at line x". Update: Forget the warnings thing... $1 = 50; is a flat out compilation error
      what I think you're getting at is that if a local variable isn't declared within any existing block, then it will treat the current file as a block.

      That's not my point at all. Reread what I've written and see my other replies in this thread.

      The first opening curly brace to the closing curly brace of a "for" loop is a block.

      Yes, so you have to put the stuff that should be declared for it outside it, otherwise it will be declared in it, and that would be wrong, because what wouldn't declare something for the whole block.

      As for the local ($1, $2) = ($1, $2); issue: Yes, is "works", but it doesn't do what you want. Try this: 'a' =~ /(.)/; local $1 = 'b'; print $1. It prints 'a' and not 'b'. Experiment some with Devel::Peek and you'll see why doing "local $1 = $1" doesn't do what you want.

      -Anomo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2014-09-20 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls