2015-06-26 121 views
0

我一直在試圖弄清楚如何運行這個例子一段時間,而我仍然堅持如何打印日期。以下是我正在處理的示例。獲取perl腳本的錯誤輸出。

(The Script for Unix/Linux) 
# Backquotes and command substitution 
1 print "The date is ", 'date';  # Windows users: 'date /T' 
2 print "The date is 'date'", ".\n"; # Backquotes treated literally 
3 $directory='pwd';     # Windows users: 'cd' 
4 print "\nThe current directory is $directory."; 

(Output) 
1 The date is Mon Jun 25 17:27:49 PDT 2007. 
2 The date is 'date'. 
4 The current directory is /home/jody/ellie/perl. 

這是我的工作和輸出。

print "The date is ", 'date /T'; 
print "The date is 'date'", ".\n"; 
$directory='cd'; 
print "\nThe current directory is $directory."; 

(Output) 
The date is date /TThe date is 'date'. 
The current directory is cd. 

任何幫助,這是非常感謝。謝謝。

回答

1

你必須改用單引號的反引號:

print "The date is ", `date /T`; 
print "The date is ", `date`, ".\n"; 
$directory=`cd`; 
print "\nThe current directory is $directory."; 
+1

因爲類似的問題,當我第一次嘗試孩子的時候,它不鼓勵我編程,所以我會添加這樣的建議:美國鍵盤上的[backtick](https://en.wiktionary.org/wiki/backtick) ESC下面的按鈕。在其他佈局上,可能很難找到。在例如一個德語單詞,它是退格鍵左邊的按鈕,在按住Shift鍵的同時按下空格鍵,然後按空格,因爲它的意思是[重音符號,叫做重音符號](https://en.wikipedia.org/?title= Grave_accent),也可以放在一個字母上,比如'è'。如果沒有幫助,請從這裏複製:'\'' – simbabque

+1

或使用'qx()',它更易於輸入,甚至可能看起來更好。 –

2

你已經擁有你做錯了什麼(用單引號代替反引號或qx(...))的一個很好的解釋,但它可能是值得指出的您不需要在示例中的兩種情況下調用外部程序。

要獲取當前日期,請在標量上下文中調用localtime

print scalar localtime; 

對於更復雜的日期和時間處理看Time::PieceDateTime。請使用Cwd

use Cwd; 

print getcwd; 

由於兩個原因,不運行不必要的外部程序是一個好主意。首先它使你的代碼更加便攜,其次它更高效(外部程序運行在一個新的shell環境中 - 並且啓動其中一個是相對昂貴的操作)。