in reply to Re: why is $1 cleared at end of an inline sub?
in thread why is $1 cleared at end of an inline sub?
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=∄
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: why is $1 cleared at end of an inline sub?
by haukex (Archbishop) on Sep 17, 2021 at 22:41 UTC | |
Re^3: why is $1 cleared at end of an inline sub? (semantics anonymous vs named subs)
by LanX (Saint) on Sep 17, 2021 at 21:34 UTC | |
Re^3: why is $1 cleared at end of an inline sub?
by choroba (Cardinal) on Sep 17, 2021 at 20:52 UTC | |
Re^3: why is $1 cleared at end of an inline sub?
by LanX (Saint) on Sep 17, 2021 at 23:08 UTC |