Hi,

I was looking up previous posts on how to get the path to a script. It seems the preferred suggestion is

use Cwd qw(abs_path); my $path = abs_path($0);
However, when I do this, I get the path including the script file itself, e.g. if I run a script /long/path/to/some/script.plx, abs_path($0) returns the whole thing and not /long/path/to/some/ as I expected. So I still have to use a regex to remove the script name, but someone in the old posts said that was looking for trouble.

Is that, uh, intended/normal, or am I doing something wrong?

And as long as I'm at it: Basically, I want to ensure that relative paths containing information the script needs are always correct, even if I call the script from somewhere else by specifying the path, like maybe

.../other/dir/ $ ../../some/script.plx
So I wanted to get the path and then chdir there. Is that the right way to go about it or is there a much easier way? I tend to think too complicated sometimes :-/ I'm on Linux and portability isn't an issue.


In reply to Difference btw. abs_path and $0? by Anonymous Monk

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.