Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Operator Precedence

Reply
Thread Tools

Operator Precedence

 
 
Jeff Mott
Guest
Posts: n/a
 
      09-22-2003
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;

?
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      09-22-2003
In article <(E-Mail Removed) >, 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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      09-22-2003
Jeff Mott <(E-Mail Removed)> 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
 
Reply With Quote
 
Jeff Mott
Guest
Posts: n/a
 
      09-22-2003
> 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.
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      09-22-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

http://www.velocityreviews.com/forums/(E-Mail Removed) (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+w Ed7XEAnRg+
Lsk8bxH23sGyP1THRokvRVEX
=Igzx
-----END PGP SIGNATURE-----
 
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
operator precedence with && and comma siliconwafer C Programming 21 08-10-2005 12:27 AM
operator= and implicit type conversion - precedence. Master of C++ C++ 4 02-07-2005 09:28 AM
Operator Precedence kbd Java 10 07-27-2004 03:50 AM
Operator precedence David Frauzel Perl 2 05-16-2004 11:19 PM
&& and || Operator precedence enforcement Richard Bos C Programming 18 01-20-2004 01:04 PM



Advertisments