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

How to read hash of .cfg file

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

My .cfg file have hash for example,
my %CFG = ( Command =>[DES=>"Copy", CMD=>"cp file1 file2'Hello'", DES=>"Remove" CMD=>"rm file1", DES=>"View" CMD=>"vi file1" ], );
Can any one suggest me how can I read this hash of config file into my script? Cheers

20090703 Janitored by Corion: Restored content

Comment on How to read hash of .cfg file
Download Code
Re: How to read hash of .cfg file
by CountZero (Bishop) on Jul 03, 2009 at 06:08 UTC
    do 'my_config.cfg' or die $!;
    is the canonical way to do this, BUT:
    1. The config file must be valid Perl (and yours isn't)
    2. You cannot use lexical variables as their scope will end at the end of the config file.

    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: How to read hash of .cfg file
by Anonymous Monk on Jul 03, 2009 at 06:45 UTC
Re: How to read hash of .cfg file
by mzedeler (Pilgrim) on Jul 03, 2009 at 18:52 UTC

    I spent some time writing a reply to one of the many other, nearly identical questions (Reading hash), you have posted. Will you please go back and ask there, if there is something you didn't get?

Re: How to read hash of .cfg file
by ikegami (Pope) on Jul 03, 2009 at 20:37 UTC

    Can any one suggest me how can I read this hash of config file into my script?

    There are a number of common configuration file formats, and some of them can represent arbitrarily nested structures. YAML and JSON appear to be quite popular now.

    Of course, your data structure is still inappropriate for the data is contains. What you posted has a lot of unrelated data at the same level:

    my %CFG = ( Command => [ "DES", "Copy", "CMD", "cp file1 file2'Hello'", "DES", "Remove", "CMD", "rm file1", "DES", "View", "CMD", "vi file1", ], );

    Instead, I'd recommend:

    my %CFG = ( Command => [ { DES => "Copy", CMD => "cp file1 file2'Hello'" }, { DES => "Remove", CMD => "rm file1" }, { DES => "View", CMD => "vi file1" }, ], );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (18)
As of 2014-10-21 12:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (103 votes), past polls