http://www.perlmonks.org?node_id=780902

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,

Replies are listed 'Best First'.
Re: Reading .cfg file
by Your Mother (Archbishop) 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 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 Anonymous Monk on Jul 17, 2009 at 05:14 UTC
Re: Reading .cfg file
by afoken (Chancellor) 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". ;-)