Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Can I retry "use" when module load fail ?

by exilepanda (Monk)
on Apr 15, 2013 at 03:45 UTC ( #1028668=perlquestion: print w/ replies, xml ) Need Help??
exilepanda has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

It happens to me that when I program CGI scripts with frames or iframes, it throws 500 error by random time; however, if I reload that single frame again, it works normally. I checked the log, and it logged sort of module load fail records. I perceive that's kind of upper network connection limit from my WinXP-Pro, or some flock states triggers these errors.

Anyway, I am looking for if there's anyway I can do something like this :

eval { use stict; use warnings; use This; use That; }; while ( $@ ) { eval { use stict; use warnings; use This; use That; } } # rest of my code in main
Since I know the use statement is run first then everything else, so this code won't work. But is that anything else I can do about it? </code>

Comment on Can I retry "use" when module load fail ?
Download Code
Re: Can I retry "use" when module load fails?
by Athanasius (Monsignor) on Apr 15, 2013 at 04:02 UTC

    From the documentation for use:

    use Module LIST ... is exactly equivalent to
    BEGIN { require Module; Module->import( LIST ); }

    So, simply convert any use statements that might fail into the equivalent require statements, and your strategy of testing them dynamically using eval will then work OK.

    (BTW, there should be no need to test the pragmas use strict and use warnings in this way.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks answering!! However I ran in another problem, simplified as follow. Say that I have a module CommonPiece
      package CommonPiece; require Exporter; our @ISA =qw/Exporter/; our @EXPORT =qw/htmlStyle/; sub htmlStyle { print "<style>...</style>"; } 1; __END__

      Now in main

      use CommonPiece; htmlStyle; # this works
      However, if I change to :
      BEGIN { require CommonPiece; } htmlStyle; #Undefined subroutine &main::htmlStyle called at /...path/

      What did I missed?

        You missed the "import" part noted in Athanasius's post.

                     "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
                -- Dr. Cox, Scrubs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028668]
Approved by Athanasius
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-08-30 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls