0

I've defined a path (say, $MYPAT) in .bashrc file and I would like to use it in perl scripts, so that inside the script I just can write open(IN, "<$MYPAT/dir1/... How can I import this variable to directly use it in my script?

I guess is the same problem posted here How to use aliases defined in .bashrc in shell script but applied to perl.

Thank you!

4

2 回答 2

3

假设$MYPAT是在您的 .bashrc 中定义的环境变量,您可以使用%ENVperl 中的特殊哈希访问它,其中包含您进程的所有环境变量。见perlvar%ENV_

my $path = $ENV{MYPAT};

此外,作为文体说明:

  1. 您应该使用词法文件句柄而不是全局变量
  2. 您应该使用 open 的三参数形式。

所以而不是

open(IN, "<$path/dir1/...") 

做那个

open my $fh, '<', "$path/dir1/..." or die "yadda yadda";
于 2013-07-02T17:18:05.647 回答
1

每项工作都有很多模块,使之成为可能。我认为 CPAN 中的这个模块会对您有所帮助,只需阅读一些文档即可。

Shell::Source
于 2013-07-10T05:31:23.970 回答