(According to https://stackoverflow.com/a/17479551/6607497 it should work, but doesn't) I have some code like this:
use strict;
use warnings;
if (open(my $fh, '>', '/tmp/test')) {
print $fh << 'TAG';
BEGIN {
something;
}
TAG
close($fh);
}
If I leave out $fh (which is a file handle opened for output, BTW), the BEGIN block is output correctly (to STDOUT).
However when I add $fh, Perl (5.18, 5.26) tried to execute something which causes an run-time error:
Bareword "something" not allowed while "strict subs" in use at /tmp/heredoc2.pl line 6.
syntax error at /tmp/heredoc2.pl line 9, near "FOO
close"
Execution of /tmp/heredoc2.pl aborted due to compilation errors.
What is wrong?