http://qs321.pair.com?node_id=11136826

perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:

Have a quick question, the answer for which seems to be obvious, but just wanting to check and maybe hope I'm wrong.

#!/usr/bin/perl use strict; use warnings; use P; my $intxt; $intxt = << 'TXT' ; <package type="rpm"> <name>7kaa-music</name> <url>http://7kfans.com/</url> TXT ; sub REindex($$;$) { #like 'index', except substr is RE my ($str,$ss)=(shift, shift); my $p = @_ ? shift:0; $str =~ m{^.{$p,$p}($ss+)} ? length $1 : -1; } my @lines=split "\n", $intxt; my $ln; my $lineno=0; sub getln() { return $lineno<@lines ? $lines[$lineno++] : undef; } my $ttag; sub getnxt_tagln(); local * getnxt_tagln; *getnxt_tagln = sub () { do { $_=getln(); defined $_ or return undef; } until m{^\s*<(/?\w+)}; $ttag=$1; }; my $tag; NXTPKG: while (getnxt_tagln()) { # why '$1' null? $ln = $_; $tag = $1; Pe "_=%s, ttag=%s, tag=%s", $_, $ttag, $tag; } # vim: ts=2 sw=2 ai number
My question concerns the comment after the NXTPKG line: why '$1' null (∄)?

When I run this:

_=<package type="rpm">, ttag=package, tag=∄;
_=  <name>7kaa-music</name>, ttag=name, tag=∄;
_=  <url>http://7kfans.com/</url>, ttag=url, tag=∄;

tag is null/undef when I get out of my inline-sub. I can get around it by assigning $1 to $ttag, but I don't have any other Regex's that should be clearing '$1'. Seems a bit weird to have the end of a local sub clear '$1', yet that seems to be what is happening. Why? What was the logic of forcing/doing that?

tnx!