in reply to Re^2: Config file default options?
in thread Config file default options?

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

Replies are listed 'Best First'.
Re^4: Config file default options?
by sanjay nayak (Sexton) on Sep 08, 2006 at 09:08 UTC

    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
      How can i assign the values of keys in different variables so that i can use these variables in different part of my program?

      I'm not going to do that as I think it's a bad idea and that keeping the values in a single hash is easier to code, more flexible and more maintainable. You have all the data in a hash. What can you gain from copying it into separate variables?

      If you really want to do this, then you should look for information about "symbolic references". But I really don't think it's a good idea.

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

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

        Hi
        Thanks a lot for your suggestion. It helps me lot for solving my problem.
        Regards
        sanjay