CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Push paths to @INC array

 



hacksics
Novice

Dec 25, 2008, 8:53 AM

Post #1 of 9 (860 views)
Push paths to @INC array Can't Post

Hi everyone,

Is there any way to push a variable to @INC array in perl. I've noted if the full path is given then it accepts but a variable is given then it ignores,

Ex:

Code
push @INC, "/home/user_x/custom_libs"; # working 


my $LIB_PATH = "/home/user_x/custom_libs";
push @INC, $LIB_PATH; # not working

Thanks,


shawnhcorey
Enthusiast


Dec 25, 2008, 9:20 AM

Post #2 of 9 (856 views)
Re: [hacksics] Push paths to @INC array [In reply to] Can't Post


In Reply To
Hi everyone,

Is there any way to push a variable to @INC array in perl. I've noted if the full path is given then it accepts but a variable is given then it ignores,

Ex:

Code
push @INC, "/home/user_x/custom_libs"; # working 


my $LIB_PATH = "/home/user_x/custom_libs";
push @INC, $LIB_PATH; # not working

Thanks,


See `perldoc lib`.

Code
use lib "/home/user_x/custom_libs";


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


hacksics
Novice

Dec 25, 2008, 9:27 AM

Post #3 of 9 (855 views)
Re: [shawnhcorey] Push paths to @INC array [In reply to] Can't Post

Yeah,

Even If I use,


Code
my $LIB_PATH = "/home/user_x/custom_libs"; 
use lib "$LIB_PATH";


It does not get the lib path defined in the variable. Am I missing anything here?

Thanks


shawnhcorey
Enthusiast


Dec 25, 2008, 9:41 AM

Post #4 of 9 (851 views)
Re: [hacksics] Push paths to @INC array [In reply to] Can't Post


In Reply To
Yeah,

Even If I use,


Code
my $LIB_PATH = "/home/user_x/custom_libs"; 
use lib "$LIB_PATH";


It does not get the lib path defined in the variable. Am I missing anything here?

Thanks


Perl has two phases: compile and run-time. `use` is executed at compile time, variable assignment at run time. You have to get it to assign the value at compile time:

Code
BEGIN { 
my $LIB_PATH = "/home/user_x/custom_libs";
use lib $LIB_PATH;
}


Note that $LIB_PATH is not accessible outside the block. To have it so:

Code
BEGIN { 
our $LIB_PATH = "/home/user_x/custom_libs";
use lib $LIB_PATH;
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


hacksics
Novice

Dec 25, 2008, 10:12 AM

Post #5 of 9 (848 views)
Re: [shawnhcorey] Push paths to @INC array [In reply to] Can't Post

Hi Shawn,

Thank you so much for your quick response. But now it gives me a compile time error. can you please have a look

my code:

Code
BEGIN { 
our $LIBS = "$ENV{'HOME'}"."/Libs";
use lib "$LIBS";
}


Error returned by perl

Code
Empty compile time value given to use lib at /highway/rmon/home/rtool line 14


Thanks,


hacksics
Novice

Dec 25, 2008, 10:20 AM

Post #6 of 9 (845 views)
Re: [hacksics] Push paths to @INC array [In reply to] Can't Post


In Reply To
Hi Shawn,

Thank you so much for your quick response. But now it gives me a compile time error. can you please have a look

my code:

Code
BEGIN { 
our $LIBS = "$ENV{'HOME'}"."/Libs";
use lib "$LIBS";
}


Error returned by perl

Code
Empty compile time value given to use lib at /highway/rmon/home/rtool line 14


Thanks,


But this works without any issue,


Code
BEGIN { 
our $LIBS = "$ENV{'HOME'}"."/Libs";
push @INC, $LIBS;
}



shawnhcorey
Enthusiast


Dec 25, 2008, 10:22 AM

Post #7 of 9 (844 views)
Re: [hacksics] Push paths to @INC array [In reply to] Can't Post


In Reply To
Hi Shawn,

Thank you so much for your quick response. But now it gives me a compile time error. can you please have a look

my code:

Code
BEGIN { 
our $LIBS = "$ENV{'HOME'}"."/Libs";
use lib "$LIBS";
}


Error returned by perl

Code
Empty compile time value given to use lib at /highway/rmon/home/rtool line 14


Thanks,


It means $ENV{HOME} is undef. This is what I have in my Perl script template:

Code
use FindBin '$RealBin'; FindBin::again(); 
use lib glob( '~/lib' ), $RealBin;


But that only works in Linux. :)

You could try:

Code
use FindBin '$RealBin'; FindBin::again(); 
use lib glob( '~/Libs' ), $RealBin;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


hacksics
Novice

Dec 25, 2008, 10:29 AM

Post #8 of 9 (842 views)
Re: [shawnhcorey] Push paths to @INC array [In reply to] Can't Post


Code
BEGIN {  
our $LIBS = "$ENV{'HOME'}"."/Libs";
push @INC, $LIBS;
}


This works without any error and I'm using solaris. :)

Thank you so much for saving my day :)


KevinR
Veteran


Dec 25, 2008, 10:41 AM

Post #9 of 9 (839 views)
Re: [hacksics] Push paths to @INC array [In reply to] Can't Post

a little bit better written like so:


Code
BEGIN {   
our $LIBS = "$ENV{'HOME'}/Libs";
push @INC, $LIBS;
}

-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives