30

I know about the hash use of the => operator, like this

$ cat array.pl
%ages = ('Martin' => 28,
         'Sharon' => 35,
         'Rikke' => 29,);

print "Rikke is $ages{Rikke} years old\n";
$ perl array.pl
Rikke is 29 years old
$

and I thought it was just syntax to initialize hashes, but in answers to How can I qualify a variable as const/final in Perl?, => has been used like this

use Readonly;
Readonly my $infilename => "input_56_12.txt";

What exactly does => mean? Are there more ways in which => can be used?

Community
  • 1
  • 1
Lazer
  • 79,569
  • 109
  • 264
  • 349
  • 8
    Fair enough. In my defense, I did Google, which rejected searching for `=>`. – Lazer Nov 04 '10 at 16:20
  • 6
    And a few years later we have [symbolhound](http://symbolhound.com/?q=perl+%3D%3E) for such searches. Oddly enough, one of the top results is ... this question :) – mu is too short Jun 29 '13 at 03:41
  • 2
    I've just been forced into reading Perl scripts for the first time and this question came up. It's been helpful. – John Phu Nguyen Jun 07 '17 at 02:02
  • This is really esoteric stuff. Wish there was a book about cool stuff like this. Who knows, maybe there is one already. – User9102d82 Aug 07 '18 at 17:09

2 Answers2

40

The => operator in perl is basically the same as comma. The only difference is that if there's an unquoted word on the left, it's treated like a quoted word. So you could have written Martin => 28 which would be the same as 'Martin', 28.

You can make a hash from any even-length list, which is all you're doing in your example.

Your Readonly example is taking advantage of Perl's flexibility with subroutine arguments by omitting the parenthesis. It is equivalent to Readonly(my $infilename, "input_56_12.txt"). Readonly is a function exported by the Readonly module which takes two arguments: a reference, and a value. The internals of Readonly are worthy of another question if you want to understand them.

Here's an example of using it as a comma in an unexpected way:

$ perl -e 'print hello => "world\n"'
helloworld
Ben Jackson
  • 78,375
  • 8
  • 86
  • 141
  • So, what does `Readonly my $infilename => "input_56_12.txt";` do? – Lazer Nov 04 '10 at 06:38
  • It is just a comma. You could rewrite it as : `Readonly(my $infilename, "input_56_12.txt");`. For further details on the Readonly module (an alternative to constant), please refer to http://search.cpan.org/perldoc?Readonly – OMG_peanuts Nov 04 '10 at 08:26
36

From perlop:

The => operator is a synonym for the comma except that it causes its left operand 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 behaviour, 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 );

From PBP:

I have found some good information from Perl Best Practices about Fat Commas => and i think it should be nice to mention over here too.

It's better to reserve the fat comma exclusively for the following things:-

Use it when constructing a hash:

my %h = ( FOO => 23 );

or when passing named arguments to a subroutine ie.,

$text = format_text({FOO => 23, BAR => 30});

or when creating a constant:

 Readonly my $FOO => "23";

For more detail see the Chapter4:Values and Expressions (Fat Commas) section of Perl Best Practices.

CJ7
  • 20,640
  • 59
  • 173
  • 305
Nikhil Jain
  • 7,912
  • 2
  • 23
  • 46