Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: why is $1 cleared at end of an inline sub?

by LanX (Saint)
on Sep 16, 2021 at 12:42 UTC ( [id://11136829]=note: print w/replies, xml ) Need Help??


in reply to why is $1 cleared at end of an inline sub?

TL;DR all

next time please condense it to the relevant part!

> Seems a bit weird to have the end of a local sub clear '$1', yet that seems to be what is happening

yes, easily shown in a SSCCE

DB<3> sub bla { "XXX"=~/(X*)/; print "inside $1" } DB<4> bla; print "outside $1" inside XXXoutside DB<5>

> What was the logic of forcing/doing that?

I'd say it's about localizing the inner sub to protect all caller levels from effects at a distance, consider

DB<5> "YYY"=~/(Y*)/; bla; print "old $1" inside XXXold YYY DB<6>

otherwise nobody could rely on $1 etc anymore after calling a random sub.

Using a dedicated closure var holding the copied content of $1 is the way to go in your use case.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11136829]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-19 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found