Fat comma
This article is an orphan, as few or no other articles link to it. Please introduce links to this page from related articles; suggestions are available. (September 2008) |
Fat comma is a programming term primarily associated with the Perl and Ruby programming languages, though others doubtless also employ the token. It refers to an operator, indicated as “=>
”, that can be used as a substitute for the comma operator and is an example of good idiomatic Perl when used to bind key-value pairs in a hash.[1]
# a typical, idiomatic use of the fat comma in Perl
my %newHash = ( first_name => "Tom", last_name => "Auger" );
Subtleties
When working under strict mode (a solid programming practice that forces the developer to adhere to the highest standards of consistency, typing and declaration available to the language's compiler), the fat comma has one advantage over its comma (“,
”) analogue: it forces the word to its left to be interpreted as a string.[2]
Thus, where
use strict; # for the love of Larry, use strict!
my %badHash = ( bad_bareword, "not so cool" );
would produce a run-time error (barewords are not allowed under strict), the following use of the fat comma
use strict;
my %goodHash = ( converted_to_string => "very monkish" );
would be legal and idiomatic, because the token converted_to_string
would be converted to the string literal "converted_to_string"
which is a legal argument in a hash key assignment.
The result is easier-to-read code, with a stronger emphasis on the name-value pairing of associative arrays.
References
- ↑ Conway, Damian. "4: Values and Expressions". in Allison Randal and Tatiana Appandi. Perl Best Practices. O'Reilly Media, Inc.. pp. 66. ISBN 0-596-00173-8. "Whenever you are creating a list of key/value or name/value pairs, use the "fat comma" (=>) to connect the keys to their corresponding values."
- ↑ perldoc.perl.org – perlop – Comma Operator
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...