The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

realpath - make a canonicalized absolute path name

abs2rel - make a relative path from an absolute path

rel2abs - make an absolute path from a relative path

SYNOPSIS

use File::PathConvert qw(realpath abs2rel rel2abs);

$path = realpath($path);

$path = abs2rel($path);
$path = abs2rel($path, $base);

$path = rel2abs($path);
$path = rel2abs($path, $base);

use File::PathConvert qw($resolved);
$path = realpath($path) || die "resolution stopped at $resolved";

DESCRIPTION

The PathConvert module provides three functions.

realpath

realpath makes a canonicalized absolute pathname and resolves all symbolic links, extra ``/'' characters, and references to /./ and /../ in the path. realpath resolves both absolute and relative paths. It returns the resolved name on success, otherwise it returns undef and sets the valiable $File::PathConvert::resolved to the pathname that caused the problem.

All but the last component of the path must exist.

This implementation is based on 4.4BSD realpath(3).

abs2rel

abs2rel makes a relative path name from an absolute path name. By default, the base is the current directory. If you specify a second parameter, it's assumed to be the base.

The returned path may include symbolic links. abs2rel doesn't check whether or not any path exists.

rel2abs

rel2abs makes an absolute path name from a relative path name. By default, the base directory is the current directory. If you specify a second parameter, it's assumed to be the base.

The returned path may include symbolic links. abs2rel doesn't check whether or not any path exists.

EXAMPLES

realpath
If '/sys' is a symbolic link to '/usr/src/sys':

chdir('/usr');
$path = realpath('../sys/kern');

or in anywhere ...

$path = realpath('/sys/kern');

yields:

$path eq '/usr/src/sys/kern'
abs2rel
chdir('/usr/local/lib');
$path = abs2rel('/usr/src/sys');

or in anywhere ...

$path = abs2rel('/usr/src/sys', '/usr/local/lib');

yields:

$path eq '../../src/sys'

Similarly,

$path1 = abs2rel('/usr/src/sys', '/usr');
$path2 = abs2rel('/usr/src/sys', '/usr/src/sys');

yields:

$path1 eq 'src/sys'
$path2 eq '.'
rel2abs
chdir('/usr/local/lib');
$path = rel2abs('../../src/sys');

or in anywhere ...

$path = rel2abs('../../src/sys', '/usr/local/lib');

yields:

$path eq '/usr/src/sys'

Similarly,

$path = rel2abs('src/sys', '/usr');
$path = rel2abs('.', '/usr/src/sys');

yields:

$path eq '/usr/src/sys'

BUGS

If the base directory includes symbolic links, abs2rel produces the wrong path. For example, if '/sys' is a symbolic link to '/usr/src/sys',

$path = abs2rel('/usr/local/lib', '/sys');

yields:

$path eq '../usr/local/lib'		# It's wrong!!

You should convert the base directory into a real path in advance.

$path = abs2rel('/sys/kern', realpath('/sys'));

yields:

$path eq '../../../sys/kern'	# It's correct but ...

That is correct, but a little redundant. If you wish get the simple answer 'kern', do the following.

$path = abs2rel(realpath('/sys/kern'), realpath('/sys'));

realpath assures correct result, but don't forget that realpath requires that all but the last component of the path exist.

AUTHOR

Shigio Yamaguchi <shigio@wafu.netgate.net>

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 289:

You forgot a '=back' before '=head1'

Around line 291:

'=item' outside of any '=over'