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

I am curious as to how global variables in packages are kept. For instance..
Package Pack; my $image; sub one { ($image)=@_; } sub two { print $image; } ##main code use Pack; Pack::one("param"); my $ret=Pack::two(); print $ret;
This code prints "param". I guess I'm confused as to how instances of packages are created. I assumed that the two calls to subs one and two in the main code above were separate and that the variable image would not retain its value between the two calls.
How does package instantiation occcur? Is one instance of the package create everytime you use the "use" function?
Can I also assume that global variables in packages will stay the same as in the example above? Thanks.

Replies are listed 'Best First'.
Re: Instance Module
by goldclaw (Scribe) on Jan 17, 2001 at 22:35 UTC
    The whole thing about my variables is a bit difficult to get your head around. Advanced Perl Programming does a wonderful job of explaining it. This is some of the wisdom I think I have gained on the subject...not 100% sure of course....

    Basically, a my variable is a lexical varaible. It has nothing to do with packages, so forget about that. Think in terms of lexical scope. A file is a lexical scope. A block is a lexical scope( whatever is inside a pair of { } ). A package is _not_ a lexical scope. It just seems that way, because everyone usually puts one and only one package in a file, basically creating a lexical scope for that package.

    So, for your specific example, put my $image inside sub one, and you are game. If you want it to persist each time you call one, do it like this:

    Package Pack; { my $image; sub one { ($image)=@_; } } sub two { print $image; }
    Notice the extra set of braces. We have just created a scope for $image, so that one can see it, but two can't. Hope this clears things up a bit, goldclaw
Re: Instance Module
by chipmunk (Parson) on Jan 17, 2001 at 22:44 UTC
    To expand on the previous answers... There is only ever one instance of a package; a given package only has one set of variables. (local can be used to temporarily give different values to those variables.)

    Thus, you would see the same results if you were using a package variable instead of a lexical variable:

    package Pack; use vars qw/$image/; sub one { ($image)=@_; } sub two { print $image; } ##main code use Pack; Pack::one("param"); my $ret=Pack::two(); print $ret;
    Regarding use: the first time use Module occurs, the module is found, compiled and executed, and the module's import() method is called. The second time use Module occurs, Perl knows (by keeping track in %INC) that this module has already been loaded; the import() method is called but the rest of the process is skipped.
Re: Instance Module
by arturo (Vicar) on Jan 17, 2001 at 22:16 UTC

    The basic problem here : my variables are not global, they don't belong to any package. Your call to two returns the current value of $image, which (on your call to one is set to "param"). In other words $image that you declared with my is still in scope in two. If you wanted two to have its own $image, you'd have to use my in the subroutine. That's why you're seeing that behavior. (out of curiosity, and it might help conceptually, could you tell us what were you expecting to see, BTW?)

    Packages are not instances; you can think of them as "last names" (i.e. things that help you distinguish this 'Bob' from that 'Bob'). You might be confusing packages with objects; an object is a reference to a Perl data stucture (even a subroutine!) that's been blessed *into* a package. (If you're trying to do some OOP in Perl, you can start with various monks' tutorials in the Tutorials section on this site, or with perldoc perltoot on your own system)

    Also, to access the variables defined in Pack, you don't use it, you just say $Pack::image (e.g. ... and note here that won't work because there is no such variable defined, because the $image you've created isn't in the Pack package!)

    I have a (partial) scoping tutorial here that might help clear this up. IF you read that, though, I'd (a) pay attention to the comments that follow and (b) follow the "Coping with Scoping" link because that's more accurate.

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor