我使用以下语句来获取当前时间。
print "$query executed successfully at ",localtime; print "$query executed successfully at ",(localtime); print "$query executed successfully at ".(localtime);
executed successfully at 355516731103960 executed successfully at 355516731103960 executed successfully at Wed Apr 7 16:55:35 2010
前两个语句不是以日期格式打印当前时间。
第三个声明只给出日期格式的正确输出。
我的理解是第一个在标量上下文中返回一个值,所以它返回数字。
解决方法
也许最重要的事情你可以学习编程在Perl,是上下文。许多内置子程序和运算符根据上下文的不同而有所不同。
print "$query executed successfully at ",localtime,"\n"; # list context print "$query executed successfully at ",(localtime),"\n"; # list context print "$query executed successfully at ". localtime,"\n"; # scalar context print "$query executed successfully at ".(localtime),"\n"; # scalar context print "$query executed successfully at ",scalar localtime,"\n"; # scalar context print "$query executed successfully at ",scalar (localtime),"\n"; # scalar context
这可以通过拆分语句来更清楚。
my $time = localtime; # scalar context print "$query executed successfully at $time\n"; my @time = localtime; # list context print "$query executed successfully at @time\n";