Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Can I retry "use" when module load fails?

by Athanasius (Prior)
on Apr 15, 2013 at 04:02 UTC ( #1028669=note: print w/ replies, xml ) Need Help??


in reply to Can I retry "use" when module load fail ?

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,


Comment on Re: Can I retry "use" when module load fails?
Select or Download Code
Re^2: Can I retry "use" when module load fails?
by exilepanda (Monk) on Apr 15, 2013 at 04:52 UTC
    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

        I noticed... but why? I have nothing to do with import. I already used Exporter to export my stuffs. so if use is identical to BEGIN {require...}, then that should be no problem sub import{} never existed in my module, and then I won't call it. What's the difference?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1028669]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-07-11 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (219 votes), past polls