Could you give me some explanation on this Perl line

F

fl

Hi,
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;
 
F

fl

Hi,
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;

More specific here. Why there is one more $ in the first argument? \$
$1 means '$1'? I still do not understand the reason for this. Second,
There is no definition on w, what is the \w+ for? Thanks
 
F

fl

Hi,
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;

More specific here. Why there is one more $ in the first argument? \$
$1 means '$1'? I still do not understand the reason for this. Thanks
 
F

fl

Hi,
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;

More specific here. Why there is one more $ in the first argument? \$
$1 means '$1'? I still do not understand the reason for this.
In order to understand it clearly, could you write the above line in
several small lines?
Thanks
 
J

Josef Moellers

Am 3.11.2010 schrub fl:
Hi,
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;

In essense, it takes each argument until it reaches one that doesn't
look like <word>=<something> and assigns <something> to the variable $word.

There is more than one $ in the first argument as the first $ is escaped
and the "$1" is replaced by the first matching string, resulting in
"$<word>" and the "$2" is replaced by whatever is after the equals sign.

Note that fiddling around with variable names like this is deprecated.
One should use a hash for this.

HTH,

Josef
 
W

Wolf Behrenhoff

Hi,
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;

This piece of code should NOT be relevant to your project as it is very
bad code.

eval
"\$$1=\$2" <--- It creates a variable called $1 and assign the value
$2 to it. You should NOT do this, use a hash instead. Read
perldoc -q "variable name" for an explanation.
while
@ARGV Do this for all command line arguments
&& $ARGV[0]=~ /^(\w+)=(.*)/ as long as it looks like var=value
&& shift; and remove this command line arg

Better use Getopt! See
perldoc Getopt::Std
perldoc Getopt::Long

Also always "use strict; use warnings;" in all your programs. The above
example will not even compile with "use strict". That is a clear sign
that something should be changed in the code.

Wolf
 
M

Martijn Lievaart

Also always "use strict; use warnings;" in all your programs. The above
example will not even compile with "use strict". That is a clear sign
that something should be changed in the code.

$ cat t.pl
#!/usr/bin/perl
use warnings;
use strict;
eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift;

$ perl t.pl

Although I agree, you should not set variables like this, the code does
compile under strict.

M4
 
F

fl

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--

Thanks to all of you. From what you said, I know this line's function
and where I can get the appropriate way (GetOpt) to do that. The
feedbacks make me avoid some bad thing on this learning.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top