如何获取正在执行的Perl脚本的完整路径?

编程入门 行业动态 更新时间:2024-10-25 18:27:12
本文介绍了如何获取正在执行的Perl脚本的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有Perl脚本,需要在执行期间确定脚本的完整路径和文件名.我发现,取决于脚本的调用方式,$0有所不同,有时包含fullpath+filename,有时仅包含filename.因为工作目录也可能变化,所以我想不出一种可靠地获取脚本fullpath+filename的方法.

I have Perl script and need to determine the full path and filename of the script during execution. I discovered that depending on how you call the script $0 varies and sometimes contains the fullpath+filename and sometimes just filename. Because the working directory can vary as well I can't think of a way to reliably get the fullpath+filename of the script.

有人解决了吗?

推荐答案

有几种方法:

  • $0 是POSIX提供的当前执行脚本,相对于当前工作目录(如果脚本位于CWD或以下)
  • 另外,cwd(),getcwd()和abs_path()由 Cwd
  • 模块 FindBin 提供了$Bin&通常是执行脚本的路径的$RealBin变量;此模块还提供$Script& $RealScript是脚本的名称
  • __FILE__ 是Perl解释器在处理期间处理的实际文件编译,包括其完整路径.
  • $0 is the currently executing script as provided by POSIX, relative to the current working directory if the script is at or below the CWD
  • Additionally, cwd(), getcwd() and abs_path() are provided by the Cwd module and tell you where the script is being run from
  • The module FindBin provides the $Bin & $RealBin variables that usually are the path to the executing script; this module also provides $Script & $RealScript that are the name of the script
  • __FILE__ is the actual file that the Perl interpreter deals with during compilation, including its full path.

我看过前三个( $0 , Cwd 模块和 FindBin 模块)在mod_perl下失败,产生了毫无价值的输出,例如'.'或空字符串.在这种环境下,我使用 __FILE__ 并使用 File::Basename 模块:

I've seen the first three ($0, the Cwd module and the FindBin module) fail under mod_perl spectacularly, producing worthless output such as '.' or an empty string. In such environments, I use __FILE__ and get the path from that using the File::Basename module:

use File::Basename; my $dirname = dirname(__FILE__);

更多推荐

如何获取正在执行的Perl脚本的完整路径?

本文发布于:2023-11-10 02:01:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   路径   完整   Perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!