local - How to localize a variable in an upper scope in Perl? -


I have run several times in the following pattern while during the development of the pell module which is AUTOLOAD or other Suburban Despatch Technologies:

  Sub Autoloid {my $ self = $ _ [0]; My $ code = $ self-> Data_out_code_f ($ atoloyed); Goto and $ code; }  

This works fine, and caller sees the correct scope.

Now what I want to do is execute locally and $ code during execution> $ _ with $ self Whatever will be:

  sub-autoload {my $ self = $ _ [0]; My $ code = $ self-> Data_out_code_f ($ atoloyed); Local * _ = \ $ self; # And now the question is how to call & amp; $ Code # Goto and $ code; # Wont work from local workspace changes # Goto # & amp; $ Will be unearned before the code; # Will protect the local, but the caller will report # additional stack frames  

solution that includes the caller wrapping, due to issues of performance and dependency Not acceptable. It seems that there is a rule to exclude the second option.

By going back for the first time, the only way to stop the new value of $ _ is to change either by going out of the scope during the goto Not a viable option) to localize or to implement any type of uplevel_local or goto_with_local I have played with all kinds of permutations related to padvocar , sub :: utplavel , scope :: upper , B :: Hooks: Andoffscope and others, but at the right time are not able to come up with a strong solution that cleans up $ _ , and do not wrap caller .

Did anyone find a pattern working in this situation?

(Such a question is related, but caller was not a requirement, and ultimately the answer was to use a different approach, so that the solution is not useful in this case) / P>

sub :: observance appears to be working - atoll for at least a simple case is not included Is:

  Use strict; Use warnings; Sub: Use celebration; $ _ = 1; times (); Subfoo {printf "% s% s% d -% s \ n", caller, $ _} sub time {my $ code = \ & amp; Foo; My $ x = 2; Local * _ = \ $ x; Uplevel 1, $ code; }  

Output is:

  main c: \ temp \ foo.pl 6 - 2  

given , It does not really localize one variable in the original variable, but I do not think that you really want to do this, even if you can. You only want to localize $ _ for the duration of the call.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -