ISAI student has asked for the wisdom of the Perl Monks concerning the following question:
Hello all. I am doing reshape of an old, 16,000 lines of 1 file PERL script, riddled with global variables, to a more modular, use strict compatible packages files. It even less fun than it sounds. I have come to the situation where I try to auto-convert the global variables to local ones. As some of these variables are hash of hash of hashes, this thing can be very long, and hard to automate.
What that I have come to think that instead of rewriting it from scratch, is to have all of the previous global variables defined as "my" in the main loop, have a hash that would collect them, then pass a reference to the hash to thes subroutines in the file. I.E. In the main loop, there is a global variable, $foo. Now I can converting it to a hash passable, my variable
Then, in the sub sub1:my $foo; my %hash; $hash{'$foo'}=\$foo; sub1 (\%hash);
What that I would like to have is a subroutine that can generate all these my variables (there are well over 50), w/o resorting to copy paste it all the time. I.E.sub sub1{ my $ref_from_global = shift; my $foo=${$$ref_from_gloabl{'$$foo'}}; }
sub sub1 { my $dat_ref = shift; my $code_ref = shift; eval {&\code_ref); }
When the line eval {&\code_ref);, is eqauivalent to the line above, and getting variables generated within the same scope. Is that possible?
|
|---|