Why use Perl::Critic if you don't want to follow its advice?

Using require to load a configuration file is a bad idea. For one thing it will silently fail to load the file into more than one package. From the docs for require:

Otherwise, require demands that a library file be included if it hasn't already been included. ... Note that the file will not be included twice under the same specified name.

$ cat conf.pl #!/usr/bin/perl use strict; use warnings; our %conf = ( foo => 'bar', baz => 'quux' ); __END__
$ cat ONE.pm package ONE; use strict; use warnings; sub one { our %conf; require 'conf.pl'; return $conf{'foo'}; } 1;
$ cat TWO.pm package TWO; use strict; use warnings; sub two { our %conf; require 'conf.pl'; return $conf{'baz'}; } 1;
$ cat 1137798.pl #!/usr/bin/perl -w use strict; use ONE; use TWO; print ONE::one, "\n"; print TWO::two, "\n"; __END__

Output:

$ perl 1137798.pl bar Use of uninitialized value in print at 1137798.pl line 8.

Change the require calls to do as somebody else noted and you can then import your config file into more than one package, which is useful.

$ cat ONE.pm package ONE; use strict; use warnings; sub one { our %conf; #require 'conf.pl'; do 'conf.pl'; return $conf{'foo'}; } 1;
$ cat TWO.pm package TWO; use strict; use warnings; sub two { our %conf; #require 'conf.pl'; do 'conf.pl'; return $conf{'baz'}; } 1;
$ perl 1137798.pl bar quux

So use do, or better yet, just use Config::Tiny, which exists to solve your problem.

$ cat conf.ini [path] foo=bar some=other [url] baz=quux less=more
$ cat 1137798-2.pl #!/usr/bin/perl -w use strict; use Config::Tiny; my $conf = Config::Tiny->read( 'conf.ini' ); print $conf->{path}->{foo}, "\n"; print $conf->{url}->{baz}, "\n"; __END__

Output:

$ perl 1137798-2.pl bar quux

Now what's overkill about that?

The way forward always starts with a minimal test.

In reply to Re: What's the right way to include a config file as a .pm bareword? by 1nickt
in thread What's the right way to include a config file as a .pm bareword? by Cody Fendant

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.