in reply to Whether to use local()
local() gives a local value to a global variable. A local variable is visible to called subs but not calling subs. It seems to me that it would be difficult to use local to have class data. I think that this is one of those cases where a global is the correct solution to a problem. One shouldn't use globals to pass data to a subroutine - it's bad form, and it's confusing. However, since a class is a package in Perl, then a class global is the best way to store class data. Declaring the global with my makes it impossible for anyone else to accidentally change the variable outside of the lexical scope. I would also probably use a hash to make it very clear where the data is coming from:
package Class::Foo; my %ClassData = (); #methods here
Note however, that you have to be very careful if you are putting the class in the same file as the code which uses it - as a file (and NOT a package) delimits lexical scope, your class data might leak into the main program even if declared with my.
Cheers,
|
|---|