Hi Perlers,

I'm trying to implement one of the recipes I found in the Perl
Cookbook.  It is "8.16. Reading Configuration Files" recipe.  Here are
some snippets from that text:

"  ... Or better yet, treat the config file as full Perl code:

do "$ENV{HOME}/.progrc";
...
The second solution uses do to pull in raw Perl code directly. When
used with an expression instead of a block, do interprets the
expression as a filename. This is nearly identical to using require,
but without risk of taking a fatal exception.
...
You might wonder what context those files will be executed under. They
will be in the same package that do itself was compiled into.
Typically you'll direct users to set particular variables, which,
being unqualified globals, will end up in the current package. If
you'd prefer unqualified variables go into a particular package, do
this:

{ package Settings; do "$ENV{HOME}/.myprogrc" }

As with a file read in using require or use, those read in using do
count as a separate and unrelated lexical scope. That means the
configuration file can't access its caller's lexical (my) variables,
nor can the caller find any such variables that might have been set in
the file. It also means that the user's code isn't held accountable to
a pragma like use strict or use integer that may be in effect in the
caller."


My code looks like this (for testing):

  #!/usr/bin/perl
  # configtest.pl

  use warnings;
  use strict;

  { package Config; do "configtest.conf" }

  print "$_\n" for( @Config::FILE_NAME );

My configtest.conf file looks like this:
  
  # A list of file names
  @FILE_NAME = qw[
    /This/is/a/test
    /This/is/also/a/test
    /And/this/is/the/last/test
  ];

Now, this code runs, and produces the expected output.  However, it
also gives me a warning:
  Name "Config::FILE_NAME" used only once: possible typo at
./configtest.pl line 7.

I realize I can just turn my pragmas off after testing/implementation
to get rid of this, but is there a better way?  Perhaps my Perl
Cookbook is just old (yup, 1st edition.  Has this recipe been
updated?)

--Errin

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to