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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.