2012-09-28 82 views
2

我似乎很難讓我的文檔正常工作。我有一大堆文本需要塞入一個變量並保持不插入。Perl在這裏 - 文檔沮喪

這是我有:

my $move_func <<'FUNC'; 
function safemove 
{ 
    if [[ ! -f $1 ]] ; then echo "Source File Not Found: $1"; return 1; fi 
    if [[ ! -r $1 ]] ; then echo "Cannot Read Source File: $1"; return 2; fi 
    if [[ -f $2 ]] ; then echo "Destination File Already Exists: $1 -> $2"; return 3; fi 
    mv $1 $2 
} 
FUNC 

# Do stuff with $move_func 

這給了我

Scalar found where operator expected at ./heredoc.pl line 9, near "$1 $2" 
     (Missing operator before $2?) 
Semicolon seems to be missing at ./heredoc.pl line 10. 
syntax error at ./heredoc.pl line 6, near "if" 
syntax error at ./heredoc.pl line 10, near "$1 $2 
" 
Execution of ./heredoc.pl aborted due to compilation errors. 

但是,按預期以下工作:

print <<'FUNC'; 
function safemove 
{ 
    if [[ ! -f $1 ]] ; then echo "Source File Not Found: $1"; return 1; fi 
    if [[ ! -r $1 ]] ; then echo "Cannot Read Source File: $1"; return 2; fi 
    if [[ -f $2 ]] ; then echo "Destination File Already Exists: $1 -> $2"; return 3; fi 
    mv $1 $2 
} 
FUNC 

我在做什麼錯?

+2

(不分配,''<<是解釋爲左移操作符。) – ikegami

+0

啊!在這裏同樣的問題 - 只是傾銷我的錯誤,以改善搜索:「在void context中無用的左移bitshift(<<)」,「參數」EOF「在左移bitshift中不是數字」,「Use of uninitialized value $ TESTVAR in left bitshift「,」使用未初始化的值$ TESTVAR串聯(。)或字符串「。 – sdaau

回答

6

您需要使用賦值運算符,形成一個完整的語句來指派字符串:

my $move_func = <<'FUNC'; 
function safemove 
{ 
    if [[ ! -f $1 ]] ; then echo "Source File Not Found: $1"; return 1; fi 
    if [[ ! -r $1 ]] ; then echo "Cannot Read Source File: $1"; return 2; fi 
    if [[ -f $2 ]] ; then echo "Destination File Already Exists: $1 -> $2"; return 3; fi 
    mv $1 $2 
} 
FUNC 

# Do stuff with $move_func 
+5

Sonofabitch,我不敢相信我錯過了那個。謝謝您的幫助! –

+0

沒關係 - 它非常微妙! :-) –

3

你錯過了=標誌:

my $move_func = <<'FUNC';