开发者

How to use __FILE__ inside qx?

I'm trying to assign a constant at the top of a Perl script like so:

use constant {
  # ...
  CONSTNAME => qx{readlink -e __FILE__} || __FILE__,
  # ...
};

__FILE__ does not get interpolated inside the qx operator, which causes this to fail. How can I ac开发者_JAVA技巧hieve what I want, which is to interpolate the __FILE__ before calling readlink of the shell.

Please note: it is not an option to store the command inside an intermediate variable in between.


To answer the question directly, you can use the interpolate-arbitrary-expression-in-string idiom described in perlref:

print qx{echo @{[ __FILE__ ]}};

A couple caveats raised in the comments:

  • The [] supplies list context. That doesn't matter here, but is worth being aware of in general.
  • You can get in trouble if __FILE__ has shell metacharacters.

If your goal is to find the path of your perl script with all symlinks resolved, you may want to look at the FindBin module. FindBin has been part of the core perl distribution for some time (at least since 5.004 according to http://search.cpan.org/~chips/perl5.004/).


An all-Perl solution:

my $self = $0;
while( -l $self ){
  $self = readlink $self;
}
use constant {
  # ...
  CONSTNAME => $self,
  # ...
};


Use readpipe the function equivalent of qx//. Internal to Perl qx// mostly behaves like qq// and then passes the result to readpipe. When the quoting behavior of qx// gets in the way you can bypass it and call readpipe directly.

use constant {
    CONSTNAME => readpipe('readlink -e ' . quotemeta(__FILE__)) || __FILE__,
};

quotemeta will help to guard against possible shell metacharacter attacks in the file name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜