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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|