|
NAMEFindBin::libs - locate and a 'use lib' or export directories based on $FindBin::Bin.SYNOPSISThis version of FindBin::libs is suitable for Perl v5.10+.# search up $FindBin::Bin looking for ./lib directories # and "use lib" them. use FindBin::libs; # same as above with explicit defaults. use FindBin::libs qw( base=lib use=1 noexport noprint ); # print the lib dir's before using them. use FindBin::libs qw( print ); # find and use lib "altlib" dir's use FindBin::libs qw( base=altlib ); # move starting point from $FindBin::Bin to '/tmp' use FindBin::libs qw( Bin=/tmp base=altlib ); # skip "use lib", export "@altlib" instead. use FindBin::libs qw( base=altlib export ); # find altlib directories, use lib them and export @mylibs use FindBin::libs qw( base=altlib export=mylibs use ); # "export" defaults to "nouse", these two are identical: use FindBin::libs qw( export nouse ); use FindBin::libs qw( export ); # use and export are not exclusive: use FindBin::libs qw( use export ); # do both use FindBin::libs qw( nouse noexport print ); # print only use FindBin::libs qw( nouse noexport ); # do nothting at all # print a few interesting messages about the # items found. use FindBinlibs qw( verbose ); # turn on a breakpoint after the args are prcoessed, before # any search/export/use lib is handled. use FindBin::libs qw( debug ); # prefix PERL5LIB with the lib's found. use FindBin::libs qw( perl5lib ); # find a subdir of the lib's looked for. # the first example will use both ../lib and # ../lib/perl5; the second ../lib/perl5/frobnicate # (if they exist). it can also be used with export # and base to locate special configuration dir's. # # subonly with a base is useful for locating config # files. this finds any "./config/mypackage" dir's # without including any ./config dir's. the result # ends up in @config (see also "export=", above). use FindBin::libs qw( subdir=perl5 ); use FindBin::libs qw( subdir=perl5/frobnicate ); use FindBin::libs qw( base=config subdir=mypackage subonly export ); # base and subonly are also useful if your # project is stored in multiple git # repositories. # # say you need libs under api_foo/lib from api_bar: a # base of the git repository directory with subdir of # lib and subonly will pull in those lib dirs. use FindBin::libs qw( base=api_foo subdir=lib subonly ); # no harm in using this multiple times to use # or export multple layers of libs. use FindBin::libs qw( export ); use FindBin::libs qw( export=found base=lib ); use FindBin::libs qw( export=binz base=bin ignore=/foo,/bar ); use FindBin::libs qw( export=junk base=frobnicatorium ); use FindBin::libs qw( export base=foobar ); DESCRIPTIONGeneral UseThis module will locate directories along the path to $FindBin::Bin and "use lib" or export an array of the directories found. The default is to locate "lib" directories and "use lib" them without printing the list.Options controll whether the lib's found are exported into the caller's space, exported to PERL5LIB, or printed. Exporting or setting perl5lib will turn off the default of "use lib" so that: use FindBin::libs qw( export ); use FindBin::libs qw( p5lib ); are equivalent to use FindBin::libs qw( export nouse ); use FindBin::libs qw( p5lib nouse ); Combining export with use or p5lib may be useful, p5lib and use are probably not all that useful together. Alternate directory name: 'base' The basename searched for can be changed via 'base=name' so that use FindBin::libs qw( base=altlib ); will search for directories named "altlib" and "use lib" them. Exporting a variable: "export", "scalar", "append"
Subdirectories The "subdir" and "subonly" settings
will add or exclusively use subdir's. This is useful if some of your lib's are
in ../lib/perl5 along with ../lib or all of the lib's are in ../lib/perl5.
These could be handled with: use FindBin::libs; use FindBin::libs qw( subdir=perl5 subonly ); which uses the "lib" dir's along with any lib/perl5 dirs. This can also be handy for locating subdir's used for configuring packages: use FindBin::libs qw( export base=config subonly=mypackage ); Will leave @config containing any mypackage dir's found up the tree, nearest to closest. The array format is convienent for locating configuration files shared between projects in separate, sibling directories. For example given: ./proj/Foo/etc ./proj/etc with use FindBin::libs qw( export subdir=etc subonly ) will export @etc with qw( ../proj/Foo/etc ../proj/etc ) in lexical order by distance from the #! code. At that point use List::Util qw( first ); my $path = first { -e "$_/Global.config" } @etc; will locate the nearest "Global.confg" file. Note that this is not the same as using "scalar" since that will export $etc with only ./Foo/etc. Setting PERL5LIB: p5lib For cases where the environment is more useful for setting up library paths "p5lib" can be used to preload this variable. This is mainly useful for automatically including directories outside of the parent tree of $FindBin::bin. For example, using: $ export PERL5LIB="/usr/local/foo:/usr/local/bar"; $ myprog; or simply $ PERL5LIB="/usr/local/lib/foo:/usr/lib/bar" myprog; (depending on your shell) with #! code including: use FindBin::libs qw( p5lib ); will not "use lib" any dir's found but will update PERL5LIB to something like: /home/me/sandbox/branches/lib:/usr/local/lib/foo:/usr/lib/bar This can make controlling the paths used simpler and avoid the use of symlinks for some testing (see examples below). Skipping directoriesBy default, lib directories under / and /usr are sliently ignored. This normally means that /lib, /usr/lib, and '/usr/local/lib' are skipped. The "ignore" parameter provides a comma-separated list of directories to ignore:use FindBin::libs qw( ignore=/skip/this,/and/this/also ); will replace the standard list and thus skip "/skip/this/lib" and "/and/this/also/lib". It will search "/lib" and "/usr/lib" since the argument ignore list replaces the original one. Homegrown Library ManagementAn all-too-common occurrance managing perly projects is being unable to install new modules becuse "it might break things", and being unable to test them because you can't install them. The usual outcome of this is a collection of hard-codeduse lib qw( /usr/local/projectX ... ) code at the top of each #! file that has to be updated by hand for each new project. To get away from this you'll often see relative paths for the lib's, which require running the code from one specific place. All this does is push the hard-coding into cron, shell wrappers, and begin blocks. With FindBin::libs you need suffer no more. Automatically finding libraries in and above the executable means you can put your modules into cvs/svn and check them out with the project, have multiple copies shared by developers, or easily move a module up the directory tree in a testbed to regression test the module with existing code. All without having to modify a single line of code.
NotesAlternativesFindBin::libs was developed to avoid pitfalls with the items listed below. As of FindBin::libs-1.20, this is also mutli-platform, where other techniques may be limited to *NIX or at least less portable.
FindBin::libs-1.2+ uses File::SpecIn order to accmodate a wider range of filesystems, the code has been re-written to use File::Spec for all directory and volume manglement.There is one thing that File::Spec does not handle, hoever, which is fully reolving absolute paths. That still has to be handled via abs_path, when it works. The issue is that File::Spec::rel2abs and Cwd::abs_path work differently: abs_path only returns true for existing directories and resolves symlinks; rel2abs simply prepends cwd() to any non-absolute paths. The difference for FinBin::libs is that including redundant directories can lead to unexpected results in what gets included; looking up the contents of heavily-symlinked paths is slow (and has some -- admittedly unlikely -- failures at runtime). So, abs_path() is the preferred way to find where the lib's really live after they are found looking up the tree. Using abs_path() also avoids problems where the same directory is included twice in a sandbox' tree via symlinks. Due to previous complaints that abs_path did not work properly on all systems, the current version of FindBin::libs uses File::Spec to break apart and re-assemble directories, with abs_path used optinally. If "abs_path cwd" works then abs_path is used on the directory paths handed by File::Spec::catpath(); otherwise the paths are used as-is. This may leave users on systms with non-working abs_path() having extra copies of external library directories in @INC. Another issue is that I've heard reports of some systems failing the '-d' test on symlinks, where '-e' would have succeded. See Also
BUGS
AUTHORSteven Lembark, Workhorse Computing <lembark@wrkhors.com>COPYRIGHTCopyright (C) 2003-2014, Steven Lembark, Workhorse Computing. This code is released under the same terms as Perl-5.20 or any later version of Perl.POD ERRORSHey! The above document had some coding errors, which are explained below:
Visit the GSP FreeBSD Man Page Interface. |