On Thu, Feb 3, 2011 at 11:46 AM, Shlomi Fish <[email protected]> wrote:
> On Thursday 03 Feb 2011 10:30:22 Avishalom Shalit wrote:
>> 'key' is better . because it is explicit
>> the bareword in {key}
>> is translated into the string 'key'
>
> Actually, they are both equivalent. Like you said ->{key} is translated into
> ->{'key'} which is how Perl behaves.
>
>>
>> unless you are using "strict"
>> which would disallow barewords.
>
> strict, however, still allows barewords inside hash subscripts, and converts
> them to strings automatically, so there is no harm in using it without the
> '...' and it actually adds less clutter to the code.
>
> Another point to consider is that if you are using these for accessing the
> slots/fields/attributes/member-values of objects, you should be using
> accessors instead:
>
I agree that $users{name} and $users{'name'} are the same.
Performance?
===========
As I understand Chanan also knew that they provide the same
*result* and was asking of the internal behavior.
Chanan, why do you care? Do I guess correctly that someone
suggested $users{name} might be slower than $users{'name'} ?
If the issue is performance related then, please remember the standard
answer to that - you could also give - is "use the profiler to measure".
BTW I think Perl actually "optimizes away" the lack of quotes:
D:>perl -MO=Deparse -e "$x{name} = 'Foo'"
$x{'name'} = 'Foo';
D:>perl -MO=Deparse -e "$x{'name'} = 'Foo'"
$x{'name'} = 'Foo';
and even
D:>perl -MO=Deparse -e "$x{"name"} = 'Foo'"
$x{'name'} = 'Foo';
Style
======
Regarding style I am not sure leaving out the quotes is the "better way".
Even though I have been writing that way for many years now.
For two reasons.
I write
$user{email} = '[email protected]';
but when I am teaching, this slightly confuses some people and
I need to give them extra explanation on what is <email> here.
Especially when we reach the other case of
$user{'first name'} = 'Foo';
This is even more obvious issue in a related situation:
my %user = (
email => '[email protected]',
'first name' => 'Foo',
);
Learning the need to put quotes around the keys when there is
a space is just another thing they need to learn at a time when
they are already overloaded with information.
It also looks ugly to have the keys sometimes with
sometimes without quotes so I am seriously thinking
to change my own style to always use the quotes in my code.
In class then I'd use the same and mention the fact that they
can remove the quotes in certain cases only as comment.
regards
Gabor
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl