Operating System - HP-UX
1753830 Members
9371 Online
108806 Solutions
New Discussion юеВ

Perl programming assistance

 
SOLVED
Go to solution
Kurt Renner
Frequent Advisor

Perl programming assistance

I am writing a perl script in which I have some global variables that contain vital information for the script to function. These variables will be set to a default value upon initialization, OR by a command-line switch, OR by variable/value pairs in a configuration file. Command-line switches must override all other methods. The configuration file is formatted according to standard perl syntax i.e.:

$Backup{'BCVset'} = 1;
$Brbackup{'BackupType'} = "online_split";
.
.
etc

Within the main script, I want to check to see if the variable is already defined (as the result of a command line switch) when the variable/value is encountered in the configuration file. When done reading the configuration file, any required variable that was not set as a result of a command-line switch or a setting in the configuration file will be defaulted to a set value.

Following is a code snippet to help explain:

# Open the configuration file
open (CFG_FILE, "$Cfgfile");
# Read the config file line by line.
while (){
chomp;
# If the line length is greater than zero
# and it is not a comment do this section
if (length && !/^\s*#.*$/){
# make sure the value is quoted
s/\s*=\s*(\S+);/ = qw($1);/;
# Capture the variable name
/^(\S+)\s*=/;
##### Here I want to check if the variable
##### encounterd is already defined with
##### a value
if (!defined($1)) {
...doesn't work. $1 is always defined
because of the previous match
operation
if (!defined($$1)) {
...doesn't work.

any ideas would be appreciated.

Thanks!
Kurt Renner
Do it right the first time and you will be ahead in the long run.
4 REPLIES 4
Kurt Renner
Frequent Advisor

Re: Perl programming assistance

Sorry for the formatting of the code. I put it in a .txt attachment so it's easier to follow.
Do it right the first time and you will be ahead in the long run.
Kurt Renner
Frequent Advisor

Re: Perl programming assistance

Let's try that again....
Do it right the first time and you will be ahead in the long run.
Rodney Hills
Honored Contributor
Solution

Re: Perl programming assistance

If I am processing a config file for options, I would do something like the following-

($var,$val)=/^\s*(\S+)\s*=\s*(\S+)/;
$myopt{$var}=$val unless $myopt{$var};

I use %myopt with a key of the variable name to hold the values and to do testing.

HTH

-- Rod Hills
There be dragons...
Kurt Renner
Frequent Advisor

Re: Perl programming assistance

Rodney,
I have found a little simpler way of doing what I want. It's less efficient, but easier for me to deal with. I have a ParseArgs routine that's called first. I need this to get one critical piece of information from the command-line arguments. I then read the configuration file and override any variables specified in the config file by doing a "eval $_" per line. I then call the ParseArgs routine again to override any variables set via the config file with the passed arguments.

This solves my problem. Your suggestion would also work. Thanks for your input.
Do it right the first time and you will be ahead in the long run.