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

my $foo; my %hash; $hash{'$foo'}=\$foo; sub1 (\%hash);
Then, in the sub sub1:
sub sub1{ my $ref_from_global = shift; my $foo=${$$ref_from_gloabl{'$$foo'}}; }
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 $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?


In reply to using eval or equivalent to automatically generate my statements by ISAI student

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.