Hi Monks,

First post so I apologize if I miss something or if there are small errors in the example I provided, I am just using it to explain my issue. I have run into this situation a few times now and have not been able to track down an answer. Let me be clear that this is for a work application so I cannot simply change the current setup of files etc...

Background: We use multiple .conf files to house "parsers" for data files. Each of these .conf files has a sub that contains a header map in the form of an array. These .conf files are not packages.

Question: I would like to write a program that will get that header map from one of these .conf files then get it from a different one as well but only for the purposes of a very simple operation. My current method is to use a require (as seen below) and grab the sub. The problem is when I use require a second time to get the second .conf file it completely overwrites the first require. Can I call this second one and have the scope of it limited to a particular subroutine or variable?

Example:

if (-e "myfilepath/test1.conf") { require "myfilepath/test1.conf" or die("myfilepath/test1.conf - $! +\n"); } else { die("Missing provider config file: myfilepath/test1.conf\n"); } $header_map = &get_config; # grab a file to parse while (defined(my $line = <FILE>)) { my $second_file = &compare(); # split $line into the header if ($first_file{test} =~ /$seconf_file{test}/i) { # etc.... } } sub compare { if (-e "myfilepath/test2.conf") { require "myfilepath/test2.conf" or die("myfilepath/test2.conf +- $!\n"); } else { die("Missing provider config file: myfilepath/test2.conf\n"); } $header_map_2 = &get_config; # grab a file to parse while (defined(my $line = <FILE>)) { # split $line into the header } return "$parsed_line"; }

Thanks!


In reply to Can you limit the scope of require? by taylorK

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.