Apparently the fact that FindBin is utterly broken is still news to many people, because I see the following very often:
use FindBin; use lib $FindBin::Bin;
This will put the directory from which the script was invoked into @INC
. The right incantation for that is:
use File::Spec::Functions qw( catpath splitpath rel2abs ); use lib catpath( ( splitpath( rel2abs $0 ) )[ 0, 1 ] );
Yes, it’s quite a bit longer. However, it’s not broken, and in those cases in which it fails, it will fail reliably and comprehensibly, rather than doing bizarre bogus things as FindBin will.
Re:Unfortunately not.
Aristotle on 2007-08-05T20:17:45
I can’t reproduce that.
$ cat bin/t.pl
#!/usr/bin/perl -l
use File::Spec::Functions qw( rel2abs );
print rel2abs $0;
$/usr/bin/perl bin/t.pl
/home/ap/bin/t.plCan you?
Re:Unfortunately not.
Matts on 2007-08-05T20:50:06
ah never mind, I can't. I was remembering a bug that got tickled in qpsmtpd by this, but I think it's because we aren't using rel2abs.
Re:It's not just the longwinded bogus things it do
Aristotle on 2007-08-06T13:46:13
just produced an error […] because it had insufficient access rights to one of the ancestor directories
Argh! That never occured to me – just another reason to steer clear. It’s almost comical how broken FindBin is if you get down to it – it’s almost a miracle how rarely people seem to be affected by that.
Re:Have you filed a bug report
Aristotle on 2007-08-06T13:48:31
As I wrote in the comments of my other post about this:
The whole point of FindBin is that if it can’t locate the script via
$0
it will try to find it in thePATH
[…] Ripping that behaviour out 10 years after the fact doesn’t seem wise, even if the behaviour is broken for some edge cases. I’d prefer to deprecate FindBin in favour of a saner new alternative.And?
Limbic Region on 2007-08-06T16:22:46
I read that, it doesn't change my position. You seem suprised that people are suprised FindBin is broken. Where is the bug supposed to be reported? In the designated place or some external forum.
I think it is a bad precedence to suggest that we shouldn't document bugs because they have existed for a really long time and fixing them means deprecating the module entirely.
I am not trying to be flippant or antagonistic. I just think it is unfair to expect folks to have read everything you have read.Re:And?
Aristotle on 2007-08-06T16:35:31
I am entirely fine with a doc patch for FindBin, and if there is a better module, a deprecation notice in the FindBin docs pointing to it. That’s not “fixing” FindBin, and therefore is a good idea. I hadn’t thought of that.
Re:File::Spec::Functions fails, too
Aristotle on 2008-09-18T10:15:04
Yes, because it purposely does not examine the file system. This should work:
use Cwd 'abs_path';
use File::Basename;
use lib dirname( abs_path $0 );