如何确定符号链接的 perl 脚本的位置? [英] How to determine location of perl script that was symbolic linked?

查看:49
本文介绍了如何确定符号链接的 perl 脚本的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间在 SE 上寻找解决方案,但还没有遇到这个问题.

I've spent quite a bit of time looking for a solution on SE, but haven't come across this issue.

我有一个 Perl 脚本SCRIPT",它可以安装在不同的位置,但它总是读取本地文件(与脚本在同一个目录中).如果用户直接调用脚本(a la ../../foo/SCRIPT),一切都按预期工作.Perl 脚本查找本地文件.

I have a Perl script "SCRIPT" that can be installed in different locations, but it always reads in local files (in the same dir as the script). If the user invokes the script directly (a la ../../foo/SCRIPT) everything works as expected. The Perl script finds the local files.

但是,如果用户创建到 Perl 脚本的符号链接 (ln -s my_script ../../foo/SCRIPT),并运行 sym_link,则 sym_linked Perl 脚本会在当前目录中查找文件而不是脚本原始位置的本地目录.

But, if the user creates a symbolic link to the Perl script (ln -s my_script ../../foo/SCRIPT), and runs the sym_link, the sym_linked Perl script is looking in the current dir for the files and not the dir local to the original location of the script.

我想弄清楚如何确定 Perl 脚本实际驻留的路径.然后我会将它放在我想读入的文件的前面.有什么想法吗?

I'm trying to figure out how to determine the path that the Perl script actually resides in. Then I would prepend that onto the front of the files I want to read in. Any ideas?

我试过调用以下方法--

I've tried calling the following--

  • 目录名(__FILE__)
  • abs_path()
  • cwd
  • 目录名(abs_path($0))
  • Cwd:cwd()
  • Cwd::abs_rath()
  • File:Basename::fileparse($0)
  • Cwd:realpath($0)

但它们都返回链接的路径,而不是实际的 perl 脚本.

but they all return the path to the link, and not the actual perl script.

推荐答案

use FindBin qw( $RealBin );

请参阅 perldoc FindBin.FindBin 是一个标准模块,与 Perl 一起安装.

See perldoc FindBin. FindBin is a standard module and is install with Perl.

这篇关于如何确定符号链接的 perl 脚本的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆