NAME

Path::Naive - Yet another abstract, Unix-like path manipulation routines

VERSION

This document describes version 0.03 of Path::Naive (from Perl distribution Path-Naive), released on 2014-09-19.

SYNOPSIS

 use Path::Naive qw(
     split_path concat_path normalize_path abs_path
     is_abs_path is_rel_path);

 # split path to directories
 @dirs = split_path("");              # dies, empty path
 @dirs = split_path("/");             # -> ()
 @dirs = split_path("a");             # -> ("a")
 @dirs = split_path("/a");            # -> ("a")
 @dirs = split_path("/a/");           # -> ("a")
 @dirs = split_path("../a");          # -> ("..", "a")
 @dirs = split_path("./a");           # -> (".", "a")
 @dirs = split_path("../../a");       # -> ("..", "..", "a")
 @dirs = split_path(".././../a");     # -> ("..", ".", "..", "a")
 @dirs = split_path("a/b/c/..");      # -> ("a", "b", "c", "..")

 # normalize path (collapse . & .., remove double & trailing / except on "/")
 $p = normalize_path("");              # dies, empty path
 $p = normalize_path("/");             # -> "/"
 $p = normalize_path("..");            # -> ".."
 $p = normalize_path("./");            # -> "."
 $p = normalize_path("//");            # -> "/"
 $p = normalize_path("a/b/.");         # -> "a/b"
 $p = normalize_path("a/b/./");        # -> "a/b"
 $p = normalize_path("a/b/..");        # -> "a"
 $p = normalize_path("a/b/../");       # -> "a"
 $p = normalize_path("/a/./../b");     # -> "/b"
 $p = normalize_path("/a/../../b");    # -> "/b" (.. after hitting root is ok)

 # in list context, normalize_path returns list, this is useful to save you from
 # having to split the normalized path yourself.
 @p = normalize_path("a/b/./");        # -> ("a", "b")

 # check whether path is absolute (starts from root)
 say is_abs_path("/");                # -> 1
 say is_abs_path("/a");               # -> 1
 say is_abs_path("/..");              # -> 1
 say is_abs_path(".");                # -> 0
 say is_abs_path("./b");              # -> 0
 say is_abs_path("b/c/");             # -> 0

 # this is basically just !is_abs_path($path)
 say is_rel_path("/");                # -> 0
 say is_rel_path("a/b");              # -> 1

 # concatenate two paths
 say concat_path("a", "b");           # -> "a/b"
 say concat_path("a/", "b");          # -> "a/b"
 say concat_path("a", "b/");          # -> "a/b/"
 say concat_path("a", "../b/");       # -> "a/../b/"
 say concat_path("a/b", ".././c");    # -> "a/b/.././c"
 say concat_path("../", ".././c/");   # -> "../.././c/"
 say concat_path("a/b/c", "/d/e");    # -> "/d/e" (path2 is absolute)

 # this is just concat_path + normalize_path the result
 $p = concat_path_n("a", "b");         # -> "a/b"
 $p = concat_path_n("a/", "b");        # -> "a/b"
 $p = concat_path_n("a", "b/");        # -> "a/b"
 $p = concat_path_n("a", "../b/");     # -> "b"
 $p = concat_path_n("a/b", ".././c");  # -> "a/c"
 $p = concat_path_n("../", ".././c/"); # -> "../../c"
 @p = concat_path_n("../", ".././c/"); # -> ("..", "..", "c")

 # abs_path($path, $base) is equal to concat_path_n($base, $path). $base must be
 # absolute.
 $p = abs_path("a", "b");              # dies, $base is not absolute
 $p = abs_path("a", "/b");             # -> "/b/a"
 $p = abs_path(".", "/b");             # -> "/b"
 $p = abs_path("a/c/..", "/b/");       # -> "/b/a"
 $p = abs_path("/a", "/b/c");          # -> "/a"
 @p = abs_path("a/c/..", "/b/");       # -> ("b", "a")

DESCRIPTION

This is yet another set of routines to manipulate abstract Unix-like paths. Abstract means not tied to actual filesystem. Unix-like means single-root tree, with forward slash / as separator, and . and .. to mean current- and parent directory. Naive means not having the concept of symlinks, so paths need not be traversed on a per-directory basis (see File::Spec::Unix where it mentions the word "naive").

These routines can be useful if you have a tree data and want to let users walk around it using filesystem-like semantics. Some examples of where these routines are used: Config::Tree, Riap (App::riap).

FUNCTIONS

split_path($path) => list

normalize_path($path) => str | list

is_abs_path($path) => bool

is_rel_path($path) => bool

concat_path($path1, $path2, ...) => STR

concat_path_n($path1, $path2, ...) => str | list

abs_path($path) => str | list

SEE ALSO

Path::Abstract a similar module. The difference is, it does not interpret . and ...

File::Spec::Unix a similar module, with some differences in parsing behavior.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Path-Naive.

SOURCE

Source repository is at https://github.com/sharyanto/perl-Path-Naive.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Path-Naive

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.