gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:59:18 29 4
gpt4 key购买 nike

我使用以下语句来获取当前时间。

  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";

关于perl - 如何使用 Perl 的本地时间和 print 来获取时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2591995/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com