How to use __FILE__ inside qx?

812 views Asked by At

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 achieve 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.

3

There are 3 answers

5
Andy On BEST ANSWER

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/).

0
Ven'Tatsu On

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.

1
shawnhcorey On

An all-Perl solution:

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