How does double arrow (=>) operator work in Perl?

后端 未结 2 1123
萌比男神i
萌比男神i 2020-11-29 06:42

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

$ cat array.pl
%ages = (\'Martin\' => 28,
         \'Sharon\' => 35,
         \         


        
2条回答
  •  一个人的身影
    2020-11-29 06:51

    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
    

提交回复
热议问题