NAME

Path::This - Path to this source file or directory

SYNOPSIS

  use Path::This '$THISFILE';
  print "This file is $THISFILE\n";

  use Path::This '$THISDIR';
  use lib "$THISDIR/../lib";

  # using core modules - use constant or BEGIN to resolve __FILE__ at compile time
  use Cwd 'abs_path';
  use File::Basename 'dirname';
  use constant THISFILE => abs_path __FILE__;
  use constant THISDIR => dirname abs_path __FILE__;
  my ($THISFILE, $THISDIR);
  BEGIN { $THISFILE = abs_path __FILE__ }
  BEGIN { $THISDIR = dirname abs_path __FILE__ }

DESCRIPTION

Exports package variables by request that represent the current source file or directory containing that file. Dynamic or constant sub versions can also be requested. Paths will be absolute with symlinks resolved.

Note that the package variable or constant sub will be exported to the current package globally. If the same package will be used in multiple files, use the dynamic sub export so the file path will be calculated when the sub is called.

For cases where this module cannot be loaded beforehand, the last section of the "SYNOPSIS" shows how to perform the same task with core modules.

EXPORTS

$THISFILE

&THISFILE

THISFILE

  print "$THISFILE\n";
  my $file = THISFILE;

Absolute path to the current source file. Behavior is undefined when called without a source file (e.g. from the command line or STDIN). $THISFILE will export a package variable, &THISFILE will export a dynamic subroutine, and THISFILE will export an inlinable constant.

$THISDIR

&THISDIR

THISDIR

  print "$THISDIR\n";
  my $dir = THISDIR;

Absolute path to the directory containing the current source file, or the current working directory when called without a source file (e.g. from the command line or STDIN). $THISDIR will export a package variable, &THISDIR will export a dynamic subroutine, and THISDIR will export an inlinable constant.

BUGS

Report any issues on the public bugtracker.

AUTHOR

Dan Book <dbook@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2019 by Dan Book.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

SEE ALSO

FindBin, Dir::Self, lib::relative