2

What does the => mean in Perl?

my $CreateArray = [
objectClass => [ "top", "person", "organizationalPerson", "inetOrgPerson" ],
cn => "Jane User",
uid => "0000001",
sn => "User",
mail => "JaneUser@mycompany.com"
];

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  'JaneUser@mycompany.com'

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.

  • 1
    A name for the structure in the top code box or the bottom? Please clarify a bit so that I can improve my answer. – Dynamic Aug 12 '12 at 21:04
  • Thank you for your answer. Well, I meant both boxes and especially the "comma operator". I didn't know this term and thought it's a hash. I didn't know that flat structures are used for pairs of keys and values. – chris Aug 12 '12 at 21:56
  • There's also a winking fat comma. – Chankey Pathak Nov 21 '12 at 5:12
10

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.

|improve this answer|||||
  • 1
    The left hand side is only quoted if it's strictly composed of Perl identifier characters. – brian d foy Nov 18 '13 at 9:31
0

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

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.