in reply to code that runs at module loading
From your description, I'd say what you're doing is 100% ok. Putting "my" variables in the module is just normal modular data hiding and should work fine with both "use" (compile time) and "require" (run time) in the common case where you don't need the data to be known at compile time. If you need it at compile time (e.g. to push onto @INC), simply enclose in a BEGIN block.
Since data hiding is a good thing, be sure to declare these "in the wild" variables in the smallest scope you can. In simple cases, organise your module so that the "my" globals and the functions that use them are stuffed at the very end of the file. Alternatively, put a "my" variable and the function/s that use it together in a bare block. For example:
Note that fred() is globally known, while %some_global is not. Again, this is just plain old hiding of data into the smallest scope you can. Notice that this is a simple "modular" style of programming. As a good rule of thumb, ask the question "do I need more than one of these?"; if the answer is yes, use O-O style, otherwise you can often get away with the simpler modular style described above.# some module code # ... { my %some_global = ( ... ); sub fred { # fred() is the only function in the module # that uses %some_global # ... } } # the %some_global variable is not in scope here. # ... rest of module
|
|---|