You asked if placing code in a package outside subroutines is "right".
I'm afraid that what is "right" is not that clear cut, it is often a matter of opinion. If you get the result you need then there is a case for saying it is "right". Whether the code is clear, easy to maintain, and efficient is another matter, and often not easy to quantify.
A BEGIN block is specifically provided for code to be executed once at compile time. Usually this is the best place to put it since it is easy to find. There are some cases where you do need code outside the BEGIN block, but not many. The bulk of a package is "usually" invoked at runtime using subroutines, not "always". | [reply] |
I personally believe that you're confused (and incidentally the way you like to write tends to confirm that you are:) people clearly told you that you can have code out of subs in modules. That is, it's valid Perl. People generally use modules as collections of reusable code: subs to be imported as additions to the builtins or methods of some class. There's no reason they shouldn't also do things. Indeed, they often have to, to "initialize" in some way or another those collections of code. For example, think of a module which will connect to some site to retrieve some info and build suitable "constants" - it's not that strange...
| [reply] [d/l] |