gpt4 book ai didi

perl - 在终端窗口中运行脚本并定义对象值

转载 作者:行者123 更新时间:2023-12-02 00:59:22 26 4
gpt4 key购买 nike

当我运行 Perl 脚本时,我在我的 Mac 上打开终端窗口并在移动到包含 Perl 脚本的文件夹后写入“perl test1.pl”。

我经常发现自己想多次运行同一个 Perl,通常是同时运行,但变化非常小。

例如,这个 Perl 脚本“test1.pl”如下所示:

$year = 2001;
<rest of code that uses $year>

我想执行 "test1.pl"where $year = 2001, where $year = 2002, etc. 为此我通常运行脚本 where $year = 2001, 然后调整脚本使 $year = 2002,保存,打开新的终端窗口,再次运行,重复。

有没有办法通过在终端窗口中指定 $year 的值来提交 Perl 脚本?

我在想:

perl test1.pl, $year = 2001

最佳答案

(许多方法中的)一种方法是:

my $year = (@ARGV) ? shift : 2001;

然后运行它:

perl test1.pl 2002

如果您不在命令行中指定年份,则默认为 2001 年。另请参阅:perldoc -v @ARGV

关于perl - 在终端窗口中运行脚本并定义对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533685/

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