-1

According to the Perl docs, the => operator in list context is equivalent to a comma with extra quoting powers. That much I understand, but the docs also state

The => operator is helpful in documenting the correspondence between keys and values in hashes, and other paired elements in lists.

They give this example:

login( $username => $password );

Now, to me that seems equivalent to

login($username, $password)

Is there any other difference apart from the implicit quoting between the two? Are the two parameters passed to login() linked somehow?

More importantly, what would be an example of "paired elements" in a list (not a hash)? If I define a list as

@f=("foo" => "bar")

can I somehow use foo to access bar? Does this somehow make the array associative?


I have read How does double arrow (=>) operator work in Perl? but that asks about how it is used in general not specifically in list context.

Community
  • 1
  • 1
terdon
  • 3,260
  • 5
  • 33
  • 57
  • 1
    Nit: No such thing as array context. Arrays can't even be returned by subs. It's list context. – ikegami Feb 26 '14 at 14:53
  • Nit: `=>` is equivalent to a comma with extra quoting powers regardless of context. – ikegami Feb 26 '14 at 14:54
  • @ikegami huh, so it is, I didn't know that `%h=("foo",bar)` is equivalent to `%h=("foo" => bar)`, thanks. Fair enough about array context too, I was trying to avoid repetition, Q edited. – terdon Feb 26 '14 at 15:05
  • Is is, but that's in list context too. – ikegami Feb 26 '14 at 15:18
  • @ikegami ah, yes, of course it is. In my defense, I am a biologist not a programmer :). Thanks for the clarifications. – terdon Feb 26 '14 at 15:19

2 Answers2

3

Is there any other difference apart from the implicit quoting between the two? Are the two parameters passed to login() linked somehow?

No and no.

The material you quoted just talks about documenting paired elements (c.f. self-documenting code). You have to write the code that treats them as pairs in an array or list.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

The point is better made by something like

login(username => $username, password => $password)

which is identical to

login('username', $username, 'password', $password)

but shows the pairing much better.

Borodin
  • 126,100
  • 9
  • 70
  • 144
  • Understood, thanks. My main problem was that I had not interpreted the word _documented_ literally and thought that the elements in the list were somehow linked. – terdon Feb 26 '14 at 15:00