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

Hi all,
What I need is to create simple config file which will map fields from csv files into anothers fields in another file. Simple, isn't it? So I created config file 'account.csv':

#User;LoginName;FullName;Description;Password;Profile;DefaultLibrary;S +tate $cfg[0] = "User;$line[2];$line[1];Default account for $line[1];$line[3 +];VIEWER;;"; #Acl;Name;Description;Users $cfg[1] = "Acl;$line[1];Default access list for $line[1];$line[2]#Supp +ort"; #Folder;Name;Description;Scope;Reports;Owner;Parent $cfg[2] = "Folder;$line[1];Default folder for $line[2];$line[1];;admin +istrator;/Customer_SLA";

Then I wrote script which open source csv file push each it line into @file. Then I need something like this:

foreach $line (@file) { @line = split(/;/,$line); require "account.cfg"; print $cfg[0]; foreach $cfg ( @cfg ) { push( @topol, $line[0].";".$cfg ); } }
So in @topol should be for each line of source csv file, three lines for each line of config. But, sure, this doesn't work because I can't use perlfunc:require by this way. (only for first pass are variables filled from @line array) This is only "non working example" :-))) of what I need. There is only one condition: config must simple, even for non Perl users. I can use regex to replace some marks by values but I am sure, that easier way must exist.

I am sure that there is way, but I am so stupid to find it. :-) And what about you? Thanks

Li Tin O've Weedle
mad Tsort's philosopher

Replies are listed 'Best First'.
Re: easy config file
by {NULE} (Hermit) on Oct 05, 2001 at 18:56 UTC
    Hi,

    Back in my younger and less wise days of Perling, I wrote an application that ran on many different instances of data and depended upon a configuration file to know how to act for each instance of the data. I started out with something much like you have demonstrated above, but with requested features and necessary additions I found a need to represent arrays, hashes and compound data structures in my now very complex configuration file.

    These configuration files continue to grow in size and complexity to this day to they point where they can be 20k+ in size and I have an 18 page document describing how to configure it. Not only that, but it is very easy to break and if you do so the entire application stops working (for that instance of the data). (eval catches it, but the data is needed to run, so it just stops.)

    I bring this up in such detail because I did what I thought at the time to be a wise action and kick myself on a daily basis for my actions now. I hope you are not setting yourself up for the same situation that I find myself in today. I face a monumental task of rewriting a large application and re-educating many users.

    If today I was starting over again with my current knowledge I would implement the entire application based on configuration files written in XML. The XML::Simple module excels at performing such tasks. The added advantage is that such files are very eye-ball friendly. So I entreat you to consider this module or some other alternative our wonderful fellow monks can provide.

    Best of luck,
    {NULE}

    -- http://www.nule.org

    Update:
    Thanks for the information, merlyn. I will be sure to eval{use Data::Denter} for my own application. I may be inclined towards XML just because it is so cross-platform, cross-language, etc.

Re: easy config file
by suaveant (Parson) on Oct 05, 2001 at 18:36 UTC
    do() will execute code each time, and is probably more what you are looking for... require will only do it the first time...

                    - Ant
                    - Some of my best work - Fish Dinner

Re: easy config file
by Fletch (Bishop) on Oct 05, 2001 at 19:12 UTC
Re: easy config file
by tstock (Curate) on Oct 05, 2001 at 19:01 UTC
    So I created config file 'account.csv'
    ...
    require "account.cfg";


    File names could be a problem :)
    If you just want to 'require' for each interaction, maybe an eval will help, but there must be a better way...
      I believe account.cfg is the file he showed us and account.csv is a file he is parsing, but does not show us...

                      - Ant
                      - Some of my best work - Fish Dinner

      I am sorry,
      As you mentioned I make mistake. "account.cfg" is config file above, "account.csv" is source file which is parsed into the @file.

      Thanks to all who help me find the right way :-)

      Li Tin O've Weedle
      mad Tsort's philosopher

Re: easy config file
by perrin (Chancellor) on Oct 05, 2001 at 19:22 UTC
    You need to use do() or eval() in the loop.

    I think it would be better to use some simple templates for this. Take a look at Text::Template. It's easier for end-users than what you have right now.