-----Original Message-----
From: Gavin Henry [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, November 03, 2004 4:15 AM
To: Perl Beginners List
Subject: Re: Sourcing Configuration files

Chris Devers said:
>> On Tue, 2 Nov 2004, Gavin Henry wrote:
>>
>>> What is the easiest way to move variable declarations out into a
file
>>> in /etc/ and requiring a perl program to read them in at startup. If
>>> they are not there, then the program must complain.
>>
>> Have you considered using Tie::File, FreezeThaw or Data::Dumper?
>>
>>     <http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm>
>>     <http://search.cpan.org/~ilyaz/FreezeThaw-0.43/FreezeThaw.pm>
>>
<http://search.cpan.org/~jhi/perl-5.8.0/ext/Data/Dumper/Dumper.pm>

> Thanks I will try them. I think it's a bit weird I can't do this out
of
> the box. Does anyone else?


Hi Gavin,

Out-of-the-box configuration files, as you say, can be done as the
following simple code demonstrates:

--configtest.pl--
-----------------
#!/usr/bin/perl
use warnings;
use strict;

get_config();

print "FOO_VALUE = $Config::FOO_VALUE\n";
print "BAR_VALUE = $Config::BAR_VALUE\n";

sub get_config{
        package Config;
        our $FOO_VALUE;
        our $BAR_VALUE;
        do '/etc/my_values.conf'
}


--/etc/my_values.conf--
-----------------------
$FOO_VALUE = "This is the value of foo";
$BAR_VALUE = "This is the value of bar";


I hope that helps.  That 'do' statement is the key.  If you tell perl to
'do' a filename, it will parse it, at run-time, just like any other
code.  I use that sort of thing all the time.  It is not necessary to
declare a package like I did in the above code, it just looks prettier
to me, and it makes sure that if anyone else (later) uses your config
file, they don't have to worry about colliding variable names in their
namespace.

HTH
--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