File-PlainPath

 view release on metacpan or  search on metacpan

lib/File/PlainPath.pm  view on Meta::CPAN

use strict;
use warnings;
package File::PlainPath;

# ABSTRACT: Construct portable filesystem paths in a simple way

our $VERSION = '0.030'; # VERSION

use File::Spec;


require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(path to_path);

sub import
{
    my $class  = shift;
    my %opts   = ();
    my @export = ();
    while (@_) {
        my $arg = shift;
        if ($arg =~ m{^ [-] (.+) $}x)
        {
            $opts{$1} = shift;
            next;
        }
        push @export, $arg;
    }
    
    if (exists $opts{'separator'}) {
        $^H{+__PACKAGE__} = $opts{'separator'};
    }
    
    @_ = ($class, @export);
    goto \&Exporter::import;
}


sub path {    
    my @caller       = caller(0);
    my $separator    = exists $caller[10]{+__PACKAGE__} ?
        $caller[10]{+__PACKAGE__} : '/';
    my $separator_re = qr{ \Q$separator\E }x;

    my @paths = @_;
    my @path_components = map { split($separator_re, $_) } @paths;

    return File::Spec->catfile(@path_components);
}


*to_path = *path;


1;

__END__

=pod

=encoding UTF-8

=head1 NAME

File::PlainPath - Construct portable filesystem paths in a simple way

=head1 VERSION

version 0.030

=head1 SYNOPSIS

    use File::PlainPath qw(path);
    
    # Forward slash is the default directory separator
    my $path = path 'dir/subdir/file.txt';
    
    # Set backslash as directory separator
    use File::PlainPath -separator => '\\';   
    my $other_path = path 'dir\\other_dir\\other_file.txt';

=head1 DESCRIPTION

File::PlainPath translates filesystem paths that use a common directory
separator to OS-specific paths. It allows you to replace constructs like this:

    my $path = File::Spec->catfile('dir', 'subdir', 'file.txt');

with a simpler notation:

    my $path = path 'dir/subdir/file.txt';



( run in 2.343 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )