perl - How do I localise variables in another package when I only know their names at runtime? -


I need to move some variables in another package, but I do not know how long their name is passed Already happened. My efforts were not made to use local with the typoglob, so I saved the value of the variable and then fell back to manually reinstalling. Is there a better way? Note, checking the error to see if one exists before roaming with a variable, it is getting rid of clarity.

  #! / Usr / bin / perl strict; Use warnings; My% original; For my $ name (qw / foo times / falcon) {my $ var = \ $ {meta :: {$ name}}; $ Origin {$ name} = $$ var; $$ var = $$ var * 2; } Meta :: P (); {My $ var = \ $ {Meta :: {$ name}} for my $ name (keys% origin); $$ var = $ orig {$ name}; } Meta :: P (); Package meta; BEGIN {our $ foo = 1; Our $ bar = 2; Our $ Falcon = 3; } Sub P {} included in the print ("::", $ meta :: foo, $ meta :: bar, $ meta :: baz), "\ n"}  

To avoid an eval like this:

  my $ eval = ''; For my $ name (qw / foo bar baz /) {$ eval. = "Local \ $ meta :: $ name = \ $ meta :: $ name * 2; \ n"; } Eval "$ eval meta :: p ()"; Meta :: P ();  

What is eval trying to avoid wastage of time? Is the new code worse than eval?

Note, I also do not want to use symbolic references, all code should work under hard . The current solution works, so I guess what I'm doing is not looking for hacks around, I'm looking for a better solution (if someone is present).

closing white rifers should allow you to do whatever you want Are there. I do not have access to the system to do a test with ATM, but something like this should be done:

  Use strict; Use warnings; {No hard; refs`; Local $ {"Meta :: $ name"} = $ {"Meta :: $ name"} * 2; Meta :: P (); } Meta :: P ();  

Update:

After some testing, I got a snag.

It is easy to use local on a single symbolic reference but it is not so easy to alter variable names - loop creation (for the map, etc.) all one Make small, scope, they work, those who end up localization.

  # This works but does not work on an array of names. {No strict 'refs'; Local ($ {'Meta :: Foo'}, $ {'Meta :: Bar'}, $ {'Meta :: Falcon'}); Meta :: P (); } Meta :: P (); # It does not work at all! {No strict 'refs'; My @to_localize = Map "Meta :: $ _", qw / foo bar /; Local $ {$ _} = $$ _ * 2 @to_localize; Meta :: P (); } Meta :: P ();  

The only solution that uses me goto , which we all know is considered harmful .

  {No hard 'refs'; My @to_localize = Map "Meta :: $ _", qw / foo bar /; LOCALIZER: My $ localize_me = shift @ to_localize; Local $ {$ localize_me} = $$ local_2 * 2; Goto loclizer if @to_ localize; Meta :: P (); } Meta :: P ();  

I am open to better ideas.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -