Operator Precedence

J

Jeff Mott

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;

?
 
A

Andreas Kahari

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

Anno Siegel

Jeff Mott said:
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
 
J

Jeff Mott

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

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (Jeff Mott) wrote in @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-----
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top