Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: What script is this, and where is it? (Re: who am I?)

by AZed (Monk)
on Sep 24, 2008 at 22:21 UTC ( [id://713513]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^2: What script is this, and where is it? (Re: who am I?)
by ikegami (Patriarch) on Sep 24, 2008 at 22:45 UTC

    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,

    Agreed.

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

    That's wrong.

    /var/tmp/otherdir/symdir/../datadir
    is the same directory as
    /var/tmp/mydir/../datadir

    so you demonstrated that working with readlinked $0 works.

    even then, it won't give you the same output as FindBin::RealBin.

    Doesn't matter.

    FindBin is more readable, though.

    What good is readable if it doesn't work as well. And I don't see how dirname($script_qn) is less readable than $FindBin::RealBin.

      Edit: claims withdrawn. See below. Original message preserved below the jump.

        *cough* ... look again.

        /var/tmp/otherdir/symdir/../datadir' => '/var/tmp/otherdir/datadir'

        I read it fine the first time.

        /var/tmp/otherdir/symdir/../datadir

        is equivalent to

        /var/tmp/datadir

        and not equivalent to

        /var/tmp/otherdir/datadir

        In "/var/tmp/otherdir/symdir/../datadir", the ".." is that of "mydir" which is a hard link to "/var/tmp".

        Do like I did and test before contradicting someone.

        $ cd /var/tmp $ mkdir mydir $ mkdir datadir $ mkdir otherdir $ touch mydir/in_mydir $ touch datadir/in_datadir $ touch otherdir/in_otherdir $ cd otherdir $ ln -s ../mydir symdir $ ls /var/tmp/otherdir/symdir/../datadir in_datadir $ perl -le'opendir $dh, $ARGV[0] or die; print for grep /in/, readdir +$dh' /var/tmp/otherdir/symdir/../datadir in_datadir

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://713513]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found