2017-09-27 35 views
1

我正在調用perl子例程,並將路徑作爲來自unix CLI模式的參數。 但是獲得Bareword發現錯誤。無論如何要通過這個。無法使用路徑作爲參數從bash調用perl子例程

[[email protected] ~/test]**$perl -e "require qw(./burt.pm) ;file(/u/path,/u/build/);"** 
Bareword found where operator expected at -e line 1, near "/u/path" 
    (Missing operator before path?) 
Bareword found where operator expected at -e line 1, near "/u/build" 
    (Missing operator before build?) 
syntax error at -e line 1, near "/u/path" 
Execution of -e aborted due to compilation errors. 


    [[email protected] ~/test]$ cat burt.pm 
    #!/usr/software/bin/perl5.8.8 
    use strict; 
    sub file 
    { 
     my ($path1, $path2) = @_; 
     print "path1 $path1\n"; 
     print "path2 $path2\n"; 
    } 
    1; 
+1

在你的字符串周圍加上引號。 – simbabque

+0

請注意,我嘗試向路徑添加雙引號。無法成功 –

+1

雙引號不會嵌套。使用單引號或q()或qq()。 – choroba

回答

4

您需要引用您的字符串。您不能使用雙引號""來做到這一點,因爲您已經將它們用於-e標誌的shell參數。改爲使用單引號''或引用運算符qqq

$ perl -e "require 'burt.pm'; file('/u/path', '/u/build');" 

這是一般一個好主意,在單行使用q所以不會與shell引用干涉。

在任何情況下,在Linux系統上,您可能希望使用單引號''作爲-e之後的Perl程序,因爲雙引號""可以啓用shell的引用機制。

在Perl,雙引號""轉插值,所以變量和特殊字符得到填補我會寫你的程序是這樣的:

$ perl -e 'require q{burt.pm}; file(q{/u/path}, q{/u/build});' 

還要注意,如果沒有strict編譯Perl程序將處理裸詞作爲字符串,但斜槓/不是裸詞的一部分。 Perl可能認爲有一個模式匹配或一個分割正在進行,在這種情況下這是一個語法錯誤。

+0

請注意,對於Perl模塊('.pm'文件)來說,命名約定是第一個字母是captial,其餘的很小。基本上'ucfirst'駱駝案件。所有小寫​​名稱都保留給'strict','warnings','re'或'utf8'等編譯指示。 – simbabque

相關問題