2017-04-10 16 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

私はこれを簡素化しますそこにモジュールがあります知っているが、私はモジュールでタスクを簡素化する前に、問題がこれであるかを知りたいと思います。

EDIT 2

エラーはPerlが終了タグで/による代用としてヒアドキュメント内のテキストを見ていることを示していると思われます。私がそれらをエスケープすれば、エラーの一部はspace between pattern and following wordに関して消え去るが、残りのエラーは残る。

+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 
関連する問題