Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
The stupid question is the question not asked
 
PerlMonks  

•Re: When C<use Module;> *not* the same as C<require Module; import Module;>?

by merlyn (Sage)
on Jan 08, 2005 at 11:04 UTC ( [id://420537]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to When C<use Module;> *not* the same as C<require Module; import Module;>?

If perlfunc was correct, then calling import Module; would do that, but it often (usually?) doesn't.
Can you give an example of that? As in, show how:
use That::Module;
and
BEGIN { require That::Module; import That::Module; }
set up different symbols in the current package? This would be a useful bug to report and get fixed.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: When C<use Module;> is *not* the same as C<require Module; import Module;>?
by BrowserUk (Patriarch) on Jan 08, 2005 at 11:26 UTC

    Sure, here's the latest one I encountered.

    It'll take me a while to look up some of teh others I've encountered (and dodged) in the past.

    #! perl -slw #use HTTP::Status; require HTTP::Status; import HTTP::Status; print RC_FORBIDDEN; __DATA__ P:\test>test4 ## This is with c<use> 403 P:\test>test4 ## This is with require/import Name "main::RC_FORBIDDEN" used only once: possible typo at P:\test\tes +t4.pl line 5. print() on unopened filehandle RC_FORBIDDEN at P:\test\test4.pl line 5 +.

    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
      Notice the "print on unopened filehandle". Try it again with parens() after the name, or a BEGIN {} block. Your problem is that the symbol is being misinterpreted at compile time. I bet the import is still happening at runtime.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Try it again with parens() after the name.... Your problem is that the symbol is being misinterpreted at compile time.

        Spot on. Thanks.


        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.

      You forgot to wrap in BEGIN {}.

      #! perl -slw #use HTTP::Status; BEGIN { require HTTP::Status; import HTTP::Status; } print RC_FORBIDDEN; __DATA__ P:\test>test4 ## This is with c<use> 403 P:\test>test4 ## This is with require/import but no BEGIN Name "main::RC_FORBIDDEN" used only once: possible typo at P:\test\tes +t4.pl line 5. print() on unopened filehandle RC_FORBIDDEN at P:\test\test4.pl line 5 +. P:\test>test4 ## This is with require/import inside BEGIN 403

      Updated to add complete code listing with output.

        You forgot to wrap in BEGIN {}

        No, that was deliberate. There would be no point in not using use if I did. The whole point is to defer the loading of the module until and if it is needed.

        As constants are subs, and Perl allows you to call a sub that hasn't been declared yet, it is possible to use the constants in your code prior to them having been loaded. The bit I was missing, as merlyn pointed out above, is that it won't be recognised as a constant sub without having been previously declared or, I use the brackets().


        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://420537]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.