Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: filesystem path concatenation

by Your Mother (Archbishop)
on Sep 28, 2015 at 14:35 UTC ( [id://1143256]=note: print w/replies, xml ) Need Help??


in reply to filesystem path concatenation

What toolic said but you're better off in general, I argue, reaching for a module like Path::Tiny.

use strictures; use Path::Tiny; my $root = "some/place"; my $name = "somefile.ook"; my $path = path( $root, $name ); print $path, $/; print $path->relative("some"), $/; print $path->absolute, $/;

There are many advantages to this style especially in code that will grow; reading, writing, iterating, relative, absolute... all kinds of fun made simple and easy for others to follow when they read your code. (update: added a missing comma.)

Replies are listed 'Best First'.
Re^2: filesystem path concatenation
by RichardK (Parson) on Sep 28, 2015 at 14:58 UTC

    Or even File::Spec which is a core module :-

    $path = File::Spec->catfile( @directories, $filename );

      I prefer the higher level of abstraction with the same accurate portability by far :P

      moo@cow~>perldoc -m Path::Tiny | ack File::Spec use File::Spec 3.40 (); my $cpath = $path = File::Spec->canonpath($path); #pod Gives you C<< File::Spec->rootdir >> as a C<Path::Tiny> object if + you're too sub rootdir { path( File::Spec->rootdir ) } @{$self}[ VOL, DIR, FILE ] = File::Spec->splitpath( $self->[PATH] +); #pod in L<File::Spec> would normally do so on your platform. If you n +eed them #pod like C<catfile> or C<catdir> from File::Spec, but without caring +about #pod C<< File::Spec->splitpath( $path->stringify ) >> or C<"."> for a +path without a #pod parent directory portion. Because L<File::Spec> is inconsistent, + the result #pod C<< File::Spec->abs2rel() >>. # Easy to get wrong, so wash it through File::Spec (sigh) sub relative { path( File::Spec->abs2rel( $_[0]->[PATH], $_[1] ) ) } #pod equivalent to what L<File::Spec> would give from C<splitpath> and + thus friendlier to use than L<File::Spec> and provides easy access to funct +ions from Gives you C<< File::Spec->rootdir >> as a C<Path::Tiny> object if you' +re too in L<File::Spec> would normally do so on your platform. If you need t +hem like C<catfile> or C<catdir> from File::Spec, but without caring about C<< File::Spec->splitpath( $path->stringify ) >> or C<"."> for a path +without a parent directory portion. Because L<File::Spec> is inconsistent, the +result C<< File::Spec->abs2rel() >>. equivalent to what L<File::Spec> would give from C<splitpath> and thus
      I see, thank you all !

      File::Spec is definitely The Right Way™ to do this, because it is both purposeful and portable.   You really don’t want to have to be mucking-around with slashes, do you?   Worrying whether they ought to be forward or backward on this system or that?   Naww-w-w-ww, not if someone else has already done it for you!   And, since it is in the Core, you don’t have to install it:   it is already there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1143256]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found