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

It isn't really like "local" scoping, because they inherit inward into inner blocks. Although I couldn't have predicted that they'd retain the same value each time, you have triggered one of my pet peeves: namely using $1 without testing whether or not the match succeeds. Please write your code so that you avoid $1 unless you are sure of a match, and problems like this will go away.

As for the "localization", it's more like "scope-limited copy-on-write". If you're in an inner scope and you change $1, the effect doesn't propogate to an outer scope. However, if you merely access it, you get the inherited outer value.

-- Randal L. Schwartz, Perl hacker

  • Comment on •Re: Regex Capturing: Is this a bug or a feature?

Replies are listed 'Best First'.
Re: •Re: Regex Capturing: Is this a bug or a feature?
by shotgunefx (Parson) on Sep 28, 2002 at 21:08 UTC
    Avoiding the problem is fine now that I know about it.
    Normally I do say if ( m/(whatever)/ ){ assign} which is why I never noticed this before. But I've never heard anyone or read anything that equates dynamic scoping with anything other than local() in perl and I've seen the terms local and dynamic used interchangeably.

    In my example (or at least the code it was reduced from) I did a match, saved the value and checked later for a defined value. If this was a "regular" dynamic scoped variable than I would be testing if it passed or failed, just later on.

    I own and have read all the ORA Perl books (with the exception of Mastering Regular Expressions) and I don't recall it every being mentioned. I think it should be clarified in perlre is all.

    -Lee

    "To be civilized is to deny one's nature."
      It has bitten me too, and I bet some other people too. I know about it know, and it is ok, but what really lacks is documentation about it.

      I seem to recall that it triggers some awkvardness in solutions too, at times, as just because a regexp matches, it may not actually fill all of the $n variables, and old values may still be there, and that makes checking harder. I would have to get back on that with a real example though, in case it is my memory that fools me. :)


      You have moved into a dark place.
      It is pitch black. You are likely to be eaten by a grue.
        I believe you're correct. I tried to verify my recollection and came up with something even stranger.
        #!/usr/bin/perl # 5.6.1 use strict; use warnings; my @values = qw ( one var.1 test); getsymbolval(@values); ################################################# sub getsymbolval{ my @syms = @_; foreach my $symbol (@syms){ $symbol=~m/(\w+)(\.)?(\d+)/; print "symbol: $symbol\t\$1: $1\t\$2:$2\n"; my ($ts,$te) = ($1,$2); } wantarray ? @syms : $syms[0]; } __END__

        Output follows (minus uninitilized warnings): What's up with just 'v' being in the regex?
        symbol: one $1: $2:
        symbol: var.1 $1: var $2:.
        symbol: test $1: v $2:
        Very odd.

        -Lee

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