Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Reading .cfg file

by Sun751 (Beadle)
on Jul 17, 2009 at 02:34 UTC ( #780902=perlquestion: print w/ replies, xml ) Need Help??
Sun751 has asked for the wisdom of the Perl Monks concerning the following question:

I found below code in the PerlMonks and found quite helpful to read configuration(.cfg) file,
sub ReadCfg { my $file = $_[0]; our $err; { # Put config data into a separate namespace package CFG; # Process the contents of the config file my $rc = do($file); # Check for errors if ($@) { $::err = "ERROR: Failure compiling '$file' - $@"; } elsif (! defined($rc)) { $::err = "ERROR: Failure reading '$file' - $!"; } elsif (! $rc) { $::err = "ERROR: Failure processing '$file'"; } } return ($err); } # Get our configuration information if (my $err = ReadCfg('my_cfg.cfg')) { print(STDERR $err, "\n"); exit(1); }
I tried running it with .cfg file where array @CFG holds list of hashes. its works, Now I am having problem to understand what gets initialize in this process "my $rc = do($file);"
Suppose if after reading I want to pass the content of (@CFG) to another subroutine how can that be done?
I think I am totally lost, Any suggestion, Please.
Cheers,

Comment on Reading .cfg file
Download Code
Re: Reading .cfg file
by Anonymous Monk on Jul 17, 2009 at 05:14 UTC
Re: Reading .cfg file
by CountZero (Bishop) on Jul 17, 2009 at 06:29 UTC
    It is unlikely you will find your config data in @CFG. At the very least it will be in @CFG::CFG. You will have to pre-pend the package name (CFG::) to the variable name.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      You're assuming Sun751 has anything resembling that in his config file, for all we know he has bananas :)
        Well, he did say something about a @CFG in his post.

        And if he has bananas, it should be @CFG::bananas!

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Reading .cfg file
by Your Mother (Canon) on Jul 17, 2009 at 07:32 UTC

    I find that sample code awful. Please dig around in the CPAN for some more reasonable, and safer, alternatives: starting with a plain search on config.

Re: Reading .cfg file
by afoken (Parson) on Jul 18, 2009 at 10:29 UTC

    This ...

    my $rc = do($file);

    ... is extremely dangerous. The .cfg file is EXECUTED as Perl code. Imagine a config file containing one or more of the following lines:

    $foo=`rm -rf /`; $bar=qx/rm -rf \//; s|(?{exec qw(rm -rf /)})||; system 'rm -rf /';

    See also: Re^2: conf file in Perl syntax, Re: reading commands from configuration file

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-07-14 12:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (258 votes), past polls