4.14. Fat Commas
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. For example, use it when constructing a hash:
%default_service_record = (
name => '<unknown>',
rank => 'Recruit',
serial => undef,
unit => ['Training platoon'],
duty => ['Basic training'],
); or when passing named arguments to a subroutine (see Chapter 9):
$text = format_text(src=>$raw_text, margins=>[1,62], justify=>'left'); or when creating a constant:
Readonly my $ESCAPE_SEQ => "\N{DELETE}\N{ACKNOWLEDGE}\N{CANCEL}Z"; The fat comma visually reinforces the connection between the name and the following value. It also removes the need to quote the key string, as long as you use only valid Perl identifiers[*] as keys. Compare the readability of the previous examples with the following comma-only versions:
%default_service_record = ( 'name', '<unknown>', 'rank', 'Recruit', 'serial', undef, 'unit', ['Training platoon'], 'duty', ['Basic training'], ); $text = format_text('src', $raw_text, 'margins', [1,62], 'justify', 'left'); Readonly my $ESCAPE_SEQ, "\N{DELETE}\N{ACKNOWLEDGE}\N{CANCEL}Z"; An alternative criterion that is sometimes used when considering a => is whether you can pronounce the symbol as some kind of process verb, such as "becomes" or "produces" or "implies" or "goes into" or "is sent to". For example: # The substring of $name becomes whatever's in $new_name substr $name, $from, $len => $new_name; # Send this signal to this process send_signal($signal => $process); # Open a handle to a particular file open my $binary => '<:raw', $filename or croak "Can't open '$filename': $OS_ERROR"; Underlying this approach is the idea of using the prominence of the fat comma to mark the boundary of two distinct subsets within an argument list. At the same time, the arrow-like appearance of the operator is supposed to convey a sense of moving or changing or mapping values. The problem here is that it's far too easy to misinterpret the direction and destination of the "movement" being represented. For example: # The substring of $name GOES INTO $new_name (No it doesn't!) substr $name, $from, $len => $new_name; # Open a handle GOING OUT TO a particular file (No it won't!) open my $binary => $filename; Moreover, the original pronunciation-based criterion for using a fat comma can easily be forgotten. Thereafter, the => is likely to be used indiscriminately, often counter-intuitively, and occasionally as a kind of "wish-fulfillment operator": # This may or may not send the signal to the process # (depending on the order in which send_msg( ) expects its arguments) send_msg($signal => $process); # This doesn't find the index of the target in the text (it's vice versa) $found_at = index $target => $text; # An excellent money-making plan ... for the casino push @casino_money => @my_wallet; Considering the potential for confusion, it's better to reserve the fat comma exclusively for hash entries, named arguments, and other name/value pairs. |