简体   繁体   中英

Perl => operator in list context

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.

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 (cf self-documenting code ). You have to write the code that treats them as pairs in an array or list.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM