comma operator

Discussion in 'Perl Misc' started by ashishg@mailinator.com, Apr 22, 2005.

  1. Guest

    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
    , Apr 22, 2005
    #1
    1. Advertising

  2. John Bokma Guest

    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.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Apr 22, 2005
    #2
    1. Advertising

  3. Eric Amick Guest

    On 21 Apr 2005 20:26:22 -0700, 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
    Eric Amick, Apr 22, 2005
    #3
  4. Ashish Guest

    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!
    Ashish, Apr 22, 2005
    #4
  5. John Bokma Guest

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

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Apr 22, 2005
    #5
  6. <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 22, 2005
    #6
  7. Bart Lateur Guest

    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.
    Bart Lateur, Apr 22, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Davis
    Replies:
    7
    Views:
    593
    Paul Davis
    Jul 17, 2003
  2. Derek
    Replies:
    6
    Views:
    370
    Old Wolf
    Apr 13, 2004
  3. Koster

    Yet another comma operator question

    Koster, Jan 16, 2004, in forum: C Programming
    Replies:
    7
    Views:
    376
    Old Wolf
    Jan 18, 2004
  4. Michael Scarlett

    the whys of the comma operator

    Michael Scarlett, Jun 10, 2004, in forum: C Programming
    Replies:
    8
    Views:
    314
    Michael Scarlett
    Jun 19, 2004
  5. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    480
    Barry Schwarz
    Feb 8, 2005
Loading...

Share This Page