CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate: Re: [jc1742] Accessing package variables?: Edit Log

Veteran / Moderator

Jul 28, 2011, 5:52 PM

Views: 7312
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.

package Example; 

use strict;
use warnings;

sub new {
my ($class) = @_;

bless {
var1 => 'var one',
var2 => 'var two',
var3 => 'var three',
}, $class;



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)

Edit Log:
Post edited by FishMonger (Veteran) on Jul 28, 2011, 5:53 PM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives