Roots, dirs and files.
- $dir
- Something you can open and you can do a file test on.
- C:/www/domain/docroot/articles/2008/08/10
- $file
- Something you can open and you can do a file test on.
- C:/www/domain/docroot/articles/2008/08/10/01.html
- $dirname
- 10
- But could also be 2008/08/10
- $filename
- Including any extension
- 01.html
- $path
- Banned.
- Ambiguous and half the time not what I expect and almost certainly not what I expected yesterday :-)
- $xxx_root
- Key, often used dirs in a “web space”
An instance script might set
$web_root C:/www
$domain_root domain
A config file might have
docroot docroot
art_root articles
pic_root pics
A base class could have helper methods/assessors for each of the “roots”
Sub docroot{
my $self = shift;
my $cnf = $self->cnf;
return join(
q{/},
$cnf->param(q{$web_root}),
$cnf->param(q{$domain_root}),
$cnf->param(q{docroot}),
);
}
sub art_root {
my $self = shift;
my $cnf = $self->cnf;
return join(
q{/},
$self->docroot,
$cnf->param(q{art_root}
);
}
A dir, is a xxx_root, and possibly one or more dir names.
A file is a dir with a file name on the end.
my $article_file = join(
q{/},
$self->art_root,
$art_dirname,
$art_filename,
);
Anything that needs to be in an html page is $href and $src etc.
What’s wrong with path? Take a selection of core modules:
File::Basename - Parse file paths into directory, filename and suffix.
File::Path - create or remove directory trees
File::Spec - portably perform operations on file names
I find it confusing to keep on top of what a path is in the different contexts. Throw base and suffix into the mix and I’m rereading the docs again for the umpteenth time. File::Spec’s abs2rel talks about $base and $path. And where did those trees come from? And what do you do with a suffix list?
I would use these modules more if I didn’t get into such a tangle. I invariably have helper methods with names that better fit the conventions I’ve outlined e.g. mkdir.
While far from perfect everything has been a lot smoother since banning path.
A plague on paths!
How do monks manage to avoid such confusion?
update: fixed the join syntax
update2: fixed the links
-
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.