gpt4 book ai didi

python - 是否有与 Python 的 `if __name__ == ' __main_ _'` 等效的 Perl?

转载 作者:IT老高 更新时间:2023-10-28 22:06:58 25 4
gpt4 key购买 nike

有没有办法确定当前文件是否是在 Perl 源代码中执行的文件?在 Python 中,我们使用以下结构来做到这一点:

if __name__ == '__main__':
# This file is being executed.
raise NotImplementedError

我可以使用 FindBin__FILE__ 一起破解一些东西,但我希望有一种规范的方式来做到这一点。谢谢!

最佳答案

unless (caller) {
print "This is the script being executed\n";
}

caller .它在主脚本中返回 undef。请注意,这在子例程中不起作用,仅在顶级代码中起作用。

关于python - 是否有与 Python 的 `if __name__ == ' __main_ _'` 等效的 Perl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/707022/

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