4

ヒアドキュメントを適切に機能させるのに苦労しているようです。変数に詰め込む必要があるテキストのチャンクがあり、補間されていません。

ここに私が持っているものがあります:

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

私は何を間違っていますか?

4

2 に答える 2

8

完全なステートメントを作成するには、代入演算子を使用して文字列を割り当てる必要があります。

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
于 2012-09-28T15:36:58.953 に答える
5

=あなたはサインを逃しました:

my $move_func = <<'FUNC';
于 2012-09-28T15:38:20.340 に答える