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

I would like to save the information (username, password, database name, etc.) to connect to a database. I considering using the following method:

app.cfg
---------------------------

$username = "blah";
$password = "blinky";
$databaseName = "foo";

app.pl
----------------------------

#!/usr/bin/perl -w

use strict;
require 'app.cfg';

print "Username: $username\n";
When I do this I get a message like this:

Global symbol "$username" requires explicit package name at ./app.pl line 6.

Is this a good approach?
How can I best include this data in all of my app scripts?

If I remove the 'use strict;' then it works but that is not acceptable. I have to use strict (company policy).

-- rogueFalcon
Why do you people insist on doing things sdrawkcab?

Replies are listed 'Best First'.
Re: Config files
by maverick (Curate) on Dec 10, 2001 at 22:29 UTC
    I'd use Config::General for this. You'll be able to have more complex config files amoung other things.

    Most would consider this approach a Bad Idea (tm)....

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Re: Config files
by TomK32 (Monk) on Dec 10, 2001 at 22:40 UTC
Re: Config files
by Lucky (Scribe) on Dec 10, 2001 at 22:47 UTC
    You can make your own class to process config files. For plain config files it may looks like this:
    package PlainConfig; use strict; use Carp; use vars qw($AUTOLOAD); sub new{ my $self=shift; my $config=shift || croak "There is no path parameter specified"; my $new={}; open CONFIG, $config || croak "Couldn't open config file - $config"; while (<CONFIG>){ s/^\s*//; next if (/^\s*#/ or /^$/); /^([^=\s]+)\s*=\s*([^\n\r]*)/; $new->{$1}=$2; } close CONFIG; bless $new, $self; return $new; } sub AUTOLOAD{ my $self=shift; my $attr=$AUTOLOAD; $attr=~s/(.*::)+//; return $self->{$attr}; } 1;
    Then your script will be looks like this:
    use PlainConfig; my $config=new PlainConfig; print "Username: ".$config->username."\n";
    But there are many other approaches. Choose the best.
Re: Config files
by rob_au (Abbot) on Dec 11, 2001 at 05:28 UTC
    I would have to agree with the suggestions of others to look at existing CPAN modules such as Config::IniFiles and CPAN::General and would like to throw another into the mix - App::Config. This module makes it very easy to not only intergrate configuration files into applications, but also take into account command line arguments which may be passed to override the options in the configuration file.

    Additionally, the interface of this module is very easy to use - My only gripe with this interface however, is the requirement to define configuration parameters one line at a time (see the POD documentation for an example of this), an issue which I addressed with my own extension module - Local::Config - an extension to App::Config

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'