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.
Remove the semicolon. There is no statement end.
my %hash = ( key => <<'END',
abc
def
END
another_key => 17,
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With