taylorK has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: Can you limit the scope of require?
by dave_the_m (Monsignor) on Jul 20, 2018 at 20:20 UTC
    Difficult to comment without knowing what one of your .conf files looks like. One possibility is to use 'do' rather than 'require', and import each one into a different namespace. For example:
    /tmp/foo.conf: sub f { print __PACKAGE__, "\n" } 1; main script: package P1; do '/tmp/foo.conf'; package P2; do '/tmp/foo.conf'; P1::f(); P2::f(); which outputs: P1 P2
    Dave.
Re: Can you limit the scope of require?
by haukex (Archbishop) on Jul 20, 2018 at 22:12 UTC

    I am assuming that you can't modify the .conf files? If you can, then IMO the best solution (better than the following) would be to have them return or set references to anonymous subs rather than use named subs.

    Otherwise, tybalt89 and dave_the_m have provided two possible solutions that I think are good. Since TIMTOWTDI, here's one more - this one stores references to the sub get_config in lexical variables, which has the advantage that they are normal variables with the same scoping rules, you can pass them around, etc.

    I'm using do (with the recommended error handling) instead of require because the latter normally won't load a file more than once (see require and %INC). That means that if you were to use my load_config more than once on the same file, with require there is a chance that you could get a reference to the wrong sub, or it just plain wouldn't work. do executes the file every time, which avoids this.

    Foo.conf:

    use warnings; use strict; sub get_config { return "Foo!" } 1;

    Bar.conf:

    use warnings; use strict; sub get_config { return "Bar!" } 1;

    main.pl:

    use warnings; use strict; sub load_config { my $file = shift; local *get_config; if (not my $return = do $file) { die "couldn't parse $file: $@" if $@; die "couldn't do $file: $!" unless defined $return; die "couldn't run $file" unless $return; } return \&get_config; } my $foo = load_config( "Foo.conf" ); my $bar = load_config( "Bar.conf" ); print $foo->(), "\n"; # prints "Foo!" print $bar->(), "\n"; # prints "Bar!"
Re: Can you limit the scope of require?
by tybalt89 (Monsignor) on Jul 20, 2018 at 19:42 UTC

    I don't understand your problem, so here's a WAG.

    # require.one sub foo { print "one\n" } 1;
    # require.two sub foo { print "two\n" } 1;
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1218946 use strict; use warnings; require './require.one'; foo(); { # lexical block local *foo; require './require.two'; foo(); } # end lexical block foo();

    "local" is your friend :)