
jc1742
New User
Jul 28, 2011, 1:46 PM
Post #8 of 11
(6326 views)
|
Re: [FishMonger] Accessing package variables?
[In reply to]
|
Can't Post
|
|
Then you need to write accessor/mutator subs which retrieve and/or set the vars. So you're saying that what I asked about can't be done? ;-) Adding the cpu cost of a function call to a simple variable reference is a rather large price to pay for something that, in most other language, has no time overhead. Thus, in C or C++, referencing a named field in a struct or class has no such overhead. The equivalent in perl seems to be accessing a named local variable in a package. If that requires a function ("method") call, it's a huge performance hit. Also, if this is necessary, it sorta shoots down the primary reason for exporting variables. Of what value is exporting if one still needs access subs within the package to get/set those variables' values? I'm clearly missing something about perl's OO/package design here ... ;-) But I'll point out again that the "man perlmod" page disagrees; in its first paragraph it states "You can refer to variables and filehandles in other packages by prefixing the identifier with the package name and a double colon: $Package::Variable." The reason I asked my question in the first place was that I tried exactly this (where the $Package was the value returned by one of two "new A" calls), and it failed. I also tried it with '->' in place of '::', and that failed the same way. The call on each package's newx sub worked, in a sense, showing that my test package was defined and the syntax works for subs. It just fails for simple variables, contrary to what "man perlmod" says. So did the "man perlmod" page lie to me about this? (If so, how can we punish the culprit that wrote that passage? ;-) If not, I'm at a loss as to where to go next. But I'll read some more of those docs. Several of them have names that are new to me. I wonder where I might have found references to perltooc or perltoot? They're not names that I'd have guessed to try, and aren't mentioned in my "man perl" overview page.
|