Using FindBin in Multiple Modules

Here is the script, I have two files: 1. dir / A.pm 2. dir / new_dir / at

Here's what A.pm looks like:

package A;
use FindBin;
use Test::More;
is (FindBin->again, 'dir', 'got dir');
1;                    

Here's what at looks like:

use FindBin;
use Test::More qw(no_plan); 
use A;
is (FindBin->again, 'dir/new_dir', 'got dir/new_dir');

So, I ran the at file with perl new_dir / at and expected my tests to pass. But this is my test result:

not ok 1 - got dir
#   Failed test 'got fir'
#   at A.pm line 6.
#          got: 'dir/new_dir'
#     expected: 'dir'
ok 2 - got dir/new_dir
1..2

Am I doing something wrong? I am very new to Perl. Please, help!

+5
source share
3 answers

As Dave Sherohman notes , FindBin is designed to find the location of the main script, not individual modules. From the documentation:

NAME
       FindBin - Locate directory of original perl script

(Admittedly, the documentation is somewhat confused, referring to the “modules” in the “KNOWN ISSUES” section, but this does not really mean what you think it means.)

, perldoc -m FindBin, , FindBin script $0. , use ( require), %INC, - :

package Foo::Bar;
use File::Spec;
my ($vol, $dir, $file) = File::Spec->splitpath( $INC{'Foo/Bar.pm'} );
+7

FindBin Perl, .

, — :: ShareDir — :

use Cwd            qw( realpath );
use File::Basename qw( dirname );

my $module_dir = dirname(realpath(__FILE__));

, Find:: Bin: , chdir .

+5

, a.t dir/new_dir/, new_dir/a.t dir/, ?

, , . a.t dir/new_dir, dir/new_dir FindBin - - Bin ary (program), , A.pm, a.t.

->again perl, , mod_perl, .

+3

All Articles