2015-06-26 120 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环境中 - 并且启动其中一个是相对昂贵的操作)。