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: Beginner:
Trying to do conditional use statements

 



srhadden
Novice

Mar 6, 2012, 2:25 PM

Post #1 of 5 (1247 views)
Trying to do conditional use statements Can't Post

I'm working on some CGI, and I want to have a site-wide debug on/off. I thought about using the really nice module CGI::Carp because it will output errors to the web site and I don't have to look in the apache logs.

Anyway, I wanted to only include the module if a variable is set. But I am having a basic problem.

This does't seem to work. I think there is a problem with passing options to the module.

eval "use CGI::Carp \"fatalsToBrowser\"";

rint "test"; #intention syntax error

If it is working properly, the perl code will generate an html page with the syntax error. But it doesn't.

Is there a way to test if a module is loaded and then pass an argument to it? That was a workaround idea I had.


FishMonger
Veteran / Moderator

Mar 6, 2012, 2:51 PM

Post #2 of 5 (1245 views)
Re: [srhadden] Trying to do conditional use statements [In reply to] Can't Post

See: perldoc -f require


srhadden
Novice

Mar 6, 2012, 3:06 PM

Post #3 of 5 (1244 views)
Re: [FishMonger] Trying to do conditional use statements [In reply to] Can't Post

Is there a syntax error with my eval use statement?

It works with simple cases like:

eval "File::Basename";

But if I try to pass arguments using eval, it doesn't like it.


FishMonger
Veteran / Moderator

Mar 6, 2012, 5:03 PM

Post #4 of 5 (1237 views)
Re: [srhadden] Trying to do conditional use statements [In reply to] Can't Post

Putting the use statement to load CGI::Carp in an eval statement/block would be the wrong approach if you're wanting conditional loading of the module, but if you want to do that, then the proper syntax would be:

Code
eval { use CGI::Carp "fatalsToBrowser"; } ;


All 'use' statements are executed during the compilation phase prior to the evaluation of any conditional statements/blocks that they may be within.

The require statements are executed at runtime, so they can be placed inside conditional blocks and only loaded if needed.

If you want a more robust approach to conditional loading of modules, then you should look at using Module::Load::Conditional.
http://search.cpan.org/~bingos/Module-Load-Conditional-0.46/lib/Module/Load/Conditional.pm


srhadden
Novice

Mar 7, 2012, 10:58 AM

Post #5 of 5 (1197 views)
Re: [FishMonger] Trying to do conditional use statements [In reply to] Can't Post

Gotcha. Thank you!! Can't ask for more that than ;)

 
 


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

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