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: Advanced:
Dynamic Perl Module Loading

 



fashimpaur
User / Moderator

Jun 20, 2002, 9:11 AM

Post #1 of 4 (1554 views)
Dynamic Perl Module Loading Can't Post

Is there any way to dynamically, or better yet, selectively, load perl modules?

Example:

I have an application which runs from either a web server or from the command
line.

What I would like to do:

Code
 use strict; 
use someOtherPackage;


if (defined $ENV{'HTTP_USER_AGENT'}){
use CGI;
use CGI::Pretty;
use CGI::Carp 'fatalsToBrowser';
}


# ... logic for command line and browser interfaces

However, when I do this, if a fatal error occurs when running it from the
command line, all errors are displayed with HTML tags invoked by CGI::Carp.

So, therefore, all modules are getting loaded even though I requested them
to be used only if there is an HTTP_USER_AGENT.

Is there a way to do only load the modules if they are truly required?

Any assistance would be greatly appreciated.
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


uri
Thaumaturge

Jun 20, 2002, 7:48 PM

Post #2 of 4 (1551 views)
Re: [fashimpaur] Dynamic Perl Module Loading [In reply to] Can't Post

use is a compile time operation and you can't directly control it with conditional logic. require is a runtime operation and does what you want regarding loading. but it doesn't call the module's import function which is what use does and what allows the cgi fatals to work. it needs to intercept output to stderr to reformat it for the web.

so try this:

Code
if ( blah ) { 
require CGI::Carp;
CGI::Carp->import( 'fatalsToBrowser') ;

etc.

another method which i don't prefer is to use string eval. it looks simpler but i just don't like seeing it used when it is not needed. showing string eval is not something i will do here.


yapp
User

Jun 27, 2002, 4:19 AM

Post #3 of 4 (1537 views)
Re: [fashimpaur] Dynamic Perl Module Loading [In reply to] Can't Post

If you're planning to use CGI::Carp at runtime, it won't catch the compile-time errors.

Maybe (but I don't really like such idea), you can put all other (CGI) parts of your program in a different file,

Second, I think it's better to check for the existance of $ENV{'REQUEST_METHOD'} This variable is provided by the webserver, and a browser (or telnet) can omit the user-agent header in the request.


Code
if(exists $ENV{'REQUEST_METHOD'}) 
{
eval
{
require 'the_rest_of_the_program.pl';
};

if($@)
{
# Output the message, that you failed to load the file...
require CGI;
my $cgi = new CGI();
print $cgi->header();
print qq'<H1>Error</H1> $@';
}
}
else
{
print "You can only run this program from CGI environments.\n";
}


In my code, I use an eval, but this is a BLOCK eval.. to catch the errors. That's a different eval then URI referred to. I'm also not so keen in the other eval version. This block eval however, just trapps errors, not executes strings as Perl code. You can call this the Perl alternative for try...catch in other languages.

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


yapp
User

Jun 27, 2002, 4:24 AM

Post #4 of 4 (1534 views)
Re: [uri] Dynamic Perl Module Loading [In reply to] Can't Post


In Reply To
another method which i don't prefer is to use string eval. it looks simpler but i just don't like seeing it used when it is not needed. showing string eval is not something i will do here.


I totally agree, so I hope you can solve my problem.

I need to load a module dynamically, but I only have a string (based on the input parameters of the script). I can't use that in the require, because require expects that strings reffer to filenames, but I want to use the "platform-independant Perl module name notation".

Any thought?

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

 
 


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

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