Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Better way to read "configuration" files

by LameNerd (Hermit)
on Mar 03, 2003 at 23:43 UTC ( #240192=perlquestion: print w/replies, xml ) Need Help??

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

I am so bored writting while(<>) loops
to read in files that contain information for my scripts.
Here's an example of what I do when I need to do this:
my $userid; my $password; while(<DATA>) { next if /^#/; ($userid, $password) = split /=/; } print "$userid:$password\n"; __DATA__ #user=passwd test=prrft5142
Does some monk out there have a better way of doing this
sort of thing?

Replies are listed 'Best First'.
Re: Better way to read "configuration" files
by Limbic~Region (Chancellor) on Mar 04, 2003 at 00:02 UTC
Re: Better way to read "configuration" files
by zengargoyle (Deacon) on Mar 03, 2003 at 23:59 UTC
    # myprog.config %authinfo = ( user => 'password', user2 => 'password2', ); # end of myprog.config #!/usr/bin/perl # # myprog.pl - configured by file 'myprog.config' use strict; use warnings; our %authinfo; do 'myprog.config'; print "$_:$authinfo{$_}$/" for keys %authinfo;

    but recently i've let Config::General handle my configuration needs.

Re: Better way to read "configuration" files
by parv (Vicar) on Mar 03, 2003 at 23:52 UTC
Re: Better way to read "configuration" files
by ignatz (Vicar) on Mar 04, 2003 at 02:58 UTC
Re: Better way to read "configuration" files
by ctilmes (Vicar) on Mar 04, 2003 at 14:13 UTC
    Other suggestions are good. My new favorite is YAML. If you haven't seen it, be sure to check out http://yaml.org/.
    use YAML; my @pwlist = Load(join '', <DATA>); # Print as perl structure use Data::Dumper; print Dumper(\@pwlist); __DATA__ # comments ignored user: test passwd: prrft5142 --- user: fred passwd: fredpw
    Output:
    $VAR1 = [ { 'passwd' => 'prrft5142', 'user' => 'test' }, { 'passwd' => 'fredpw', 'user' => 'fred' } ];
Re: Better way to read "configuration" files
by LAI (Hermit) on Mar 04, 2003 at 14:14 UTC
Re: Better way to read "configuration" files
by caedes (Pilgrim) on Mar 04, 2003 at 00:01 UTC
    If I'm feeling extra lazy I usually just 'require' some other file into the program. eg:
    package MY::Conf; #No directories or URLs end in "/" our %var; ################################# $var{DATA_DIR} = "Zephir/data"; $var{CONF_DIR} = "Zephir/conf"; $var{AVATAR_DIR} = "images/buttons"; $var{AVATAR_URL} = "/images/buttons"; $var{TMPL_DIR} = "Zephir/tmpl"; $var{HOME_PAGE} = "http://asdf.net/"; $var{HOSTNAME} = "asdf.net"; 1;

    -caedes

Re: Better way to read "configuration" files
by Caillte (Friar) on Mar 04, 2003 at 15:20 UTC
    After getting bored of this myself I started using XML config files ;)

    This page is intentionally left justified.

Re: Better way to read "configuration" files
by Cabrion (Friar) on Mar 03, 2003 at 23:54 UTC
    I try to allow for whitespace with something like . . .
    my $userid; my $password; while(<DATA>) { next if /^ *#/; ($userid, $password) = split / *= */; $userid =~ s/^ +//; $userid =~ s/ +$//; } print "$userid:$password\n"; __DATA__ #user=passwd test = prrft5142
    The other monks will probably encourage you to use various modules.
Re: Better way to read "configuration" files
by Pug (Monk) on Mar 04, 2003 at 00:00 UTC
    Do you only need the first username/password or all of them. If you want all of them I would do something like this.
    use Data::Dumper; my %ids=map { chomp; #Get rid of the \n (split(/=/))[0,1] } grep( !(/^\s*#/||!/=/), <DATA>); print Data::Dumper->Dump([\%ids]); exit; __DATA__ #user=passwd test=prrft5142 tester=foo #This is a comment foo
    The reason why the grep has 2 patterns to match is for some error checking.
    The first checks to see if it is a comment. And the second checks to see if it has an = sign in it.You probly want to put a bit more checking there like at least 1 leal character on both sides or something. 8)
    Update: added the \s* to the first condition on the grep.
    also added a test case for the \s* comment and the
    conditiosn where there is no =.
    --
    Pug
Re: Better way to read "configuration" files
by mgibian (Acolyte) on Mar 04, 2003 at 01:11 UTC
    Before you get bound to an inflexible file based configuration mechanism, have you considered using something like LDAP? In the long run, that offers far more flexibility and will scale very well.
Re: Better way to read "configuration" files
by Mr_Person (Hermit) on Mar 05, 2003 at 20:26 UTC
    Lots of great CPAN modules for that. You may want to check out AppConfig.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://240192]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2021-03-08 13:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (124 votes). Check out past polls.

    Notices?