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 reply to Re^2: why is $1 cleared at end of an inline sub?
by perl-diddler
in thread why is $1 cleared at end of an inline sub?
by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |