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

I'm rather new to Perl and I got a few questions that probably rather simple. I've recently written some scripts that are pretty neat in my book but I don't know if style is correct for the whole perl community. I read the man page about style and what not, but what bothers me is if I use strict my code gets very log, is that how it is? and also I'm using the say variables through the 5 scripts. I want to create one configuration type file that has all my variables in it and say require or use it. do have to make it with the perl headers and chmod the file to be x, can I put use in it, and do I have to make a module? Any help anwsering these questions is much appreciated. Thank you in advance just incase I forget.....

Replies are listed 'Best First'.
Re: Perl configuration files
by xdg (Monsignor) on Sep 15, 2006 at 16:07 UTC

    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.

      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.

Re: Perl configuration files
by Melly (Chaplain) on Sep 15, 2006 at 16:44 UTC

    strict is your friend - it'll save you a lot of debugging (mistyped variable names, etc.).

    As for using or requiring a file to set variables, you could do that, but you probably don't want to.

    What might make sense is to write a function in a second perl script that reads a file containing your parameters and returns them as, say, a hash-reference.

    e.g. (not fully tested)

    require 'test_03.pl'; my $result = SetVars(); foreach(keys %{$result}){ print "$_ : ${$result}{$_}\n"; }

    and then in test_03.pl:

    use strict; sub SetVars(){ my %vars; open(VARS, 'myvars.txt')||die "Cannot open myvars.txt:$!\n"; while(<VARS>){ chomp; $vars{(split /:/)[0]} = (split /:/)[1]; } return \%vars; } return 1;
    Tom Melly, tom@tomandlu.co.uk
Re: Perl configuration files
by ides (Deacon) on Sep 15, 2006 at 16:04 UTC

    I'd suggest using one of the many Config:: modules on cpan.org., specifically Config::General which gives you the ability to have complex configuration files in an Apache like style.

    Frank Wiles <frank@revsys.com>
    www.revsys.com

Re: Perl configuration files
by madbombX (Hermit) on Sep 15, 2006 at 16:44 UTC
    I wrote a program that has a configuration module. The program is not too long and its not perfect, but it shows a little bit of style (a very little bit) and makes use of a config module (Config::ApacheFormat). It might be of some use to you. The program is available here: Exchange to Postfix Firewall Configuration.

    Eric