Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In perl, can HEREIS notation be used inside hash initialization?

I am trying to initialize a hash like this:

use strict;

my %hash =
(
    key => <<END;
abc
def
END
    ,
    another_key => 17
);

When I run perl -cw on this code, I get the error 'syntax error at hash-initialize-test.pl line 5, near ";"'.

Is there a way to use HEREIS notation (such as <<END;) within hash initialization? If not, why not?

There are several simple workarounds, but I like to use HEREIS notation for multi-line strings because it is elegant and avoids introducing unnecessary variables.

like image 475
David Levner Avatar asked Jan 04 '15 22:01

David Levner


1 Answers

Remove the semicolon. There is no statement end.

my %hash = ( key => <<'END',
abc
def
END
             another_key => 17,
           );
like image 77
choroba Avatar answered Sep 25 '22 16:09

choroba