Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by krusty (Hermit)
on Sep 29, 2002 at 02:45 UTC ( #201515=note: print w/ replies, xml ) Need Help??


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?

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


Comment on Re: Re: Re: Re: Regex Capturing: Is this a bug or a feature?
Select or Download Code
Re: Re: Re: Re: Re: Regex Capturing: Is this a bug or a feature?
by Anonymous Monk on Sep 29, 2002 at 14:26 UTC
    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://201515]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-08-30 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls