The operator => (fat comma) in Perl

The operator => is used to separate values. It is most commonly used in the description of the Perl hash. Here's an example:

▶ Run

my %h = (
    a => 1,
    b => 2,

The operator => is syntactic sugar. This operator is used instead of the comma operator ,, but unlike the comma operator, it takes the left operand as a string without having to enclose it in quotation marks. Here is a very similar description of a Perl hash, but using a comma:

▶ Run

my %h = (
    'a', 1,
    'b', 2,

In the description of the hash is always better to use the operator => to separate the key-value since this use is a more clear code.

Instead of the comma operator , you can always use => (but not Vice versa). But not always so worth doing. Here is another description of the hash, which works just like the previous two, but do not need:

▶ Run

my %h = (
    a => 1 =>
    b => 2 =>

The operator => is often called the "fat comma" because it acts like a comma (with some addition), but consists of two characters instead of one.

The left operand

The operator => considers a string left operand only if the following conditions are true:

  • the left operand begins with a letter or symbol _
  • and the left operand may contain only letters, numbers and symbols _

So this code will not work (because the left operand is a symbol -):

▶ Run

my %h = (
   some-key => 3, # error!

You must explicitly enter into some-key quoted: 'some-key'.

Use is not in the hash

The operator => in the first place is to use to describe value pairs in the hash. It looks like the arrow and when used in the hash, it looks very organic — the key points to a value. But the operator => can be used not only hashes, but generally in any lists (but not always it makes sense). Here is an example use of this operator in the array:

▶ Run

my @arr = ( one => two => 'three' );

In this example, was created an array which is 3 rows. Because operator has been used =>, the left operand can not be enclosed in quotation marks. Therefore, the first two rows are used without quotes. On the right operand of this magic does not apply, so the third line had to be enclosed in quotes.

This example would be better to write using operator qw:

▶ Run

my @arr = qw(one two three);

The operator need to write together

The operator => consists of two characters and those two characters must be next to each other. It is impossible to place the space between them. In that case if you write = >, it will be a mistake. Here is an example of incorrect code:

▶ Run

my %h = (
    a = > 1, # error!

If you run this code, it will appear on the screen:

syntax error at line 4, near "= >"
Execution of aborted due to compilation errors.

Official documentation

Here's a snippet of the output perldoc perlop about the operator =>:

    The "=>" operator (sometimes pronounced "fat comma") is a synonym for the
    comma except that it causes a word on its left to be interpreted as a
    string if it begins with a letter or underscore and is composed only of
    letters, digits and underscores. This includes operands that might
    otherwise be interpreted as operators, constants, single number v-strings
    or function calls. If in doubt about this behavior, the left operand can
    be quoted explicitly.

    Otherwise, the "=>" operator behaves exactly as the comma operator or list
    argument separator, according to context.

    For example:

        use constant FOO => "something";

        my %h = ( FOO => 23 );

    is equivalent to:

        my %h = ("FOO", 23);

    It is NOT:

        my %h = ("something", 23);

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

        %hash = ( $key => $value );
        login( $username => $password );

    The special quoting behavior ignores precedence, and hence may apply to
    part of the left operand:

        print time.shift => "bbb";

    That example prints something like "1314363215shiftbbb", because the "=>"
    implicitly quotes the "shift" immediately on its left, ignoring the fact
    that "time.shift" is the entire left operand.

Other articles