in reply to Re: Acceptable way to divide a long module into multiple files
in thread Acceptable way to divide a long module into multiple files

Also be aware that at least some versions of Perl are a touchy about package variables. If you split a module between two files, say "Foo_1.pm" and "Foo_2.pm", and you define a variable in "Foo_1.pm", then you have to use the fully qualified name in "Foo_2.pm", even if the current package is the same as "Foo_1.pm". This applies to both my and our variables. This issue does not apply to subroutines. They can be used without qualification in all files assigned to the "Foo" package.

Sorry, but no.

In your example try having hello() print out the value of $HELLO. You will find that Foo_2.pm did not alter it. Alternately try using our in Foo_2.pm and see that you can access the right $GOODBYE. And you really are accessing it without the fully qualified package name.

Here are the relevant facts:

So here is what happened. In Foo_1.pm you declared a lexical variable $HELLO with my. And you lexically scoped access to $Foo::GOODBYE with our. In Foo_2.pm you were out of the lexical scope of the declarations in Foo_1.pm, so you silenced any possible warning by using the fully qualified package name. This gave you access to a variable named $HELLO, but the wrong one. And you got access to the expected $GOODBYE.

All of this is documented behavior.

  • Comment on Re^2: Acceptable way to divide a long module into multiple files

Replies are listed 'Best First'.
Re^3: Acceptable way to divide a long module into multiple files
by ELISHEVA (Prior) on Jan 12, 2011 at 08:57 UTC

    Thanks, tilly. I think what confused me was that I've been viewing the package as a lexical scope that could span files. Now I see that it is only a namespace and that the largest lexical scope in Perl is a file. Do I have that right?

    To illustrate what (I think) tilly is saying (because I find it somewhat abstract):

    #--------------------------------------------------------- # Foo_1.pm use strict; use warnings; package Foo; my $HELLO='elloHay'; our $GOODBYE ='oodbyeGay'; sub hello { "hi! $HELLO -> $GOODBYE"; } 1; #--------------------------------------------------------- # Foo_2.pm use strict; use warnings; package Foo; # You are right Tilly - "our" doesn't reset $GOODBYE # and strict won't complain about an unqualified our # variable so long as we declare it our $GOODBYE; print "GOODBYE=$GOODBYE\n"; # no compiler complaints from above line # outputs: GOODBYE=oodbyeGay $Foo::HELLO='Bonjour'; #this is OK $Foo::GOODBYE='Au revoir'; #this is OK print hello() . "Hello=$Foo::HELLO, Goodbye=$Foo::GOODBYE\n"; # in the hello() sub HELLO and GOODBYE are both unqualified. # the my variable(HELLO) retains its old value, but the our # variable (GOODBYE) reflects the new value because with # or without qualification it is global (in the package table) #outputs: hi! elloHay -> Au revoir: Hello=Bonjour Goodbye=Au revoir #NOT hi! elloHay -> oodbyeGay: Hello=Bonjour Goodbye=Au revoir #NOR hi! Bonjour -> Au revoir: Hello=Bonjour Goodbye=Au revoir

    So, no bug.

      Exactly. Lexical is purely defined by the text - where the declaration is and the position of braces. Therefore the largest possible lexical scope is a file.