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

That's a bit misleading. Benchmark::cmpthese will be available in your entire program - in both packages. And so will Foo::cmpthese, for that matter. "package scope" is a bit of a misnomer, as there isn't a place where a package end (unlike a lexical scope).

I like to compare the package statement to the effects of cd. And if you do:

cd /Foo # package Foo; ln /Benchmark/cmpthese cmpthese # use Benchmark qw{cmpthese}; cd ~ # package main; ./cmpthese # cmpthese();
you get "no such file or directory". But that doesn't mean the link /Foo/cmpthese has ceased to exist.
Perl --((8:>*

Replies are listed 'Best First'.
Re^4: Where and when you should place 'use module;'?
by fishbot_v2 (Chaplain) on Oct 12, 2005 at 14:23 UTC

    I clearly state that it is the import portion of use that is scoped to the package, so I didn't think that it was misleading, but thank you for clarifying. My key point was that no aspect of a use module is file scoped, as the upthread post claimed.

    I see your point with respect to package scope, but I disagree. Scope means the area in which the symbols are immediately available and immediately effected - the current context or 'view'. In your example, the scope of an rm * is the CWD. If you cd to another directory, the scope changes, but the old scope isn't destroyed forever, it is just one step removed from the action.

    This is the difference between scope and extent. Lexicals have a dynamic extent - they persist only as long as you keep a window on that scope open. Package variables have limited scope but unlimited extent.

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

      -M

      Free your mind

        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!

        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 ];