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";

# constant subs can be constant-folded by the parser

use Path::This 'THISDIR';
my $bar_path = THISDIR . '/bar'; # string formed at parse time

# equivalent values resolved with only core modules
# use constant or BEGIN to resolve __FILE__ as early as possible

use Cwd 'abs_path';
use File::Basename 'dirname';

use constant THISFILE => abs_path __FILE__;
# or
my $THISFILE;
BEGIN { $THISFILE = abs_path __FILE__ }

use constant THISDIR => dirname abs_path __FILE__;
# or
my $THISDIR;
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 defined across 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.

See lib::relative for the specific case of adding paths to @INC relative to the current source file.

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 (& not needed to call it), 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 (& not needed to call it), 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