FAQ

[Peter@PSDT.com - Sat Apr 19 08:32:56 2003]:

I was raised on the belief that "an unsuccessful regex match leaves $1
and
friends at their previous values." So I was surprised to discover an
exception to that rule. It's been around for a long time, so perhaps
I'm
missing something. OTOH, if this is not expected behavior, perhaps
there's
a bug.

% cat foo
#!/usr/bin/perl
for (qw(843 foo 23skidoo bar 345)) {
print /^(\d+)$/ ? "$_ matched" : "$_ didn't match";
print "; \$1 = $1\n";
}

% ./foo
843 matched; $1 = 843
foo didn't match; $1 = 843
23skidoo didn't match; $1 = 8
bar didn't match; $1 = 8
345 matched; $1 = 345

In the case of the unsuccessful match against 23skidoo, $1 is
truncated to
the first character of its previous value. This is in 5.6.1, 5.8.0,
and 5.9.0.

Just as a historical point of reference: In 5.004_04, $1 is set to the
first character of 23skidoo, and is undef on an unsuccessful match:

843 matched; $1 = 843
foo didn't match; $1 =
23skidoo didn't match; $1 = 2
bar didn't match; $1 =
345 matched; $1 = 345
From perlre.pod
"...failed matches in Perl do not reset the match variables."

so the above is a bug. My guess, and this is just a guess, is that on
the failed match, SvCUR_set() is getting called somewhere. Looking at
the results with Devel::Peek...

843 matched; $1 = 843
SV = PVMG(0x801ae3c0) at 0x8812a6f4
REFCNT = 1
FLAGS = (GMG,SMG,READONLY,pPOK)
IV = 0
NV = 0
PV = 0x85bf0300 "843"\0
CUR = 3
LEN = 4
MAGIC = 0x7df8efa0
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = PERL_MAGIC_sv(\0)
MG_OBJ = 0x8812a6e0
MG_LEN = 1
MG_PTR = 0x7c1a19c0 "1"

foo didn't match; $1 = 843
SV = PVMG(0x801ae3c0) at 0x8812a6f4
REFCNT = 1
FLAGS = (GMG,SMG,READONLY,pPOK)
IV = 0
NV = 0
PV = 0x85bf0300 "843"\0
CUR = 3
LEN = 4
MAGIC = 0x7df8efa0
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = PERL_MAGIC_sv(\0)
MG_OBJ = 0x8812a6e0
MG_LEN = 1
MG_PTR = 0x7c1a19c0 "1"

23skidoo didn't match; $1 = 8
SV = PVMG(0x801ae3c0) at 0x8812a6f4
REFCNT = 1
FLAGS = (GMG,SMG,READONLY,pPOK)
IV = 0
NV = 0
PV = 0x85bf0300 "8"\0
CUR = 1
LEN = 4
MAGIC = 0x7df8efa0
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = PERL_MAGIC_sv(\0)
MG_OBJ = 0x8812a6e0
MG_LEN = 1
MG_PTR = 0x7c1a19c0 "1"

bar didn't match; $1 = 8
SV = PVMG(0x801ae3c0) at 0x8812a6f4
REFCNT = 1
FLAGS = (GMG,SMG,READONLY,pPOK)
IV = 0
NV = 0
PV = 0x85bf0300 "8"\0
CUR = 1
LEN = 4
MAGIC = 0x7df8efa0
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = PERL_MAGIC_sv(\0)
MG_OBJ = 0x8812a6e0
MG_LEN = 1
MG_PTR = 0x7c1a19c0 "1"

345 matched; $1 = 345
SV = PVMG(0x801ae3c0) at 0x8812a6f4
REFCNT = 1
FLAGS = (GMG,SMG,READONLY,pPOK)
IV = 0
NV = 0
PV = 0x85bf0300 "345"\0
CUR = 3
LEN = 4
MAGIC = 0x7df8efa0
MG_VIRTUAL = &PL_vtbl_sv
MG_TYPE = PERL_MAGIC_sv(\0)
MG_OBJ = 0x8812a6e0
MG_LEN = 1
MG_PTR = 0x7c1a19c0 "1"

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl5-porters @
categoriesperl
postedJul 12, '05 at 10:18p
activeJul 12, '05 at 10:18p
posts1
users1
websiteperl.org

1 user in discussion

Steve Peters via RT: 1 post

People

Translate

site design / logo © 2021 Grokbase