Generating HTML file via heredoc in Perl fails with Bareword error

I am trying to figure out how to create a simple HTML file using heredoc in Perl, but I keep getting

Bareword found where operator expected at pscratch.pl line 12, near "<title>Test"
    (Missing operator before Test?)
Having no space between pattern and following word is deprecated at pscratch.pl line 13.
syntax error at pscratch.pl line 11, near "head>"
Execution of pscratch.pl aborted due to compilation errors.

      

I can't figure out what the problem is. This is the script in general:

use strict;
use warnings;

my $fh;
my $file = "/home/msadmin1/bin/testing/html.test";

open($fh, '>', $file) or die "Cannot open $file: \n $!";

print $fh << "EOF";
<html>
  <head>
    <title>Test</title>
  </head>

  <body>
    <h1>This is a test</h1>
  </body>
</html>
EOF

close($fh);

      

I've tried using single and double quotes around EOF

. I also tried to avoid all the tags <>

that didn't help.

What should I do to prevent this error?

EDIT

I know there are modules out there that will simplify this, but I would like to know what is the problem with this before I simplify things with a module.

EDIT 2

It looks like the error is indicating that Perl is looking at the text in the heredoc as a replacement due /

to the closing tags. If I move away from them, some of the error will go away relatively space between pattern and following word

, but the rest of the error will remain.

+3


source to share


1 answer


Remove the space in front << "EOF";

as it doesn't play well with printing the file descriptor.

Here are the various working / non-working options:

#!/usr/bin/env perl

use warnings;
use strict;

my $foo = << "EOF";
OK: with space into a variable
EOF

print $foo;

print <<"EOF";
OK: without space into a regular print
EOF

print << "EOF";
OK: with space into a regular print
EOF

open my $fh, ">foo" or die "Unable to open foo : $!";
print $fh <<"EOF";
OK: without space into a filehandle print
EOF

# Show file output
close $fh;
print `cat foo`;

# This croaks
eval ' 
print $fh << "EOF";
with space into a filehandle print
EOF
';
if ($@) {
    print "FAIL: with space into a filehandle print\n"
}

# Throws a bitshift warning:
print "FAIL: space and filehandle means bitshift!\n";
print $fh << "EOF";
print "\n";

      



Output

OK: with space into a variable
OK: without space into a regular print
OK: with space into a regular print
OK: without space into a filehandle print
FAIL: with space into a filehandle print
FAIL: space and filehandle means bitshift!
Argument "EOF" isn't numeric in left bitshift (<<) at foo.pl line 42.
152549948

      

+1


source







All Articles