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


In reply to Re: reading from a file and initializing a variable by davido
in thread reading from a file and initializing a variable by s_gaurav1091

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.