in reply to Re^2: FindBin works differently on Fedora Core 2?
in thread FindBin works differently on Fedora Core 2?

Replace

use FindBin qw($RealBin);

with

use File::Spec::Functions qw( rel2abs splitpath catpath ); my $RealBin = catpath( (splitpath rel2abs($0))[0,1] );

or

use File::Spec::Functions qw( rel2abs ); use File::Basename qw( dirname ); my $RealBin = dirname( rel2abs($0) );

for example. And read how perverse FindBin is.

- tye        

Replies are listed 'Best First'.
Re^4: FindBin works differently on Fedora Core 2? (replace)
by eserte (Deacon) on Jun 08, 2004 at 14:45 UTC
    Both solution give wrong results. Suppose /tmp/foo/abc.pl and a symlink from /tmp/abc.pl -> /tmp/foo/abc.pl. Changing cwd to /tmp and call ./abc.pl.
    • With the first version I get "/tmp/".
    • With the second version I get "/tmp".
    • With FindBin I get "/tmp/foo", as expected.
    Maybe you wanted to emulate $FindBin::Bin, not $FindBin::RealBin?

      Sorry, I don't use FindBin (no one would ever guess that).

      Once again we have an example of a feature that FindBin has but doesn't bother to export in a reusable manner (expanding symbolic links).

      use File::Spec::Functions qw( rel2abs splitpath catpath ); my $RealBin = $0; for( $RealBin ) { $_ = readlink($_) while -l $_; $_ = catpath( (splitpath rel2abs($_))[0,1] ); }

      or

      use File::Spec::Functions qw( rel2abs ); use File::Basename qw( dirname ); my $RealBin = $0; $RealBin = readlink($RealBin) while -l $RealBin; $RealBin = dirname( rel2abs($RealBin) );

      and evidence that File::Spec could use some additions (realpath, basename, dirname).

      - tye        

        If you look at at the source you'll see FindBin first expands symlinks using readlink, and then runs the result through Cwd::abs_path which does the exact same thing. Brilliant isn't it?

        update:

        sub File::Spec::dirname { my $self = shift; return $self->catpath( ( $self->splitpath(shift) )[ 0, 1 ] ); } sub File::Spec::basename { my $self = shift; return $self->catpath( ( $self->splitpath(shift) )[ -1 ] ); } sub File::Spec::realpath { my $self = shift; require Cwd; return $self->canonpath( Cwd::realpath(@_) ); }

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

        Once again we have an example of a feature that FindBin has but doesn't bother to export in a reusable manner (expanding symbolic links).

        FindBin does what it is made for. From the manpage:

        FindBin - Locate directory of original perl script