Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

capture vars undef

by msemtd (Scribe)
on Jul 27, 2004 at 14:04 UTC ( [id://377738]=perlquestion: print w/replies, xml ) Need Help??

msemtd has asked for the wisdom of the Perl Monks concerning the following question:

I don't understand why this results in $1 = $2 = $3 = undef...
$_ = "aaaabbbbcccc"; if ( not /^(\w\w)(\w\w)(\w\w)$/ ) { if ( not /^(\w\w)\w\w(\w\w)\w\w(\w\w)\w\w$/ ) { die "failed"; } } print "$_ = $1 $2 $3\n";
...but this gives me $1 = aa, $2 = bb, $3 = cc...
$_ = "aaaabbbbcccc"; if ( not /^(\w\w)\w\w(\w\w)\w\w(\w\w)\w\w$/ ) { die "failed"; } print "$_ = $1 $2 $3\n";
...as I would have expected from the first snippet. Am I just being thick?

Replies are listed 'Best First'.
Re: capture vars undef
by ccn (Vicar) on Jul 27, 2004 at 14:13 UTC

    because $1, $2, $3 ... are dynamically scoped to the current BLOCK.
    see perlvar

    $_ = "aaaabbbbcccc"; if ( not /^(\w\w)(\w\w)(\w\w)$/ ) { # inside a block if ( not /^(\w\w)\w\w(\w\w)\w\w(\w\w)\w\w$/ ) { die "failed"; } print "inside: $_ = $1 $2 $3\n"; } #outside a block print "outside: $_ = $1 $2 $3\n";
Re: capture vars undef
by deibyz (Hermit) on Jul 27, 2004 at 14:15 UTC
    From perldoc perlre:

    The scope of $<digit> (and $`, $&, and $') extends to the end of the enclosing BLOCK or eval string, or to the next successful pattern match, whichever comes first.

    So, in your case, I think inner $<digit> vars go out of scope, getting undef

Re: capture vars undef
by msemtd (Scribe) on Jul 27, 2004 at 14:21 UTC
    aha! so...
    $_ = "aaaabbbbcccc"; if ( not /^(\w\w)(\w\w)(\w\w)$/ and not /^(\w\w)\w\w(\w\w)\w\w(\w\w)\ +w\w$/ ) { die "failed"; } print "$_ = $1 $2 $3\n";
    ...will act as "expected"! Many thanks all.
      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.

        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 | http://chady.net/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://377738]
Approved by Corion
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-29 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found