comma operator

A

ashishg

Hi,

I am currently reading Programming Perl.

Regarding the comma operator, on page 93, it says:

In a scalar context it evaluates its left argument, throws the value
away, then evaluates its right argument and returns that value.

On page 55, there is a code snippet:

while ($_ = ARGV[0], /^-/) {
shift;
last if /^--$/;
... # other lines
}

which is equivalent to:

while (<>) {
... # some code
}

I did not understand:

while ($_ = ARGV[0], /^-/) {

if ARGV[0] is to be evaluated and thrown away, and $_ is to be assigned
/^-/, then why not just say $_ = /^-/

I do not know the use/effect of $_ = /^-/ too ...

Thanks,
Ashish
 
J

John Bokma

wrote:
Hi,

I am currently reading Programming Perl.

Regarding the comma operator, on page 93, it says:

In a scalar context it evaluates its left argument, throws the value
away, then evaluates its right argument and returns that value.

On page 55, there is a code snippet:

while ($_ = ARGV[0], /^-/) {
shift;
last if /^--$/;
... # other lines
}

which is equivalent to:

while (<>) {
... # some code
}

I did not understand:

while ($_ = ARGV[0], /^-/) {

if ARGV[0] is to be evaluated and thrown away, and $_ is to be assigned
/^-/, then why not just say $_ = /^-/

$_ gets the value of ARGV[0], and the *result* of that expression is
thrown away. Also note that /^-/ is not an assignment, but a match.

It also looks like you have a different Programming Perl than me, not
the 3rd edition :-D.
 
E

Eric Amick

Hi,

I am currently reading Programming Perl.

Regarding the comma operator, on page 93, it says:

In a scalar context it evaluates its left argument, throws the value
away, then evaluates its right argument and returns that value.

On page 55, there is a code snippet:

while ($_ = ARGV[0], /^-/) {
shift;
last if /^--$/;
... # other lines
}

which is equivalent to:

while (<>) {
... # some code
}

I did not understand:

while ($_ = ARGV[0], /^-/) {

if ARGV[0] is to be evaluated and thrown away, and $_ is to be assigned
/^-/, then why not just say $_ = /^-/

Because assignment has higher precedence than comma. It parses as

while (($_ = $ARGV[0]), /^-/) {

so the value of $ARGV[0] is not being thrown away, at least not yet--the
shift does that. The expression saves $ARGV[0] in $_, then sees if it
starts with a hyphen.
 
A

Ashish

Thanks!

Yes, I noticed after you pointed out that I have the 2nd Edition. I did
not know that a 3rd Edition existed. I just checked it on amazon.com. I
need to get it!
 
J

John Bokma

Ashish said:
Thanks!

Yes, I noticed after you pointed out that I have the 2nd Edition. I did
not know that a 3rd Edition existed. I just checked it on amazon.com. I
need to get it!

I can recommend it. Moreover, I consider the 3rd edition outdated too, here
and there. But I don't expect a "4th edition" soon, but I *do* expect a
Perl 6 edition, with Perl 5 info. Probably 2 books :-D.
 
T

Tad McClellan

I am currently reading Programming Perl.

Regarding the comma operator, on page 93, it says:

In a scalar context it evaluates its left argument, throws the value
away, then evaluates its right argument and returns that value.

On page 55, there is a code snippet:

while ($_ = ARGV[0], /^-/) {
shift;
last if /^--$/;
... # other lines
}

which is equivalent to:

while (<>) {
... # some code
}


You should perhaps reread that section.

They are NOT equivalent, and the (old) Camel book does not say
that they are.
 
B

Bart Lateur

I did not understand:

while ($_ = ARGV[0], /^-/) {

if ARGV[0] is to be evaluated and thrown away, and $_ is to be assigned
/^-/, then why not just say $_ = /^-/

"," has lower precedence than "=". So what happens is:

$_ = $ARGV[0]
/^-/

and it's the result of the last expression, the regex match, that is
returned.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top