How to use __FILE__ inside qx?

I am trying to assign a constant at the top of a Perl script as follows:

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

__FILE__does not interpolate inside the operator qx, which leads to a failure. How can I achieve what I want to interpolate __FILE__before calling the readlinkshell.

Please note: this is not an option to save the command inside an intermediate variable between them.

+3
source share
3 answers

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

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

A few comments made in the comments:

  • List context []. It does not matter here, but it is worth knowing in general.
  • , __FILE__ .

- perl script , FindBin module. FindBin perl ( 5.004 http://search.cpan.org/~chips/perl5.004/).

+7

All-Perl:

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

readpipe , qx//. Perl qx// qq//, readpipe. qx// , readpipe .

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

quotemeta .

+2

All Articles