Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

filesystem path concatenation

by noname (Sexton)
on Sep 28, 2015 at 14:02 UTC ( #1143245=perlquestion: print w/replies, xml ) Need Help??

noname has asked for the wisdom of the Perl Monks concerning the following question:

Hi Dear Monks,

I have a rather stupid question, lately I had to type a lot of things like that:

$path=$var1.'/'.$var2; and so on,

also I know that the same can be written this way:

$path="${var1}/${var2}";

and it seem that the second way is a bit easier to type (at least for me).

Which one should I stick to ? Are there any benefits/problems with first or second method ?

Thank you!

Replies are listed 'Best First'.
Re: filesystem path concatenation
by Your Mother (Bishop) on Sep 28, 2015 at 14:35 UTC

    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.)

      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.

Re: filesystem path concatenation
by toolic (Bishop) on Sep 28, 2015 at 14:13 UTC
Re: filesystem path concatenation
by karlgoethebier (Monsignor) on Sep 28, 2015 at 14:10 UTC

    Another idea: let it write ;-) perl -E 'say join "/", qw (foo bar);'

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1143245]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2019-12-11 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?