Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > comma operator

Reply
Thread Tools

comma operator

 
 
ashishg@mailinator.com
Guest
Posts: n/a
 
      04-22-2005
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

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      04-22-2005
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 .

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

 
Reply With Quote
 
 
 
 
Eric Amick
Guest
Posts: n/a
 
      04-22-2005
On 21 Apr 2005 20:26:22 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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 $_ = /^-/


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.

--
Eric Amick
Columbia, MD
 
Reply With Quote
 
Ashish
Guest
Posts: n/a
 
      04-22-2005
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!

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      04-22-2005
Ashish wrote:

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

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-22-2005
(E-Mail Removed) <(E-Mail Removed)> wrote:

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


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      04-22-2005
(E-Mail Removed) wrote:

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

--
Bart.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM
the whys of the comma operator Michael Scarlett C Programming 8 06-19-2004 12:08 AM
Confused about the comma operator Derek C++ 6 04-13-2004 09:03 PM
Yet another comma operator question Koster C Programming 7 01-18-2004 01:07 AM
Overloading comma to give a concatenation operator: various problems Paul Davis C++ 7 07-17-2003 07:25 AM



Advertisments