The most portable solution would be to avoid the use of relative paths (and pwd/cwd) altogether. If you really, truly need to go forward with a particular path, you can avoid the "auto-resolving link" problem altogether by constructing the fully qualified path and using that in your program.
In general, littering your code with relative paths and dependencies on the current working directory is not a good idea. There are several problems that can result:
- The current working directory might not be what you think it is, not just because the real path is substituted for the symbolic path, but also because cd can fail if you try to switch to a directory for which your effective user (EUID) doesn't have permission.
- In some cases, particularly with mounted drives, you can get stuck in no-mans land. If you are going to use cwd, you should avoid fastcwd for that reason: see http://rt.cpan.org/Public/Bug/Display.html?id=13851 (ignore the fact that this is a bug report - the discussion about it is the interesting part as regards portability issues)
- Your code is unlikely to work in taint mode which does not like relative paths.
- And in general, your code will not be portable to any web interface because the "current" directory in that context is essentially undefined - it could be anything on the face of the planet (well, web server).
See File::Spec for a portable way to attach relative paths to a fully qualified starting path.
About the only situation where I can see a genuine use for cwd is (a) start-up situations (b) incoming information (but here lie security risks which is why taint mode doesn't let you use them) and (c) an occasional piece of software that actually requires itself to be run with a particular directory or kind of directory as "current". Most applications do accept fully qualified paths even if the documentation examples only illustrate relative path parameters for convenience
In start-up/incoming information situations you may want to find certain files based on the starting directory of a user. However, in my own code, I immediately convert that to a fully qualified path and from there on in use fully qualified paths in the code.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.