Syntax appears inconsistent - why is this?

D

David Filmer

I can define a hash thusly (this works):

my %food_price = ( 'Hotdog' => 1.00, 'Popsicle' => 0.50 );

Or, similarly:

my %food = ('H' => {label => 'Hotdog ', price => 1.00 },
'P' => {label => 'Popsicle', price => 0.50 });

But the syntax seems inconsistent to me. I would expect to use curlys to
enclose %food (not parens). I associate curlys with hashes. Is there some
rationale behind the fact that Perl uses parens to populate a hash in this
way? Just curious.

curlys = { }, parens = ( )
 
A

A. Sinan Unur

I can define a hash thusly (this works):

my %food_price = ( 'Hotdog' => 1.00, 'Popsicle' => 0.50 );

Or, similarly:

my %food = ('H' => {label => 'Hotdog ', price => 1.00 },
'P' => {label => 'Popsicle', price => 0.50 });

But the syntax seems inconsistent to me. I would expect to use curlys
to enclose %food (not parens). I associate curlys with hashes. Is
there some rationale behind the fact that Perl uses parens to populate
a hash in this way? Just curious.

I am curious too. Did you even bother to read any part of the
documentation?

{label => 'Hotdog ', price => 1.00 } is a reference to an anonymous
hash, not a hash itself.
 
G

Gunnar Hjalmarsson

David said:
I can define a hash thusly (this works):

my %food_price = ( 'Hotdog' => 1.00, 'Popsicle' => 0.50 );

Or, similarly:

my %food = ('H' => {label => 'Hotdog ', price => 1.00 },
'P' => {label => 'Popsicle', price => 0.50 });

But the syntax seems inconsistent to me. I would expect to use curlys
to enclose %food (not parens). I associate curlys with hashes.

Then stop doing so, and start associating them with hash references
instead. :)
 
P

Paul Lalli

David Filmer said:
I can define a hash thusly (this works):

my %food_price = ( 'Hotdog' => 1.00, 'Popsicle' => 0.50 );

Or, similarly:

my %food = ('H' => {label => 'Hotdog ', price => 1.00 },
'P' => {label => 'Popsicle', price => 0.50 });

But the syntax seems inconsistent to me. I would expect to use curlys to
enclose %food (not parens). I associate curlys with hashes.

Your association is incorrect.
( ) are used to create arrays and hashes.
{ } are used to create hash references
[ ] are used to create array references
Is there some
rationale behind the fact that Perl uses parens to populate a hash in this
way? Just curious.

Because Perl always uses parens to populate a hash, and there's nothing
in your example that suggests the need for a special case.

Paul Lalli
 
T

Tad McClellan

my %food_price = ( 'Hotdog' => 1.00, 'Popsicle' => 0.50 );

I associate curlys with hashes.


Curlies are overloaded.

Sometimes they are part of a hash access.

Sometimes they create an anonymous hash.

Sometimes they form a BLOCK.

Sometimes they delimit a ${variable} name.

etc...

(And sometimes they form a smiley :-} , or is that a "grimacey"? )



Is there some
rationale behind the fact that Perl uses parens to populate a hash in this
way?


Perl does not use parens to populate a hash, it uses a "list" to
populate a hash.

Often parens are required to override precedence so that Perl's
parser can recognize it as a list, but parens to not form a list.
 
T

Tad McClellan

Paul Lalli said:
( ) are used to create arrays and hashes.


They are often used to _populate_ arrays and hashes, but they
do not "create" them.

{ } are used to create hash references
[ ] are used to create array references


Now those _do_ create things...
 
V

Vetle Roeim

A. Sinan Unur ([email protected]) wrote on MMMMXLVII September
MCMXCIII in <URL:news:[email protected]>: [...]
The answer is that the parens *aren't* creating a construct. The parens
are just there because of precedence (as assignment has a higher
precedence
than the comma operator). To initialize a hash, one assigns a list to it
(just like with arrays). And visa versa, a hash in list context gives you
back a list of its key/value pairs.

my %food_price = qw !Hotdog 1.00 Popsicle 0.50!;

would have worked as well, and no parens are needed.

Thank you for an enlightening post ... I had never realized that this is
the way it works. Now that I think about it, it seems so obvious. :)
 
J

Joe Smith

Paul said:
David Filmer said:
enclose %food (not parens). I associate curlys with hashes.

Your association is incorrect.
( ) are used to create arrays and hashes.
{ } are used to create hash references
[ ] are used to create array references

I would phrase it differently.
( ) are used to build lists, which can populate arrays and hashes.
{ } are used to create a reference to an anonymous hash.
[ ] are used to create a reference to an anonymous array.

-Joe
 
T

Tony Skelding

Abigail said:
Joe Smith ([email protected]) wrote on MMMMXLVIII September MCMXCIII in
<URL:%% Paul Lalli wrote:
%%
%% >>enclose %food (not parens). I associate curlys with hashes.
%% >
%% > Your association is incorrect.
%% > ( ) are used to create arrays and hashes.
%% > { } are used to create hash references
%% > [ ] are used to create array references
%%
%% I would phrase it differently.
%% ( ) are used to build lists, which can populate arrays and hashes.

Eh, wrong. () seldomly build lists. It certainly doesn't in:

my %hash = (key1 => 'val1', key2 => 'val2');

The parens in the above expression play exactly the same role as
they do in:

my $val = 3 * (4 + 5);

They help the parser to construct a parse-tree.

%% { } are used to create a reference to an anonymous hash.
%% [ ] are used to create a reference to an anonymous array.


Abigail

Or to put it it simpler terms, the parentheses are only necessary
because the = has higer precedence than the ,
 
J

John W. Krahn

Abigail said:
Joe Smith ([email protected]) wrote on MMMMXLVIII September MCMXCIII in
<URL:%%
%% I would phrase it differently.
%% ( ) are used to build lists, which can populate arrays and hashes.

Eh, wrong. () seldomly build lists. It certainly doesn't in:

my %hash = (key1 => 'val1', key2 => 'val2');

The parens in the above expression play exactly the same role as
they do in:

my $val = 3 * (4 + 5);

They help the parser to construct a parse-tree.

Could you (or anyone) explain what the inner parentheses are doing in the
second example and why please?

while () { print "infinite loop\n" }

while (()) { print "will not print\n" }


:)

John
 
J

John W. Kennedy

Abigail said:
Eh, wrong. () seldomly build lists. It certainly doesn't in:
my %hash = (key1 => 'val1', key2 => 'val2');
The parens in the above expression play exactly the same role as
they do in:

my $val = 3 * (4 + 5);

No, the () /are/ building a list above. The statement is syntactic
sugar for:

my %hash = ('key1', 'val1', 'key2', 'val2');
 
J

Jeff 'japhy' Pinyan

No, the () /are/ building a list above. The statement is syntactic
sugar for:

my %hash = ('key1', 'val1', 'key2', 'val2');

The parentheses *are* being used precendence-wise, as Abigail is stating.
The only thing that makes the right-hand side of the hash assignment a
list is the fact the the left-hand side is assignment to a hash.

my %hash = 1;

There's a one-element list on the right-hand side.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
Senior Dean, Fall 2004 % have long ago been overpaid?
RPI Corporation Secretary %
http://japhy.perlmonk.org/ % -- Meister Eckhart
 
T

Tad McClellan

John W. Kennedy said:


Whenever I find myself about to say that to Abigail, I take pause
to think about it some more, and end up not needing to say it
very often. :)

the () /are/ building a list above.


The documentation for the software that we are using says they
are not.

I take pause twice when I find myself disagreeing with the documentation
that came with the software I'm using.

The "List value constructors" section in perldata.pod is
germane to this discussion:

List values are denoted by separating individual values by commas
(and enclosing the list in parentheses where precedence requires it):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Darn! Looks like we won't be able to understand it without
following a reference, so off to perlop.pod to find out when
precedence requires it...

Perl operators have the following associativity and precedence,
listed from highest precedence to lowest.
...
right = += -= *= etc.
left , =>


The statement is syntactic
sugar

The "fat comma" is indeed sugar, but I don't think anyone was confused
about that. We have been discussing the role of the parenthesis
in this thread.

for:

my %hash = ('key1', 'val1', 'key2', 'val2');


If we write it without the parenthesis:

my %hash = 'key1', 'val1', 'key2', 'val2';

we can see from the above precedence table that it will be parsed as:

(my %hash = 'key1'), 'val1', 'key2', 'val2';

and we don't get the semantic that is needed, just as when we write

my $val = 3 * 4 + 5;

without parenthesis it is parsed as

my $val = (3 * 4) + 5;

and we don't get the semantic that is needed.


So, we *do* need parenthesis there, but not because they "form a list",
but because they override the default precedence.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top