What does the =>
mean in Perl?
my $CreateArray = [
objectClass => [ "top", "person", "organizationalPerson", "inetOrgPerson" ],
cn => "Jane User",
uid => "0000001",
sn => "User",
mail => "[email protected]"
];
produces
CreateArray:
0 'objectClass'
1 ARRAY(0x104ef40)
0 'top'
1 'person'
2 'organizationalPerson'
3 'inetOrgPerson'
2 'cn'
3 'Jane User'
4 'uid'
5 0000001
6 'sn'
7 'User'
8 'mail'
9 '[email protected]'
It seems that even numbers are keys and odd numbers are values. Why don’t they use hashes? Because they aren’t ordered? Is there a name for this strange structure?
Thank you.
3
Generally, the “big arrow” (=>
) in Perl is basically a comma, with one difference: everything on its left is treated as if it is quoted. So this:
Foo => Bar
Is the same as:
'Foo', Bar
For more info, see perlop.
1
Agreed, see perlop (perldoc perlop) for details.
For more discussion, here’s a decent thread. Don’t confuse the key=>value notation as being significant to a hash…
For example:
# no =>
my %hash = (
mykey, 'blah',
yourkey, 'blah2',
seehere, 'blah3',
90, 'see further -- try putting a 0 in front of my key');
foreach my $key (sort keys %hash) {
print qq#$key: $hash{$key}n#;
}
exit(0);
But arrows will do just as well, without quotes, avoid unquoted octals.
Reference: http://www.perlmonks.org/?node_id=381320