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

$path = realpath($path);

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

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

DESCRIPTION

The PathConvert module provides three functions.

realpath

realpath make a canonicalized absolute pathname. The realpath resolves all symbolic links, extra ``/'' characters and references to /./ and /../ in the path. The realpath will resolve both absolute and relative paths. realpath return resolved name on success else undef and set valiable $resolved to pathname which caused the problem.

All but the last component of the path must exist.

This implementation based on 4.4BSD realpath(3).

abs2rel

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

Returned path may include symbolic links. abs2rel doesn't check whether any path exist or not.

rel2abs

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

Returned path may include symbolic links. abs2rel doesn't check whether any path exist or not.

EXAMPLES

realpath
If '/sys' is symbolic link to '/usr/src/sys',

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

or in anywhere ...

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

would yield

$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');

would yield

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

Similarly,

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

would yield

$path1 eq 'src/sys'
$path2 eq '.'

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

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

would yield

$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'));

would yield

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

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

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

realpath() make the result correct but don't forget realpath require that all but the last component of the path exist.

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

or in anywhere ...

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

would yield

$path eq '/usr/src/sys'

Similarly,

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

would yield

$path eq '/usr/src/sys'

AUTHOR

Shigio Yamaguchi <shigio@ca2.so-net.or.jp>

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 286:

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

Around line 288:

'=item' outside of any '=over'