The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

VERSION

version 0.02

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 "/")
 say normalize_path("");              # dies, empty path
 say normalize_path("/");             # -> "/"
 say normalize_path("..");            # -> ".."
 say normalize_path("./");            # -> "."
 say normalize_path("//");            # -> "/"
 say normalize_path("a/b/.");         # -> "a/b"
 say normalize_path("a/b/./");        # -> "a/b"
 say normalize_path("a/b/..");        # -> "a"
 say normalize_path("a/b/../");       # -> "a"
 say normalize_path("/a/./../b");     # -> "/b"
 say normalize_path("/a/../../b");    # -> "/b" (.. after hitting root is ok)

 # 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
 say concat_path_n("a", "b");         # -> "a/b"
 say concat_path_n("a/", "b");        # -> "a/b"
 say concat_path_n("a", "b/");        # -> "a/b"
 say concat_path_n("a", "../b/");     # -> "b"
 say concat_path_n("a/b", ".././c");  # -> "a/c"
 say concat_path_n("../", ".././c/"); # -> "../../c"

 # abs_path($path, $base) is equal to concat_path_n($base, $path). $base must be
 # absolute.
 say abs_path("a", "b");              # dies, $base is not absolute
 say abs_path("a", "/b");             # -> "/b/a"
 say abs_path(".", "/b");             # -> "/b"
 say abs_path("a/c/..", "/b/");       # -> "/b/a"
 say abs_path("/a", "/b/c");          # -> "/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 OF STR

normalize_path($path) => STR

is_abs_path($path) => BOOL

is_rel_path($path) => BOOL

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

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

abs_path($path) => STR

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

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Steven Haryanto.

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