in reply to Perl configuration files

You might want to spend some time in the Tutorials section. There's a lot of good information for beginners.

Strict is useful for helping Perl help you catch simple mistakes. (That's a bit of a simplification, but it makes the point.) Without seeing your code, I can only guess why you feel it makes your code long. Have you considered that you can use "my" with multiple variables at once as long as you put them in parentheses as a list? And you can assign to it all at the same time, too:

my ($foo, $bar, $bam); # or my ($foo, $bar, $bam) = qw/ red green blue /;

For configuration variables, while you can do that "by hand", there are many, many CPAN modules to make your life easier. For very simple configuration variables, I'd suggest starting with Config::Tiny.

In config.ini:

var1 = blah var2 = 23

In your code:

use Config::Tiny; # Open the config my $config = Config::Tiny->read( 'config.ini' ); my $config_hash = $Config->{_}; print "Var 1: ", $config_hash->{var1}; # You can also change and save the config again $config_hash->{var2} = 42; $config->write( 'config.ini' );

I think you'll find that much easier than trying to write something yourself. (You'll need to install Config::Tiny from CPAN, of course.)

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^2: Perl configuration files
by rbnorthcutt (Initiate) on Sep 15, 2006 at 16:34 UTC
    so basically if i want to include a file with all my variables i should create a Config module

      You don't want to create one, you want to use one that someone has already created for you on CPAN. The example I gave uses "ini" style configuration files, which is just a text file with variables and values separated by an equals sign -- it's not Perl, just text. Config::Tiny takes care of reading that and turning it into Perl data that your program can use.

      If you haven't installed modules from CPAN before, see Installing Modules.

      To help understand the difference between modules versus just including files, you might also want to read Including files.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        thanks for all the help with this. im going to look more into this Config::Tiny. thank you again