in reply to Re^4: What script is this, and where is it? (Re: who am I?)
in thread who am I?

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

Replies are listed 'Best First'.
Re^6: What script is this, and where is it? (Re: who am I?)
by massa (Hermit) on Sep 25, 2008 at 01:07 UTC
    You are considering that datadir is a sibling to the symlink's directory. I, OTOH, consider that datadir will be most probably a sibling to the original script file's directory (because you can symlink it from anywhere, and considering otherwise would be a security risk *). We will have to agree on disagreeing.
    * create symlink, create malicious configuration file side-by-side with symlink, ... profit!
    Update: I stand corrected about the (meaning of) final destination of the datadir according to the answer by AZed below... You're welcome! And seems that we will have to agree in agreeing :-)
    []s, HTH, Massa (κς,πμ,πλ)

      No, what you did with realpath is what I want, thanks. My examples showing the final destination relative to the symlinks when rel2abs was used were intended to illustrate errors, not desired behaviour.

Re^6: What script is this, and where is it? (Re: who am I?)
by ikegami (Patriarch) on Sep 25, 2008 at 01:03 UTC
    The fix is to use Cwd::realpath instead of rel2abs. I'd appreciate it if you could test that for me.