sanjay nayak has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Config file default options?
by davorg (Chancellor) on Sep 07, 2006 at 11:24 UTC | |
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.
-- <http://dave.org.uk> "The first rule of Perl club is you do not talk about
Perl club." | [reply] [d/l] |
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? | [reply] |
by davorg (Chancellor) on Oct 05, 2006 at 08:54 UTC | |
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." | [reply] |
by sanjay nayak (Sexton) on Sep 08, 2006 at 07:28 UTC | |
I could not understand the following syntax, Just give me some hints about it.
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? | [reply] [d/l] [select] |
by davorg (Chancellor) on Sep 08, 2006 at 08:37 UTC | |
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.
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." | [reply] [d/l] |
by sanjay nayak (Sexton) on Sep 08, 2006 at 09:08 UTC | |
by davorg (Chancellor) on Sep 08, 2006 at 09:42 UTC | |
| |
|
Re: Config file default options?
by jonadab (Parson) on Sep 07, 2006 at 11:29 UTC | |
First, I think I would store the config file data in a hash, rather than an array. That way you can look up a particular datum more easily, by its name. As for the code that reads the config file, there are modules on the CPAN for reading config files, see for instance here, although some of those may require the config file to be in a certain format that may not match yours. (On the other hand, if you don't have to accommodate pre-existing config files, you could just define your config file format to be the format that the module reads.) Now, the other thing you asked about is checking the config data for each of several values and, if they are not present, calling a routine to construct them. If I were doing that, the first thing I'd do is make a list of the values I want (e.g., From, To, Cc, and so forth). The second thing I'd do is make the list a hash, with the name of the header being the key, and the subroutine that constructs the header would be the value. Something along these lines...
The third thing would be to actually do the assignments, and I'd probably use a foreach loop over the keys for that part. Something like this...
It is possible to golf that loop down to one line, but you either lose the exists test (resulting in a situation where a user cannot specify a false value) or else have to use the ? : operator, which I find beginners often don't like. Anyway, the dispatch table is going to be most of the work. Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy. | [reply] [d/l] [select] |
|
Re: Config file default options?
by holli (Abbot) on Sep 07, 2006 at 11:29 UTC | |
Given a config file like this prints Just as i think you need it. edit: added jonadab's sugguestion. holli, /regexed monk/ | [reply] [d/l] [select] |
by jonadab (Parson) on Sep 07, 2006 at 11:46 UTC | |
I would throw the word exists (or defined) in there after the unless, just in case a user ever wants to specify a false value (e.g., a blank subject line is very rarely a good idea, but occasionally when sending mail to certain kinds of really old autoresponders that read commands from the subject line it can be desirable; there are also highly specialised cases for a blank From field). Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy. | [reply] [d/l] [select] |
|
Re: Config file default options?
by GrandFather (Saint) on Sep 07, 2006 at 11:21 UTC | |
Is the format of the configuration file set, or can you make it whatever you want it to be? If you can choose the file format then you can use one of the many Config modules in CPAN. If you do not have any choice over the configuration file format then consider using a hash to store the configuration values:
DWIM is Perl's answer to Gödel | [reply] [d/l] |
|
Re: Config file default options?
by mantra2006 (Hermit) on Sep 07, 2006 at 15:14 UTC | |
I could see 5 replies to your question and I couldn't see your response ...it would be nice if you respond to these replies and monks will feel happy and also help you out if you have any issues
Thanks & Regards Sridhar | [reply] |