You really should hold off on using symbolic references.
<update>For one thing, symbolic refs can't be used to create lexical variables (which is what it appears you're trying to do).
</update> Perl is one of a very few languages that even allow them, and yet look at how much work is getting accomplished with all those languages that don't implicitly allow symrefs.
In the case of Perl, the best alternative to symbolic references, in many cases, is to use a hash. After all, the global symbol table itself is just a special kind of hash. If it's good enough for the goose, it's good enough for the gander.
use strict; # Always, for now.
use warnings; # Always, while developing code.
my %symbols;
open INFILE, "<mydata.txt" or die "Can't open infile.\n$!";
while ( my $key = <INFILE> ) {
my $value;
unless ( $value = <INFILE> ) { die "Uneven key/value pairs!"; }
$value =~ s/^\s+|\s+$//g;
$symbols{$key} = $value;
}
print "$_: $symbols{$_}\n" for keys %symbols;
Also, please don't keep asking the same question with different titles. If you don't get good answers to your initial question, it's probably because it was unclear. In that case, you can follow-up in the same thread with refined details as to what you're looking for.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.