Fat comma
Encyclopedia
Fat comma is a programming
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

 term primarily associated with the Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 and Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

 programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s, though others doubtless also employ the token. It refers to an operator
Operator (programming)
Programming languages typically support a set of operators: operations which differ from the language's functions in calling syntax and/or argument passing mode. Common examples that differ by syntax are mathematical arithmetic operations, e.g...

, 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
Hash table
In computer science, a hash table or hash map is a data structure that uses a hash function to map identifying values, known as keys , to their associated values . Thus, a hash table implements an associative array...

. In Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

, it is also called a hash rocket
  1. a typical, idiomatic use of the fat comma in Perl

my %newHash = ( first_name => "Tom", last_name => "Auger" );

Perl

When working under strict mode in Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 (a programming practice that forces the developer to adhere to the highest standards of consistency, typing and declaration available to the language's compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

), the fat comma has one advantage over its comma (“,”) analogue: it forces the word to its left to be interpreted as a string.

Thus, where this would produce a run-time error (barewords are not allowed under strict):

use strict;
my %badHash = ( bad_bareword, "not so cool" );

the following use of the fat comma would be legal and idiomatic:

use strict;
my %goodHash = ( converted_to_string => "very monkish" );

This is because the token converted_to_string would be converted to the string literal "converted_to_string" which is a legal argument
Parameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...

 in a hash key assignment.
The result is easier-to-read code, with a stronger emphasis on the name-value pairing of associative array
Associative array
In computer science, an associative array is an abstract data type composed of a collection of pairs, such that each possible key appears at most once in the collection....

s.

Ruby

In Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

, the fat comma is the token to create hashes. Ruby 1.9 introduced a special syntax when using symbols as keys, giving Ruby hashes a syntax similar to JSON
JSON
JSON , or JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects...

markup.
  1. Old syntax

old_hash = { :name => 'Ruby', :influences => ['Perl', 'Python', 'Smalltalk'] }
  1. New syntax (Ruby 1.9 only)

new_hash = { name: 'Ruby', influences: ['Perl', 'Python', 'Smalltalk'] }
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK