Do like I did and test before contradicting someone.

Huh, okay, that's unexpected, probably because the last time I did this I was dealing with a symlink to a symlink, not a symlink to a real file just in a symlinked directory. I should have used the /tmp example to begin with to avoid confusion.

Still, my main point holds: dirname(rel2abs($0)) breaks under symlinks, and even a single readlink won't get you out of nested symlinks.

Realpath works, and dirname(realpath($0)) is consistent, accurate, and clean. I withdraw all prior claims, and will be using this in the future. I don't see the need for a module for it, though I give my thanks to massa for the effort.

My original test code on rel2abs (with the major bug removed) remains after the jump for the curious.

#!/usr/bin/perl use strict; use warnings; use Cwd 'realpath'; use FindBin; use File::Basename qw( dirname ); use File::Spec::Functions qw( rel2abs ); my $name1 = readlink($0) if(-l $0); my $name = $0; $name = readlink($name) while(-l $name); my $key; my %paths = ( '$FindBin::RealBin ' => $FindBin::RealBin . "/../datadir", 'dirname($0) ' => dirname($0) . "/../datadir", 'dirname($name) ' => dirname($name) . "/../datadir", 'dirname(rel2abs($0)) ' => dirname(rel2abs($0)) . "/../datadir +", 'dirname(rel2abs($name1)) ' => dirname(rel2abs($name1)) . "/../dat +adir" ); print "Checking for mydata in ../datadir:\n\n"; foreach $key (sort(keys %paths)) { print $key,": ",$paths{$key},"\t— "; if(-e $paths{$key} . "/mydata") { print "passed\n" } else { print "failed\n" } }

Output (assuming symdir is now a real directory containing a myfile.pl symlinked to ../../myfile.pl):

Checking for mydata in ../datadir: $FindBin::RealBin : /var/tmp/mydir/../datadir passed dirname($0) : /usr/local/bin/../datadir failed dirname($name) : ../../mydir/../datadir failed dirname(rel2abs($0)) : /usr/local/bin/../datadir failed dirname(rel2abs($name1)) : /var/tmp/otherdir/symdir/../datadir faile +d

In reply to Re^5: What script is this, and where is it? (Re: who am I?) by AZed
in thread who am I? by momo33

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.