in reply to Re^4: Where and when you should place 'use module;'?
in thread Where and when you should place 'use module;'?

It is file scoped if it is not used within a package -- I tested this.

-M

Free your mind

  • Comment on Re^5: Where and when you should place 'use module;'?

Replies are listed 'Best First'.
Re^6: Where and when you should place 'use module;'?
by revdiablo (Prior) on Oct 12, 2005 at 15:22 UTC
    It is file scoped if it is not used within a package -- I tested this.

    That doesn't make any sense. You're almost always within a package. Can you show us how you tested this?

    Update: added clarification. Thanks Perl Mouse!

      You're always within a package.
      No, you are not.
      package; print __PACKAGE__; __END__ Use of "package" with no arguments is deprecated at ... Use of uninitialized value in print at ...
      From perldoc -f package:
      If NAMESPACE is omitted, then there is no current package, and all identifiers must be fully qualified or lexicals. However, you are strongly advised not to make use of this feature. Its use can cause unexpected behaviour, even crashing some versions of Perl. It is deprecated, and will be removed from a future release.
      Although you shouldn't, you can be outside of any package.
      Perl --((8:>*
      Well, I wasn't counting &main as a package - it certainly wasn't explicitly declared as one.

      -M

      Free your mind

        Well, main works just like any other package (and why you put & in front of it is beyond me). You still haven't shown us this test code you used.

Re^6: Where and when you should place 'use module;'?
by fishbot_v2 (Chaplain) on Oct 12, 2005 at 16:32 UTC

    Can you show us how you tested this? I think that you are mistaken. The term 'file scoped' only makes sense with respect to lexical scopes in Perl, and import doesn't have access to the caller's lexical scope unless you play around with something like PadWalker.

    Here is a counter example:

    ## file one.pl: use strict; use warnings; use Data::Dumper; require "./two.pl"; ## file two.pl: print Dumper [1,2,3]; ## output: $VAR1 = [ 1, 2, 3 ];
      ++ for thinking of require as a counter-example.

      -M

      Free your mind