2017-04-10 82 views
3

我试图找出如何使用Perl中的定界符来创建一个简单的HTML文件,但我不断收到创建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单引号和双引号尝试。我也尝试转义所有<>标签,但没有帮助。

我该怎么做才能防止这个错误?

编辑

我知道有模块,在那里,将简化这一点,但我想知道是什么问题,与此之前,我简化了模块的任务。

EDIT 2

的错误似乎表明,Perl是看定界符内的文本作为替代由于在结束标记的/。如果我将它们转义出来,那么错误的一部分就会消失,但是其余的错误依然存在。

+6

删除空间后''<<。 – melpomene

+0

谢谢!请做出答案,以便我可以选择它。 – theillien

+1

有了这个空间,它是一个移位操作http://perldoc.perl.org/perlop.html#Shift-Operators,所以Perl将HTML解释为Perl代码 – ysth

回答

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 ([email protected]) { 
    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