On 7/23/07, Mr. Shawn H. Corey <[EMAIL PROTECTED]> wrote:
Chris Pax wrote:
> I know that if i use: use lib "/path/to/dir" works. but when I try to
> use a variable it does not work.
> here is my code:
> @dirs = split /\// , $0;
> delete $dirs[-1];
> my $runningDir = join "/",@dirs;
> $runningDir.="/";
> use lib $runningDir;
use File::Basename;
use lib dirname( $0 );
Pay attention to the fact that the solution by Shawn does not work
also if you try to do something like this:
use File::Basename;
my $dir = dirname( $0 );
use lib $dir;
because "use lib" happens at compile-time and "$dir = dirname( $0 )"
happens at runtime. That means the expression used as argument to "use
lib" should be computed at a BEGIN block, or the variables will
probably be undef when you use them.
use File::Basename;
my $dir;
BEGIN { $dir = dirname( $0 ); }
use lib $dir;
will work. (But that's compile time also. The difference is that you
promoted some of your computation to earlier evaluation.)
If you really want to change the library path in runtime, you will need to use:
require lib; # somewhere
lib->import( $dir ); # or
push @INC, $dir; # which is just what "lib" does but with some extras
--
Just my 0.00000002 million dollars worth,
Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/