如何使用Perl的本地时间与print来获取时间戳?

前端之家收集整理的这篇文章主要介绍了如何使用Perl的本地时间与print来获取时间戳?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下语句来获取当前时间。
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";
原文链接:https://www.f2er.com/Perl/173174.html

猜你在找的Perl相关文章