fl said:
I just begin Perl learning. A sample Perl program is relevant to my
project. After reading some on Perl, I still do not understand the
first line of this sample, see below. Could you help me on its
meaning? Thanks in advance.
eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift;
If you're just beginning, the major lesson to learn from the above is:
DO NOT write such a mess!
Seriously! Deliberately terse one-liners have a place, in obfuscated
code contests, Perl Golf, and clever .sig files. But, if you intend to
write and maintain useful code that gets Real Work (tm) done, clarity
is far more important than brevity.
What's more, the use of eval in the above to construct a variable name
from a value is pure evil to be avoided whenever possible - use a real
hash instead of abusing the symbol table hash.
A simple, clear way to write the above would be:
#!/usr/bin/perl
use warnings;
use strict;
my %args;
foreach my $arg (@ARGV) {
if ($arg =~ /^(\w+)=(.*)/) {
$args{$1} = $2;
}
}
For anything other than a learning exercise though, I wouldn't bother
reinventing that wheel - GetOpt::Std and GetOpt::Long are sufficiently
round, and learning to avoid unnecessary work by taking advantage of
existing modules is a *big* part of learning Perl.
sherm--