> -----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.

>
> > 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.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to