|
|
| Welcome to the Monastery | |
| PerlMonks |
Re: 'local' for imported variables doesn't work as expected (4 updates)by choroba (Cardinal) |
| on Oct 04, 2024 at 09:57 UTC ( [id://11162050]=note: print w/replies, xml ) | Need Help?? |
|
Exporter tells you what not to export: > Exporting variables is not a good idea. They can change under the hood, provoking horrible effects at-a-distance that are too hard to track and to fix. Trust me: they are not worth it. The right way is to export the accessors to the variable rather than the variable itself. This makes local useless, so we have to find a different way. For example, we can use Scope::Guard: and Note that if you don't store the result of set_area to a variable, the module will complain:
Aside from doing it the right way, you can create the alias to the package variable yourself:
or
which can be done by extending Exporter's import:
Interestingly, using something other than a scalar works the way you wanted. and
Update: The reasons for this are unclear to me (maybe local for hash values was implemented later than local for variables, so there are probably some differences in the implementation). Update 2: Extending the import method added. Update 3: In fact, what exporter does is quite similar: and if you try the same instead in the redefined import method, it doesn't work either. There's something more than the scalar slot that needs to be aliased, not sure why. The following works, too:
Update 4: In fact, adding the following line after Exporter::Heavy's block seems to fix the behaviour of Exporter without breaking anything I have tried:
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||||||||||||