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

Hi,

Newish Perl user trying to use do $filename; to read configuration data from a file.

Configuration file consists of:

# set up the database connection variables $db_username = ''; $db_password = 'somepassword'; $db_host = 'localhost'; $db_database = 'adb'; $db_table = 'notactual';

The script consists, so far, of the following code:

#!/usr/bin/perl -w use strict; use diagnostics; my $preprocessdir; my $db_username; my $db_password; my $db_host; my $db_database; my $db_table; my $db_tracelog; my $conf_file; my $return; $conf_file = "/home/kuser/dbconnect.inc.pl"; unless ($return = do $conf_file) { warn "couldn't parse $conf_file: $@" if $@; warn "couldn't do $conf_file: $!" unless defined $return; warn "couldn't run $conf_file" unless $return; } print $db_username; print $db_password; print $db_host; print $db_database; print $db_table;

When run it gives the an error for each of the print lines

Use of uninitialized value in print at ocrgqx_audit_report.pl line 30 +(#1) (W uninitialized) An undefined value was used as if it were alread +y defined. It was interpreted as a "" or a 0, but maybe it was a mi +stake. To suppress this warning assign a defined value to your variables.

I am trying to set variables within the script using a configuration file

So I guess I've got some syntax wrong but I can't see what. Follow up query: am I using do appropriately for configuration files - I'm aware of use Safe and will when this first level of syntax is sorted - may lead to a further question ;-)

Thank you in advance

Steve

Replies are listed 'Best First'.
Re: variables not being set from a configuration file
by toolic (Bishop) on Jun 12, 2009 at 16:07 UTC
    As an alternative approach, if you want to avoid that voodo, you could read your db data into a hash using open. This would also replace all your separate (but related) scalar variables with a single hash variable.
    $ cat dbconnect.txt # set up the database connection variables username = foo password = somepassword host = localhost database = adb table = notactual $ $ cat ./770979.pl #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my %db_data; my $file = 'dbconnect.txt'; open my $fh, '<', $file or die "Can not open file $file: $!"; while (<$fh>) { chomp; next if /\s*#/; # Ignore comments if (/=/) { my ($k, $v) = split /\s*=\s*/; $db_data{$k} = $v; } } close $fh; print Dumper(\%db_data); $ ./770979.pl $VAR1 = { 'database' => 'adb', 'password' => 'somepassword', 'table' => 'notactual', 'host' => 'localhost', 'username' => 'foo' };

    Yet another alternative approach is to use one of the CPAN modules, such as Config::Simple.

Re: variables not being set from a configuration file
by rovf (Priest) on Jun 12, 2009 at 14:54 UTC

    The variables in your config file, by the power of do, will end up in package main (i.e. they are global), and hence different from the lexicals you declared. For example (from the wonderful Windows command line):

    C:\tmp>echo $xx=5 >inc.pl C:\tmp>perl -lwe "do 'inc.pl'; print $main::xx" Name "main::xx" used only once: possible typo at -e line 1. 5
    As you can see, the $xx=5 sets $main::xx to 5.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: variables not being set from a configuration file
by perreal (Monk) on Jun 12, 2009 at 14:59 UTC
    if you change 'my' decelerations to 'our' declarations it then works as expected.
Re: variables not being set from a configuration file
by Bloodnok (Vicar) on Jun 12, 2009 at 15:02 UTC
    As rovf points out, you declare lexical, but end up setting, global variables i.e. instead of my $var; ...., try declaring the variables with either our $var;.. or even use vars qw/$var .../;

    A user level that continues to overstate my experience :-))
Re: variables not being set from a configuration file
by afoken (Chancellor) on Jun 12, 2009 at 20:22 UTC

    See also Re^2: conf file in Perl syntax

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: variables not being set from a configuration file
by stevemayes (Scribe) on Jun 12, 2009 at 21:41 UTC
    Thank you all. Torn between an embarrassed "doh!" at a beginner's error on scope and 'wow, I guess there really is more that one way to do it." at the variety of suggested techniques.