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 in this code, I get a syntax error at hash-initialize-test.pl line 5, next to ";".

Is there a way to use HEREIS notation (for example <<END;

) in hash initialization? If not, why not?

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

+3


source to share


1 answer


Remove the semicolon. There is no end to the statement.



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

      

+6


source







All Articles