After many hours of trying to trace down an anomoly in some test code, I ran into what appears to be a bug in regex caputring.
In
perlre it states
"The numbered variables ($1, $2, $3, etc.) and the related punctuation set ($+, $&, $`, $', and $^N) are all dynamically scoped until the end of the enclosing block or until the next successful match, whichever comes first. "
I've always taken this to mean they are "local" scoped to a block just as if you has said local($1), apparently this is not the case. Or at least not the case on my build of Perl v5.6.1
In the example below, after the first match, $1 and $2 are still populated every iteration after. Shouldn't they be reset?
#!/usr/bin/perl
# 5.6.1
use strict;
use warnings;
use vars qw ($test);
my @values = qw ( one var.1 test);
print join ("\t->\t",getsymbolval(@values) ),"\n";
#################################################
sub getsymbolval{
no strict qw (refs);
my @syms = @_;
foreach my $symbol (@syms){
local $test;
##############################
# Bug? #
##############################
$symbol=~m/(\w+)\.(\d+)/; # Shouldn't $1,$2... get reset ea
+ch time through?
print "symbol: $symbol\t\$1: $1\t\$2:$2\n";
print "test is ",$test++,"\n"; # Never incremented more tha
+n once
my ($ts,$te) = ($1,$2);
}
wantarray ? @syms : $syms[0];
}
-Lee
"To be civilized is to deny one's nature."
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.