Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^2: capture vars undef

by fletcher_the_dog (Friar)
on Jul 27, 2004 at 17:03 UTC ( #377789=note: print w/replies, xml ) Need Help??

in reply to Re: capture vars undef
in thread capture vars undef

No this still won't work because $1,$2,$3 still fall out of scope because they are not in the 'if' block.
Update: This does work, but don't understand why since the vars have gone out of scope.

Replies are listed 'Best First'.
Re^3: capture vars undef
by Chady (Priest) on Jul 28, 2004 at 06:20 UTC

    They haven't gone out of scope. Here's an attempt at explaining that:

    In the first example, you have two if blocks, the first if block provides a new scope for the second block, so what you have is something like this:

    # provided by the outermost if $1 = undef; $2 = undef; $3 = undef; { # provided by the inner if $1 = 'aa'; $2 = 'bb'; $3 = 'cc'; }

    Note that one if block will not kill the $<digit> vars, but once the inner block finishes, then its variables are destroyed, and you get the outer block's $<digit>s

    Here's an example that might explain it better:

    $_ = "aabbccdd"; if ( /^(\w\w)\w\w(\w\w)\w\w$/ ) { if ( /^\w\w(\w\w)\w\w(\w\w)$/ ) { print "inner: $1 $2\n"; } print "mid: $1 $2\n"; # the inner if's vars are still accessible h +ere } print "outer: $1 $2\n";

    This will print aa cc and not your expected bb dd in the outer block.

    Update: here's another way to think of it:

    $_ = "aabbccdd"; /^(\w\w)\w\w(\w\w)\w\w$/; { /^\w\w(\w\w)\w\w(\w\w)$/; { print "inner: $1 $2\n"; } print "mid: $1 $2\n"; } print "outer: $1 $2\n";

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady |

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-08-14 09:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found