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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |