http://qs321.pair.com?node_id=209900

cal has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, Earlier today I recieved several options on how to force a script to use modules that I select from my local cgi bin rather than using the perl modules from the server.

None of them work. Obviously I am missing some huuuuge detail in these examples.

Please Help Me understand

The only version that prints for GD is the version on the default (1.18)      use GD;
Why can't the scripts ever see the other modules.

The other version that is located in my cgi-local directory is 1.27

Browser UK was kind enough to help me with the script that prints out which version of a module is currently being used. Thanks again to him!

#!/usr/local/bin/perl -w use CGI ':standard'; #use GD; #version GD 1.18 #use lib qw(/u/web/dom/cgi-local/GD); # EMPTY #unshift @INC, '/u/web/dom/cgi-local'; # EMPTY <BR><BR> #use FindBin; #Internal Server Error #use lib $FindBin::Bin; #Internal Server Error #use lib '/u/web/dom/cgi-local/GD'; # EMPTY print header, html( h1( "CGI: $CGI::VERSION GD: $GD::VERSION" ) );

Replies are listed 'Best First'.
Re: Using the modules I want to use.
by BrowserUk (Patriarch) on Nov 02, 2002 at 06:26 UTC

    cal perlfunc:use docs say

    It is exactly equivalent to BEGIN { require Module; import Module LIST; }

    I discovered earlier today (yesterday) that one difference with require is that you can use a full path to the module
    (eg. '/u/web/dom/cgi-local/GD.pm') instead of the bareword that use insists on.

    I am not sure if there are any caveats with doing this yourself using BEGIN{ require '/path/to/module.pm'; } but it is documented and it does work. Maybe you could try this until or unless someone identifies reasons why it shouldn't be done.


    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
Re: Using the modules I want to use.
by chromatic (Archbishop) on Nov 02, 2002 at 06:24 UTC

    To add to what Aristotle said, the reason unshifting a path onto @INC didn't work is because use happens at compile time, not at run time. Perl first reads your program and decides what to do with it (compile time). Then it executes it, in order. use happens at compile time. unshift happens at run time.

    First you have to tell Perl where to look. Then, you tell it what to find.

Re: Using the modules I want to use.
by Aristotle (Chancellor) on Nov 02, 2002 at 06:14 UTC
    This should work:
    #!/usr/local/bin/perl -w use lib '/u/web/dom/cgi-local/'; use CGI ':standard'; use GD; print header, html( h1( "CGI: $CGI::VERSION GD: $GD::VERSION" ) );
    You need to modify the @INC path before you load the modules. Also, if you have a module Foo::Bar residing in /some/dir/Foo/Bar.pm you must specify /some/dir/ as the path, without the Foo/ part.

    Makeshifts last the longest.

      Thanks for your help,

      [Sat Nov  2 01:34:17 2002] [error] [client 12.234.205.16] Premature end of script headers: /u/web/dom/cgi-local/whichversion.cgi

      What happens to the modules that you want to include from the server library?

      Cal

        Hmm. Add use CGI::Carp; and check the error log again. There should be a more detailed error message then.

        Also, run with perl -c whichversion.cgi on the shell if you can.

        Makeshifts last the longest.

        It appears that the use lib line in that loaction causes the "premature end of script" errors, Cal
      Well if I put a colon right before the path on the second line there is no error. However I still only see version 1.18..
      #!/usr/local/bin/perl -w<br> use lib ':/u/web/smpsvc/cgi-local';<br> use CGI::Carp;<br> use CGI ':standard';<br> use GD;<br> print header, html( h1( "CGI: $CGI::VERSION GD: $GD::VERSION" ) );<br>

      Cal
Re: Using the modules I want to use.
by JupiterCrash (Monk) on Nov 02, 2002 at 16:33 UTC
    I've found it helpful that the 'use' command accepts a version number. If the module in my cgi-bin is newer than the one in the system's perl library directory, something like this might help you to know. This is good if you might not want your cgi-bin path to be FIRST in the @INC, but just *in* the @INC :
    use DBI 1.20;
      Thanks, When I use this use GD 1.27; I get Internal Server Error. [Sat Nov  2 13:22:10 2002] [error] [client 12.234.205.16] Premature end of script headers: /u/web/dom/cgi-local/whichversion.cgi
        I'd guess that whicversion.cgi is sending output without an http header. If that's right then you need either
        use CGI qw/:standard/; print header;
        or just
        print "Content-type: text/html\n\n";
        before any print commands in your script, which will allow the output from the script to appear in the browser window.

        § George Sherston