I'm puzzled, most of my adult life I believed that using absolute paths are one of the tell-tell signs of bad programmers.

Seeing things like open(C,"C:\perl5\modules\my-stuff\sth"); made me shrivel and run away...

But now, I've been reading excellent tutorial on thread-safety, and there, absolute paths are a must. You're supposed to convert your old, non-thread safe, relative-path-using modules, into new, shiny, absolute-path-using jewels of thread safety.

The reason is understandable - different threads share single CWD, thus, for them not to step on each other toe, you're supposed to use absolute paths. This reason is fine, and hard to argue with...

How can I make this change, I've accumulated lot's of examples and fine reasons for avoiding absolut paths througout my life, and avoiding them became a second nature.

UPDATE Think about this as a case for accessing hierarchical databases (and typical filesystem is an example of such).

By using absolute path (be it hardcoded, or constructed with File::Spec->catfile($prefix, $relative);) you're reducing it to a flat space.

The filename "/fs/sth/db/category/subcategory/file" instead of address, becomes just a long name...

THAT is the evil pattern here, and THAT is the thing I'm having a hard time adjusting to.

This leads to inefficencies (with pattern

chdir($workdir); foreach ($files) { workOnFile(); }
) you're going through filesystem hierarchy only once, with
workOnSomeFile($cwd,$file); ... workOnSomeOtherFile($cwd,$file);
you're going through it many times, which is costly.

Java programmers tend to write sth like this: chdir("/some/dir1/dir2");..workOnFile("/some/dir1/dir2/file") which I find highly unelegant and slightly disturbing.

Do you know of a way to overcome a mental barrier before I can start writing code like that without feeling guilty?


In reply to Best practices - absolute paths? by Eyck

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.