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

Re: How do monks create paths?

by doom (Deacon)
on Apr 06, 2008 at 22:29 UTC ( [id://678688]=note: print w/replies, xml ) Need Help??


in reply to How do monks create paths?

Other people have pointed you to File::Spec->catfile: that's the way to do it for portable code (e.g. something you expect to post to CPAN).

What I do for local, unix-only hackery is: I never include a trailing slash on directories, and always build up new paths with double-quoted strings.

A typical script might look something like this:

use Env qw( $HOME ); use File::Path qw( mkpath ); use File::Basename qw( basename dirname fileparse ); use File::Copy qw( move copy ); my $some_file = shift; my $basename = basename( $some_file ); my $backup_location = "$HOME/backups"; mkpath( $backup_location ) unless -d $backup_location; my $new_file = "$backup_location/$basename.bak"; copy("$some_file", "$new_file") or die "got problems: $!";

My personal opinion is that doing things like this is a win for readability:
my $new_file = "$backup_location/$basename.bak";

Also, note the stack of "use"s at the top... it's hard to do anything The Right Way in perl without leaning on the standard library a lot. These days I use script templates that include a dozen "use"s by default, so I don't have to try to remember what module "fileparse" is hidden inside of, and so on.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found