samtregar has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I'm stumped. I'm porting Krang to Fedora Core 2 and I've run into some strange behavior from FindBin. Specifically, FindBin is returning paths ending in '/' on Fedora Core 2, something it doesn't do anywhere else, including Fedora Core 1, FreeBSD, Redhat, etc. The versions of Perl are the same (5.8.3) as are the versions of FindBin (1.44). To demonstrate, here's my test.pl:

use FindBin qw($RealBin); print "I am here: $RealBin\n";

And here it is running Fedora Core 1:

$ perl5.8.3 test.pl I am here: /home/krang

And here's Fedora Core 2, showing the rogue slash:

$ perl5.8.3 test.pl I am here: /home/krang/

Does anyone know what's going on here? I'm sure I can fix the code that's choking on the trailing slash, but I'd still like to know what's causing this difference in behavior.

-sam

Replies are listed 'Best First'.
Re: FindBin works different on Fedora Core 2?
by mojotoad (Monsignor) on Jun 07, 2004 at 22:24 UTC
    This might actually be due to the version of File::Spec being used by FindBin. I see that FC2 uses version 1.5 of File::Spec::Unix, whereas Redhat 9 (Shrike) uses 1.4. Here's a subset of the diff:
    < $VERSION = '1.5'; --- > $VERSION = '1.4'; > > use Cwd; 72,73c74,79 < < $self->canonpath(join('/', @_, '')); # '' because need a trailin +g '/' --- > my @args = @_; > foreach (@args) { > # append a slash to each argument unless it has one there > $_ .= "/" if $_ eq '' || substr($_,-1) ne "/"; > } > return $self->canonpath(join('', @args)); 85c91

    It would appear that the trailing slash is considered more "correct" but was not functioning correctly in the older version?

    At any rate, if you use the catfile() and catdir() methods from File::Spec, the dreaded '//' problem should not be an issue.

    Matt

      I don't think that's correct. The copies of File::Spec on both Fedora Core 1 and Fedora Core 2 are version 0.87 (and File::Spec::Unix is 1.5 on both, which I guess must be the file you're looking at).

      Good guess though!

      -sam

        I dug a little deeper -- short anwer is that it's not FindBin, but Cwd.

        The difference seems to be when FindBin calls Cwd::abs_path(). With an argument as simple as '/tmp/', the older version (2.06 in my case) of Cwd::abs_path will chop the trailing slash, whereas the newer version from FC2 will not (Cwd version 2.12).

        So the culprit is Cwd rather than FindBin. I'll leave it to you to find the specific difference in the abs_path behavior (I think you'll see what I mean once you start digging).

        Do tell us what you find out.

        Matt

Re: FindBin works differently on Fedora Core 2?
by PodMaster (Abbot) on Jun 08, 2004 at 06:33 UTC
    use File::Spec;

    Either abandon FindBind entirely in favor of File::Spec, or just run the returned value through File::Spec->canonpath() and forget about it (or patch FindBin to do that automatically). canonpath should always eliminate a trailing dir separator if there is one.

    update: For figuring out what's going on (if you still want to), use Devel::Trace;

    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.

      I don't know of any File::Spec function which duplicates the FindBin behavior.

        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        

        I think PodMaster is suggesting sanitising the results of what FindBin returns with File::Spec, to obtain a canonical result, so to speak.

        - another intruder with the mooring of the heat of the Perl

Re: FindBin works differently on Fedora Core 2?
by eserte (Deacon) on Jun 08, 2004 at 11:31 UTC
    FindBin uses Cwd::abspath, which has different implemetations (i.e. _perl_abs_path or an XS implementation). You can check by using

    perl -MCwd -e 'warn \&Cwd::abs_path == \&Cwd::_perl_abs_path'

      Another fine idea, but both these systems are using the XS version according to your test.

      -sam

        And the XS code has also different paths, depending if your system supports fchdir or not. Can you check in the debugger whether it is actually abs_path which gives different results?
Re: FindBin works differently on Fedora Core 2?
by iburrell (Chaplain) on Jun 08, 2004 at 19:32 UTC
    Make sure that the code is really using the same versions of the modules, and using the versions that you think it is. First, do 'rpm -q perl' on both to see which release of perl RPM you have. I am guessing that FC1 is updated and has perl-5.8.3-16. Then, add to the end of your test:
    require Data::Dumper; print Data::Dumper::Dumper(\%INC);
    This will dump out the modules that it actually loaded. See if any are in site_perl directories, because then you are using locally installed versions. You might also want to print out the $VERSION for each module.