BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

According to perlfunc, use Module;

[] is exactly equivalent to

BEGIN { require Module; import Module LIST; }

except that Module must be a bareword.

I've tried this on several occasions with several different standard/core modules, and it isn't!

When I use these modules, they export, by default, some subset of their potential exports, which for the simple case is exactly what I want.

When I have need to defer the loading of a module until (and if) it is required--with require--I would like to not have to work out which set of things I going to have to import individually, or even which :tag will give me the default set.

If perlfunc was correct, then calling import Module; would do that, but it often (usually?) doesn't.

Without getting into the specifics of any particular module, is it possible to explain why this is so, and what can be done about it?


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

Replies are listed 'Best First'.
•Re: When C<use Module;> *not* the same as C<require Module; import Module;>?
by merlyn (Sage) on Jan 08, 2005 at 16:04 UTC
    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.

      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.

        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.

Re: When C<use Module;> *not* the same as C<require Module; import Module;>?
by borisz (Canon) on Jan 08, 2005 at 16:16 UTC
Re: When C<use Module;> *not* the same as C<require Module; import Module;>?
by etcshadow (Priest) on Jan 09, 2005 at 02:12 UTC
    You've actually hit on the reason why use is automatically treated as a compile-time directive (i.e., wrapped in a BEGIN{...} block): it alters the parser's syntactic interpretation of code. Specifically, there are places where the same piece of code can have ambiguous syntactic interpretations, depending on whether or not certain symbols are known to be subroutines.

    For example, take:

    print foo;
    If foo is already known by the perl parser to be a subroutine, it will interpret this as meaning:
    print(foo());
    However, if the interpreter does not know that foo is supposed to be a subroutine, then it will interpret foo to be the name of a file-handle (the indirect argument to print). This is all happening at compile-time (well, parse-time, really... but parse and compile happen together), so it doesn't matter whether or not foo is a subroutine at runtime... because the parser will have already decided that it was being used as the name of a filehandle, at runtime.
    ------------ :Wq Not an editor command: Wq
Re: When C<use Module;> *not* the same as C<require Module; import Module;>?
by ysth (Canon) on Jan 10, 2005 at 09:58 UTC
    In the title, you leave out BEGIN{}. Having imported subroutines at compile time can make a difference as to how the following code is parsed.

    More properly, use Module [numeric-constant] [LIST] is equivalent to

    BEGIN { require Module; Module->VERSION(numeric-constant); # omitted if no constant given Module->import(LIST); # omitted if LIST is explicit () }