On Fri, 28 Oct 2011 07:23:16 -0400
John SJ Anderson <[email protected]> wrote:
>
>
> On Friday, October 28, 2011 at 07:05 , Gary wrote:
>
> > sub new {
> > my ($class, $file_name) = @_;
> > my $self = {
> > _file_name => $file_name,
> >
> > _cfg => {} # <--<< the hash i want to access
> > };
> > bless $self, $class;
> > return $self;
> > }
> >
> > but I can't work out how to access self->_cfg and its elements.
> >
>
>
> What you get back from calling this 'new' method is just a hash ref -- it's a
> jumped-up hash ref that knows it belongs to a particular class, but
> underneath, it's still ultimately just a hash ref, so you use the standard
> 'dereference with the bracket type appropriate for the underlying variable
> type' syntax, i.e., curlies.
>
> my $thing = CLASS->new();
> $thing->{_cfg}{key} = 'value';
>
> There are more elaborate ways to do this, and an argument to be made that
> directly accessing stuff like this violates your object encapsulation. I'm
> pretty sure somebody else will spell those out in more detail sooner or
> later. 8^)
>
For the how and why of accessors, see:
http://www.shlomifish.org/lecture/Perl/Newbies/lecture5/accessors/
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg
“You are banished! You are banished! You are banished!
Hey! I’m just kidding!”
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/