Operator Precedence

Discussion in 'Perl Misc' started by Jeff Mott, Sep 22, 2003.

  1. Jeff Mott

    Jeff Mott Guest

    Since the assignment operator has higher precedence than comma,
    shouldn't the following line

    my $s = substr "foo", 0, 1;

    technically be interpreted as

    (my $s = substr "foo"), 0, 1;

    ?
    Jeff Mott, Sep 22, 2003
    #1
    1. Advertising

  2. In article <>, Jeff Mott wrote:
    > Since the assignment operator has higher precedence than comma,
    > shouldn't the following line
    >
    > my $s = substr "foo", 0, 1;
    >
    > technically be interpreted as
    >
    > (my $s = substr "foo"), 0, 1;


    To assign to $s, the right hand side must be evaluated. You
    have a call to substr() there (uses at most four arguments), so
    the zero and the one will be interpreted as the second and third
    argument of that function call.

    When in doubt, use parantheses.

    --
    Andreas Kähäri
    Andreas Kahari, Sep 22, 2003
    #2
    1. Advertising

  3. Jeff Mott

    Anno Siegel Guest

    Jeff Mott <> wrote in comp.lang.perl.misc:
    > Since the assignment operator has higher precedence than comma,
    > shouldn't the following line
    >
    > my $s = substr "foo", 0, 1;
    >
    > technically be interpreted as
    >
    > (my $s = substr "foo"), 0, 1;
    >
    > ?


    ....and then return a syntax error because substr() doesn't have enough
    arguments? Fortunately, that is not how it works.

    When Perl sees "substr" it begins to parse for arguments for the
    substr function, until it finds a marker (a closing ")" or ";") that
    indicates that a complete expression has been parsed. Only then does
    it begin to look for more list elements on the right side, and only
    these compete with "=" for precedence.

    Anno
    Anno Siegel, Sep 22, 2003
    #3
  4. Jeff Mott

    Jeff Mott Guest

    > To assign to $s, the right hand side must be evaluated. You
    > have a call to substr() there (uses at most four arguments), so
    > the zero and the one will be interpreted as the second and third
    > argument of that function call.


    I know what it does do, I was referring to what it seemed like it
    should do. But Abigail answered it so we're good here.
    Jeff Mott, Sep 22, 2003
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    (Jeff Mott) wrote in news:970676ed.0309212353.1949ae95
    @posting.google.com:

    > Since the assignment operator has higher precedence than comma,
    > shouldn't the following line
    >
    > my $s = substr "foo", 0, 1;
    >
    > technically be interpreted as
    >
    > (my $s = substr "foo"), 0, 1;
    >
    > ?


    You would think so, wouldn't you?

    However, the commas in the above expressions are not comma operators, they
    are operator argument-separator commas.

    How to tell the difference? 1. perldoc perlop. 2. experience. :)

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP29TFWPeouIeTNHoEQIrqwCg0r3dUNkGBRiwOSIUdvf2+wEd7XEAnRg+
    Lsk8bxH23sGyP1THRokvRVEX
    =Igzx
    -----END PGP SIGNATURE-----
    Eric J. Roode, Sep 22, 2003
    #5
    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. David Frauzel

    Operator precedence

    David Frauzel, May 13, 2004, in forum: Perl
    Replies:
    2
    Views:
    529
    Luc Van Hove
    May 17, 2004
  2. kbd

    Operator Precedence

    kbd, Jul 22, 2004, in forum: Java
    Replies:
    10
    Views:
    734
    Roedy Green
    Jul 27, 2004
  3. Master of C++
    Replies:
    4
    Views:
    702
    Karl Heinz Buchegger
    Feb 7, 2005
  4. Richard Bos

    && and || Operator precedence enforcement

    Richard Bos, Jan 19, 2004, in forum: C Programming
    Replies:
    18
    Views:
    588
    Dan Pop
    Jan 20, 2004
  5. siliconwafer

    operator precedence with && and comma

    siliconwafer, Jul 29, 2005, in forum: C Programming
    Replies:
    21
    Views:
    625
Loading...

Share This Page