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:
- Perl has set of package tables where variables can be stored. You can access these using the fully qualified package name (eg $Foo::HELLO), or you can access the ones in the current package if they have been declared with vars, imported with Exporter, or if you're not using strict.
- Perl has an independent way to store lexical variables. These are declared with my, and do not exist in the package system. You cannot access them from outside the lexical scope where it is declared. (Well, unless you want to engage in some internals wizardry.)
- Perl has a weird hybrid declared with our. This gives lexically scoped access to a package global.
- strict.pm requires that variables be properly declared, or fully package scoped.
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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.