
FishMonger
Veteran
Jul 28, 2011, 5:52 PM
Views: 2068
|
|
Re: [jc1742] Accessing package variables?
|
|
|
Ok, I'm home now, but need to keep this short because I need to cook dinner.
So you're saying that what I asked about can't be done? ;-) No, that's not what I'm saying, but directly accessing/modifying a variable in a Perl OO module is discouraged in part because it breaks encapsulation. Yes, executing a method call does have a very small overhead and in most cases that overhead is very negligible, unless you're planning on calling the same method millions of times to access the exact same data and if you're doing that, you have a problem with code logic. Here is a minimal example of an OO module approach that does what you what. Example.pm
package Example; use strict; use warnings; sub new { my ($class) = @_; bless { var1 => 'var one', var2 => 'var two', var3 => 'var three', }, $class; } 1; example.pl
#!/usr/bin/perl use strict; use warnings; use Example; my $obj = Example->new; print $obj->{var3}, $/; $obj->{var3} = 'new value'; print $obj->{var3}, $/;
(This post was edited by FishMonger on Jul 28, 2011, 5:53 PM)
|