我试图弄清楚如何在Perl中使用heredoc创建一个简单的HTML文件,但我一直在
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.
我无法弄清问题是什么。这是完整的脚本:
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);
我尝试在EOF
周围使用单引号和双引号。我还试图逃避所有无法帮助的<>
代码。
我应该怎么做以防止此错误?
修改
我知道有些模块可以简化这一过程,但在使用模块简化任务之前,我想知道问题是什么。
编辑2
该错误似乎表明由于结束标记中的/
,Perl将heredoc中的文本视为替换。如果我逃避它们,则错误的一部分会因space between pattern and following word
而消失,但错误的其余部分仍然存在。
答案 0 :(得分:1)
删除<< "EOF";
前面的空格,因为它与文件句柄打印不能很好地交互。
以下是各种工作/非工作变体:
#!/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";
<强>输出强>
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