All of those mechanisms break if the script is being called by a symlink, and particularly so if nested symlinks are involved. You need to readlink($0) if (-l $0) first, and then check, and even then, it won't give you the same output as FindBin::RealBin.

Create the following directory structure in /var/tmp. Set up directories as follows (you'll need root for /usr/local/bin, so feel free to make the last step anywhere else in your path that you'd like):

cd /var/tmp mkdir mydir mkdir datadir mkdir otherdir cd otherdir ln -s ../mydir symdir
Now take the following code and put it in /var/tmp/mydir/myname.pl:
#!/usr/bin/perl use strict; use warnings; use FindBin; use File::Basename qw( dirname ); use File::Spec::Functions qw( rel2abs ); my $name = $0; $name = readlink($name) if(-l $name); print "Finding a data path:\n\n"; print 'dirname($name):',dirname($name),"/../datadir\n"; print 'dirname(rel2abs($name)):',dirname(rel2abs($name)),"/../datadir\ +n"; print 'dirname($0):',dirname($0),"/../datadir\n"; print 'dirname(rel2abs($0)):',dirname(rel2abs($0)),"/../datadir\n"; print '$FindBin::RealBin:',$FindBin::RealBin,"/../datadir\n";

Symlink /var/tmp/symdir/myname.pl to /usr/local/bin/myname (or somewhere else on your path, if you can't or don't want to tamper with a system directory). Run 'myname'. You get:

Finding a data path: dirname($name):/var/tmp/otherdir/symdir/../datadir dirname(rel2abs($name)):/var/tmp/otherdir/symdir/../datadir dirname($0):/usr/local/bin/../datadir dirname(rel2abs($0)):/usr/local/bin/../datadir $FindBin::RealBin:/var/tmp/mydir/../datadir

Only FindBin correctly points you to the correct data directory location.

Frankly, even without symlink forests, FindBin is more readable, though.


In reply to Re: 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.