in reply to Config file default options?

I think that if you store your values in a hash rather than individual variables then you'll find your system will be much easier to extend.

Here's a first draft of how I'd do it. It creates a hash called %cnf and the way the hash is built is controlled by another (potentially confusingly named) has called %config. In this example, 'to' and 'from' are set in the user input (simulated with the DATA filehandle) and the 'subject' is set from a default.

use strict; use warnings; sub subject { 'Subject: default subject'; } sub to { 'To: default_to@example.com'; } sub from { 'From: default_from@example.com'; } my %config = ( to => { regex => '^To:\s', sub => \&to }, from => { regex => '^From:\s', sub => \&from }, subject => { regex => '^Subject:\s', sub => \&subject }, ); my %cnf; # Load user data foreach (<DATA>) { chomp; foreach my $c (keys %config) { if (/$config{$c}{regex}/) { $cnf{$c} = $_; last; } } } # Look for missing values and fill in defaults foreach (keys %config) { unless (exists $cnf{$_}) { $cnf{$_} = $config{$_}{sub}->(); } } use Data::Dumper; print Dumper \%cnf; __DATA__ To: foo@example.com From: bar@example.com
--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Config file default options?
by sanjay nayak (Sexton) on Oct 04, 2006 at 13:28 UTC

    Hi Davorg

    I am facing little problem. By using your program i got the header values which is either default values or user defined values.But the problem is that from the same program suppose i got the values for a INVITE request, during the running of the program i suppose use your program for getting the header values of a ACK request, then the values of the hash (%cnf) are not changed. It takes the previous value which is stored during the procesing of the INVITE request. How it can take different values for different requests?

      Sounds like a scoping issue to me. You need to ensure that each time you process an new message that you also create a new %cnf variable. I'd guess that you need to add my %cnf; near the start of your processing loop.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re^2: Config file default options?
by sanjay nayak (Sexton) on Sep 08, 2006 at 07:28 UTC
    Hi thanks for suggestion. but i have little doubts.Actually your progran only prints the default values. It does not take the values mentioned by the user in the config file. Suppose the default values are kept in sub routines like
    sub subject { 'Subject: default subject'; } sub to { 'To: default_to@example.com'; } sub from { 'From: default_from@example.com'; }
    I could not understand the following syntax, Just give me some hints about it.
    my %config = ( to => { regex => '^To:\s', sub => \&to }, from => { regex => '^From:\s', sub => \&from }, subject => { regex => '^Subject:\s', sub => \&subject }, ); my %cnf; # Load user data open DATA,'sss.txt' or die $!; foreach (<DATA>) { chomp; foreach my $c (keys %config) { if (/$config{$c}{regex}/) { $cnf{$c} = $_; last; } } }

    sss.txt contains
    To:sanjay<sanjay@36.212.176.92>
    From:sanjay1<sanjay1@36.212.176.90>
    Subject:Hi
    Actually what i need, if it does not get any value in the config file it will call the sub routines for taking the default values.After that How can i assign the keys and its values in a variable so that i can use that variable for different purpose?
      Hi thanks for suggestion. but i have little doubts.Actually your progran only prints the default values.

      Actually, my code works fine on my sample data. It appears not to work on your data, but that's because your data is subtley different to mine. Explaining why (later) will give me a good way to show how flexible my approach is.

      I could not understand the following syntax, Just give me some hints about it.
      my %config = ( to => { regex => '^To:\s', sub => \&to }, from => { regex => '^From:\s', sub => \&from }, subject => { regex => '^Subject:\s', sub => \&subject }, );

      Here we're defining a hash which controls how the config is produced from the input files. It basically defines three things. The keys in the hash are the names of the data items that we are looking for. They will also contain the names of the keys in the hash that will contain the _actual_ configuration. Each associated value in the hash is a reference to another hash which contains details on how to find that value. This is in two parts. There is a regular expression which matches against the user input and a reference to a subroutine which is called to give the default value if no value is found in the user input.

      And this is where we can make changes to make your input work. For example, my regex for finding a "To:" header is ^To:\s. And that works on my input. But in your sample data you don't have a space after the colon. So the regex no longer matches and you end up getting the default values. To change the regex so it matches your data, you simply need to change the regex to remove the \s. See, you just edit the configuration, not the actual code.

      Actually what i need, if it does not get any value in the config file it will call the sub routines for taking the default values.

      I know. And that's exactly what I've given you.

      After that How can i assign the keys and its values in a variable so that i can use that variable for different purpose?

      The configuration (taken from the user input or the default subroutines) ends up in the hash %cnf. In this example, %cnf ends up with the keys 'to', 'from' and 'subject'. If this code was in "read_config" subroutine, then you could return the %cnf hash from that subroutine and use it elsewhere in your program.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg


        Hi thanks. I could understand your suggestion completely. but can you suggest some code for my question "After that How can i assign the values of keys in different variables so that i can use these variables in different part of my program?".
        Thanks and regards
        sanjay