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 16: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.