> > > -----Original Message----- > > From: Wiggins d Anconia [mailto:[EMAIL PROTECTED] > > Sent: Thursday, September 16, 2004 9:03 AM > > To: Ron Goral; Perl Beginners; [EMAIL PROTECTED] > > Subject: Re: Need some help using lib to add custom directories to @INC > > > > > > Please only post to one group, if that group does not yield a good > > answer, then try a different one. Especially since this has nothing to > > do with CGI. > > > > Actually, for me this has quite a lot to do with cgi as I am using this bit > of code to load various custom modules to process choices submitted via > forms on a web site. This is especially necessary when the website does not > have it's own secure server certificate and is using a generic one owned by > the hosting company. In such a case, when the script is called via SSL, > FindBin fails to produce a relative path to the lib directory and loading > the custom module fails miserably. The "use lib substr()" concoction works. > I just need to know why for future use. >
Right I understood that, but to us it doesn't and in this context it is *us* that matters since you are requesting the use of the mailing list resources, and the CGI list is specifically for questions related to CGI programming, not general Perl how tos. There is nothing specific in your question about CGI, only that you happen to be running it in a CGI environment, which doesn't make it relevant for this list *especially* if you are going to post to the regular beginners list without waiting to even see if you get a response. > > > > > Can anyone tell me why this code fails when trying to load > > > Some_Module_In_lib_Dir- > > > ============================== > > > my $file_path = > > > > > substr($ENV{SCRIPT_FILENAME},0,index($ENV{SCRIPT_FILENAME},'/test.cgi')); > > > > The above happens at runtime, > > > > > use lib "$file_path/../lib"; > > > > The above happens at compile time, so $file_path is empty (at least I > > suspect). > > > > > use Some_Module_In_lib_Dir; > > > > > > ============================== > > > And this code does not - > > > ============================== > > > use FindBin qw($Bin); > > > > This is loaded at compile time and I suspect it sets $Bin at compile time, > > > > > use lib "$Bin/../lib"; > > > > This is then loaded at compile time with $Bin already having been set. > > > > > use Some_Module_In_lib_Dir; > > > > > > ============================== > > > And this code does not - > > > ============================== > > > use lib '/home/user/domain-www/cgi-bin/some_dir/test/../lib' > > > > Obviously this is hardcoded and loaded at compile time. Since it is just > > a string it works. > > > > > use Some_Module_In_lib_Dir; > > > > > > ============================== > > > And this code does not - > > > ============================== > > > use lib > > > > > substr($ENV{SCRIPT_FILENAME},0,index($ENV{SCRIPT_FILENAME},'/test.cgi')) . > > > "/../lib"; > > > > This is impressive, but I suspect Perl is loading the whole statement at > > compile time, and since the %ENV is already established it works. I am > > impressed (but not terribly surprised, Perl is so cool) that it knows to > > compile and execute the whole line. > > > > > use Some_Module_In_lib_Dir; > > > > > > > > > If $Bin and $file_path are printed to screen, they show values > > identical to > > > the path used in the third example and that one derived in the fourth > > > example. So, why does $Bin get added to @INC before the program tries to > > > load Some_Module_In_lib_Dir, and $file_path does not? > > > > Printing happens at runtime, but the 'use' happens at compile time, as > > do the setting of variables, *unless* they specifically happened at > > compile time for some reason. > > > > You can use C<BEGIN> blocks to when items are executed, aka if you want > > them to be run during compile time. > > > > HTH, > > > > http://danconia.org > > > > > > This helps quite a bit. THANKS!! =) I did not know, though I suspected, > that $Bin would be set at compile time. I had noticed as I printed out the > elements of @INC the path was listed using FindBin even if I printed > "before" I did the FindBin stuff in code. It makes sense that FindBin does > it's stuff before run time. I am curious though why substr() executes at > compile time and not at runtime. Is it because it is a builtin perl > function? > > Thanks again. > > C<substr> doesn't normally run during compile time, I think (don't know for sure, maybe an internals guru will chime in) that it is the special nature of following the C<use lib> that makes it happen at compile time in this case, aka the whole part after the 'lib' is essentially taken inside of a BEGIN{} to allow for this very case, which is what is so neat about Perl, aka that it allows you to do this kind of thing. Essentially the same is tru about the FindBin usage where $Bin is used, aka it has to interpolate the string, which would normally happen at runtime too, but in this case it *has* to happen at compile time (so it knows what to 'use'), and it just so happens that variables like $Bin have already been set at compile time. http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>