We don't bite newbies here... much | |
PerlMonks |
Re^2: why is $1 cleared at end of an inline sub?by perl-diddler (Chaplain) |
on Sep 17, 2021 at 20:27 UTC ( [id://11136849]=note: print w/replies, xml ) | Need Help?? |
Both of these answers ignored that the sub was an 'anonymous'/'inline' sub that would have access to surrounding local variables in the same scope, including regex vars.
I find it surprising that '$1' is affected in this way by an anonymous sub. I wouldn't find it surprising that a normal sub would auto-save context to not completely disrupt callers (even though '$_' needs to be explicitly saved with local). To re-ask, why is an inline-sub which I thought was designed to have access to local vars (in same context) restoring '$1'. If it was accessing or changing '$_1', it would access the copy of the sub it was in. I had supposed that the '$1' would stay constant until another regex and that an inline/anon sub wouldn't treat '$1' differently from '$_1'. I was really more wondering what the rational might be for treating them differently in an anon/inline sub. In the same way I find that '$1', and '$2' are cleared coming out of a 'do' block to be strange -- I would have thought only another regex would change them. my $s="abcdefg"; $_=$s; my @res=do { m{abc(de)(fg)}; }; P "nres=%s 1=%s, 2=%s", 0+@res, $1, $2; ' nres=2 1=∄, 2=∄
In Section
Seekers of Perl Wisdom
|
|