in reply to $var{'a',1,...}
In addition to what davido posted, let me point out that "the space between the curlies" is a magical place, and exploring all its mysteries can lead to countless epiphanies.
For example, there you will find an apparent violation of the Perl dogma that says that "a list can't exist in scalar context." The context between the curlies is a scalar context; this is why, for example, in the following DB interaction, the second line initializes $hash{ 3 }:
Now, in a scalar context, a comma-separated list should evaluate to the last member of the list. Therefore,DB<1> @subkeys = qw( foo bar baz ) DB<2> $hash{ @subkeys } = 5 DB<3> x \%hash 0 HASH(0x846a000) 3 => 5
should amount to initializing $hash{ 'baz' }. Instead, it appears (to those who are not pure of soul) as though the comma-separated list survives long enough in this scalar context for it to be shellacked into a join( $;, LIST ) thingie.$hash{ 'foo', 'bar', 'baz' } = 1;
This apparent violation of dogma is resolved by declaring that the commas in $hash{ 'foo', 'bar', 'baz' } are not comma operators; they receive instead special syntactic dispensation from the Holy Interpreter (I'm sure there is a bull or two written on this somewhere).
But the wonders don't end there. It is not just textual commas that receive a special dispensation, but also =>'s, and even the implicit commas in qw() expressions, which leads to great marvels such as
I am sure one can derive hours of edifying meditation from variations of these examples.DB<1> $s = qw( eenie meenie minie moe ) DB<2> p $s moe DB<3> $hash{ qw( eenie meenie minie moe ) } = 1234 DB<4> x \%hash 0 HASH(0x846a000) "eenie\c\meenie\c\minie\c\moe" => 1234
One last thing: the commas do not get in the way of the auto-quoting that happens inside the curlies. E.g. $hash{ 'foo', 'bar' } and $hash{ foo, bar } refer to the same thing.
the lowliest monk
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: $var{'a',1,...}
by ady (Deacon) on May 23, 2005 at 06:21 UTC |