Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Calculations before using lib;

by choroba (Chancellor)
on Aug 04, 2014 at 15:06 UTC ( #1096144=note: print w/replies, xml ) Need Help??


in reply to Calculations before using lib;

To call the code during the compilation phase when use is resolved, use the BEGIN block:
use FindBin qw($Bin); my $dir; BEGIN { if ($Bin =~ /^(\/.+\/monkeyman\/bin)(\/.+)?/) { $dir = "$1/lib"; } use lib $dir; use MonkeyMan;

See perlsub for details.

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Calculations before using lib;
by ikegami (Pope) on Aug 04, 2014 at 16:10 UTC
    hum... passing undef to lib? Fixed:
    use FindBin qw($RealBin); my @extra_libs; BEGIN { if ($RealBin =~ /^(\/.+\/monkeyman\/bin)(\/.+)?/) { push @extra_libs, "$1/lib"; } } use lib @extra_libs; use MonkeyMan;
    or
    use FindBin qw($RealBin); use lib (); BEGIN { if ($RealBin =~ /^(\/.+\/monkeyman\/bin)(\/.+)?/) { import->lib("$1/lib"); } } use MonkeyMan;

    Note the switch to $RealBin in order to handle symlinks.

Re^2: Calculations before using lib;
by v_melnik (Beadle) on Aug 04, 2014 at 15:09 UTC

    Wow, thank you so much! I thought I've lost my sense %)

    V.Melnik
Re^2: Calculations before using lib;
by afoken (Monsignor) on Aug 05, 2014 at 04:40 UTC

    As perl needs to know the arguments for use lib before calling lib's import method, every expression used there is implicitly in a BEGIN block equivalent. So I often use a construct like this in my code:

    #!/usr/bin/perl use strict; use warnings; use FindBin (); use lib do { (my $dir=$FindBin::Bin)=~s|/foo/|/bar/|; $dir };

    This can also be used with taint mode to untaint the value of $FindBin::Bin:

    #!/usr/bin/perl -T use strict; use warnings; use FindBin (); use lib do { $FindBin::Bin=~m|^(/.*)| or die "Can't find myself"; "$1/ +../lib" };

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1096144]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2016-09-27 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (496 votes). Check out past polls.