perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:
My question concerns the comment after the NXTPKG line: why '$1' null (∄)?#!/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
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: why is $1 cleared at end of an inline sub?
by haukex (Archbishop) on Sep 16, 2021 at 12:38 UTC | |
by perl-diddler (Chaplain) on Sep 17, 2021 at 20:27 UTC | |
by haukex (Archbishop) on Sep 17, 2021 at 22:41 UTC | |
by LanX (Saint) on Sep 17, 2021 at 21:34 UTC | |
by choroba (Cardinal) on Sep 17, 2021 at 20:52 UTC | |
by LanX (Saint) on Sep 17, 2021 at 23:08 UTC | |
|
Re: why is $1 cleared at end of an inline sub?
by LanX (Saint) on Sep 16, 2021 at 12:42 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |