in reply to reading from a file and initializing a variable
There are several problems here. First I'll address the big one. You really REALLY don't want to do what you think you want to do. What you are trying to do is use symbolic references. What you should do is be happy with the hash. You know, the global symbol table itself is a glorified hash too. If Perl thinks a hash is good enough for it, a hash should be good enough for you too. Don't initialize global values based on symbolic references. What if the file contained some variable name that you hadn't planned for, or one that you were using for something else? You would have a difficult to track down bug.
The second problem is related to the first one. Symbolic references can only be used on package globals, but you've declared $step1, $step2, and $step3 to be lexical variables. That won't prevent you from initializing package globals of the same name, by the way. Imagine the confusion of having both lexicals and package globals with the same name! You're headed down that road.
The third problem is that your $key = $value assignment simply clobbers the symbol name held in $key, it doesn't assign anything to the package global variable of that name. You can learn how to do this if you dive into the Camel book or Perl's POD, but that would mean learning how to manipulate symbolic references, which you shouldn't be doing anyway. ;)
The fourth problem is this line:
while ($line ne ""){ chomp($line);
$line will never equal "". That's because it will always at least contain a "\n" newline character, except possibly for the last line. chomp first, and then test. Here's the cannonical way to do it:
while ( $line = <FILE> ) { chomp $line; next if $line eq ""; #...... do your stuff here } # end of while loop.
The biggest point to be made here is that instead of trying to initialize $step1, $step2, and $step3, you should be initializing $hash{step1}, $hash{step2}, and so on. Just say no to symbolic refs.
Dave
|
---|