Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove PATH-searching in FindBin #11297

Closed
p5pRT opened this issue May 2, 2011 · 5 comments
Closed

Remove PATH-searching in FindBin #11297

p5pRT opened this issue May 2, 2011 · 5 comments

Comments

@p5pRT
Copy link
Collaborator

@p5pRT p5pRT commented May 2, 2011

Migrated from rt.perl.org#89698 (status was 'resolved')

Searchable as RT89698$

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented May 2, 2011

From @perlpunk

Created by @perlpunk

FindBin checks $ENV{PATH} for the current script name if $0 is not absolute
(and doesn't contain / or \).
This has only historical reasons (on SunOS4 there was a broken shell where
$0 was not absolute wenn calling a script through PATH).

So when a script uses FindBin and is called by

  $ perl perlscript.pl
or
  $0 is only set to perlscript.pl under the broken shell

PATH is searched.

In all other cases it takes the CWD and $0.

So this feature is/was only useful for the case​:
- SunOS
- broken shell
- invoking the script via PATH.

I assume that anyone running a very old SunOS with that very old
broken shell would not want to or even be able to install perl >= 5.14.

Unfortunately though, this feature will result in wrong paths when calling

  $ perl perlscript.pl

under certain circumstances.

If perlscript.pl is not executable and there is a perlscript.pl
found elsewhere in PATH which is executable this one is taken instead
of the actually called script. This is documented with the workaround to
call

$ perl ./perlscript.pl

So we have a feature that is very probably not needed anymore and
at the same time giving wrong results, needing a workaround to prepend
"./" to the path.

Since FindBin is very handy, having the path in a variable so you can easily
use it several times, like
use lib "$Bin/../lib", "$Bin/../../project1/lib", ...;

I would suggest to remove that old "feature" so that FindBin can be
recommended again as an easy way to obtain the script directory.

Also there should be mentioned in the docs that FindBin might give
wrong results if a chdir() happened before FindBin is loaded (if not
called with an absolute path). This is also the case now under certain
circumstances (*) so removing PATH searching will not remove a useful
feature.

Attached is a patch written by Moritz Lenz.

(*)
Just one example for a wrong result​:
/home/user/perlscript.pl executable and does BEGIN { chdir "/usr/bin/" }
/usr/bin/perlscript.pl executable

/home/user $ perl perlscript.pl # calls /home/user/perlscript.pl since . is in PATH
but reports /usr/bin as $FindBin​::Bin (or dies if there is no /usr/bin/perlscript.pl)

Perl Info

Flags:
     category=library
     severity=low

Site configuration information for perl 5.10.0:

Configured by Debian Project at Fri Aug 28 22:30:10 UTC 2009.

Summary of my perl5 (revision 5 version 10 subversion 0) configuration:
   Platform:
     osname=linux, osvers=2.6.26-2-amd64, archname=i486-linux-gnu-thread-multi
     uname='linux puccini 2.6.26-2-amd64 #1 smp fri aug 14 07:12:04 utc 2009 i686 gnulinux '
     config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN -Dcccdlflags=-fPIC -Darchname=i486-linux-gnu -Dprefix=/usr -Dprivlib=/usr/share/perl/5.10 -Darchlib=/usr/lib/perl/5.10 -Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl/5.10.0 -Dsitearch=/usr/local/lib/perl/5.10.0 -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Dsiteman3dir=/usr/local/man/man3 -Dman1ext=1 -Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh -Ud_ualarm -Uusesfio -Uusenm -DDEBUGGING=-g -Doptimize=-O2 -Duseshrplib -Dlibperl=libperl.so.5.10.0 -Dd_dosuid -des'
     hint=recommended, useposix=true, d_sigaction=define
     useithreads=define, usemultiplicity=define
     useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
     use64bitint=undef, use64bitall=undef, uselongdouble=undef
     usemymalloc=n, bincompat5005=undef
   Compiler:
     cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
     optimize='-O2 -g',
     cppflags='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include'
     ccversion='', gccversion='4.3.2', gccosandvers=''
     intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
     d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
     ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
     alignbytes=4, prototype=define
   Linker and Libraries:
     ld='cc', ldflags =' -L/usr/local/lib'
     libpth=/usr/local/lib /lib /usr/lib /usr/lib64
     libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt
     perllibs=-ldl -lm -lpthread -lc -lcrypt
     libc=/lib/libc-2.7.so, so=so, useshrplib=true, libperl=libperl.so.5.10.0
     gnulibc_version='2.7'
   Dynamic Linking:
     dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
     cccdlflags='-fPIC', lddlflags='-shared -O2 -g -L/usr/local/lib'

Locally applied patches:



@INC for perl 5.10.0:
     /etc/perl
     /usr/local/lib/perl/5.10.0
     /usr/local/share/perl/5.10.0
     /usr/lib/perl5
     /usr/share/perl5
     /usr/lib/perl/5.10
     /usr/share/perl/5.10
     /usr/local/lib/site_perl
     .


Environment for perl 5.10.0:
     HOME=/home/tina
     LANG=de_DE.UTF-8
     LANGUAGE (unset)
     LD_LIBRARY_PATH (unset)
     LOGDIR (unset)
     PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
     PERL5LIB=
     PERL_BADLANG (unset)
     SHELL=/bin/bash
@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented May 2, 2011

From @perlpunk

FindBin.patch
From 14d857058cef9f55b49413a3a13d2197945fc030 Mon Sep 17 00:00:00 2001
From: Moritz Lenz <moritz@faui2k3.org>
Date: Fri, 29 Apr 2011 19:44:52 +0200
Subject: [PATCH] Remove long-stading limitation from FindBin
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The FindBin documentation states as a known bug that it will return
a wrong result if a script of the same name as the current one exists in
$PATH, and is executable.

This patch removes the functionality of searching through $PATH.
According to Graham Barr it was only necessary because the SysV shell on
Sun4OS4 was broken - a system where I can't imagine anybody wanting
(and successfully compiling) a modern perl.

On Linux this part wasn't necessary in the case of `perl -S scriptname',
tests on other platforms are very welcome.

As a side effect, this patch also removes some IO operations, speeding up
FindBin slightly.

This patch is based on discussions with Tina M��ller.

Further "discussion": http://www.perlmonks.org/?node_id=41213
---
 lib/FindBin.pm |   41 +----------------------------------------
 1 files changed, 1 insertions(+), 40 deletions(-)

diff --git a/lib/FindBin.pm b/lib/FindBin.pm
index 892d6e5..cf6ecf2 100644
--- a/lib/FindBin.pm
+++ b/lib/FindBin.pm
@@ -59,21 +59,6 @@ workaround was to force the C<BEGIN> block to be executed again:
   delete $INC{'FindBin.pm'};
   require FindBin;
 
-=head1 KNOWN BUGS
-
-If perl is invoked as
-
-   perl filename
-
-and I<filename> does not have executable rights and a program called
-I<filename> exists in the users C<$ENV{PATH}> which satisfies both B<-x>
-and B<-T> then FindBin assumes that it was invoked via the
-C<$ENV{PATH}>.
-
-Workaround is to invoke perl as
-
- perl ./filename
-
 =head1 AUTHORS
 
 FindBin is supported as part of the core perl distribution. Please send bug
@@ -103,7 +88,7 @@ use File::Spec;
 %EXPORT_TAGS = (ALL => [qw($Bin $Script $RealBin $RealScript $Dir $RealDir)]);
 @ISA = qw(Exporter);
 
-$VERSION = "1.50";
+$VERSION = "1.51";
 
 
 # needed for VMS-specific filename translation
@@ -145,30 +130,6 @@ sub init
     }
    else
     {
-     my $dosish = ($^O eq 'MSWin32' or $^O eq 'os2');
-     unless(($script =~ m#/# || ($dosish && $script =~ m#\\#))
-            && -f $script)
-      {
-       my $dir;
-       foreach $dir (File::Spec->path)
-        {
-        my $scr = File::Spec->catfile($dir, $script);
-
-        # $script can been found via PATH but perl could have
-        # been invoked as 'perl file'. Do a dumb check to see
-        # if $script is a perl program, if not then keep $script = $0
-        #
-        # well we actually only check that it is an ASCII file
-        # we know its executable so it is probably a script
-        # of some sort.
-        if(-f $scr && -r _ && ($dosish || -x _) && -s _ && -T _)
-         {
-          $script = $scr;
-          last;
-         }
-       }
-     }
-
      croak("Cannot find current script '$0'") unless(-f $script);
 
      # Ensure $script contains the complete path in case we C<chdir>
-- 
1.7.2.5

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented May 19, 2011

From @cpansprout

On Mon May 02 10​:17​:02 2011, tinita wrote​:

Attached is a patch written by Moritz Lenz.

Thank you. Applied as ce6c0f3.

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented May 19, 2011

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Collaborator Author

@p5pRT p5pRT commented May 19, 2011

@cpansprout - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.