tucano has asked for the wisdom of the Perl Monks concerning the following question:
Hi
I have a strange problem with my OOPerl module and the AUTLOAD method. Let me explain.... I have a big hash that contains attributes...
Now I have an AUTOLOAD method to get and set the attributes values. Concept of AUTOLOAD is :
my %hash = $probeset -> get_exons_map I recieve an error (scalar found where list expected).
Second time no problem, I recieve thet correct type (hash)
Seems that the AUTOLOAD can't access valute type (but only the scalar pointer) at the first time! How can resolve it?
I have a strange problem with my OOPerl module and the AUTLOAD method. Let me explain.... I have a big hash that contains attributes...
Note that this attributes contains values of different type.my %_attribute_properties = ( _probeset_id => ['????', 'read.required'], ... _affy_align => [ ['????'] , 'read.write.noinit' ] _exons_map => [ { } , 'read.write.noinit' ] ... );
Now I have an AUTOLOAD method to get and set the attributes values. Concept of AUTOLOAD is :
- First Time that i call for example my %hash = $probeset -> get_exons_map AUTOLOAD INSTALL that name into the symbol table.
- Next time i call the same attributes/argument Perl code will be more fast
my %hash = $probeset -> get_exons_map I recieve an error (scalar found where list expected).
Second time no problem, I recieve thet correct type (hash)
Seems that the AUTOLOAD can't access valute type (but only the scalar pointer) at the first time! How can resolve it?
# AUTOLOAD Accesors if ($operation eq 'get') { # can i read the attribute? unless ($self -> _permissions ($attribute, 'read')) { die ("You don't have read permission for $attribute"); } # # Install accessor definition in the symbol table # How AUTOLOAD work: *{$VAR} give access to the symbol definit +ion table so, # the second time that the method get_name is called AUTOLOAD +isn't called cause # get_name is saved in the symbol table *{$AUTOLOAD} = sub { my ($self) = @_; unless ($self -> _permissions($attribute, 'read')) { die ("$attribute does not have read permission!"); } # get the attribute value # TIE-HASH da inserire qui # The attribute could be a scalar or a reference to an arr +ay or hash if (ref($self -> {$attribute}) eq 'ARRAY') { return @{$self->{$attribute}}; } elsif (ref($self -> {$attribute}) eq 'HASH') { return %{$self->{$attribute}}; } else { return $self -> {$attribute}; } }; }elsif ($operation eq 'set') { # Verify if you can set the attribute unless ($self -> _permissions($attribute, 'write')) { die ("$attribute does not have write permission!"); } # set the attribute value $self -> {$attribute} = $newvalue; # Install this mutator in the symbol table *{$AUTOLOAD} = sub { my ($self, $newvalue) = @_; unless ($self -> _permissions($attribute, 'write')) { die ("$attribute does not have write permission!"); } $self -> {$attribute} = $newvalue; }; } # Turn strict refs on use strict 'refs'; # Return the attribute value return $self -> {$attribute}; } # }}}
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: OO Perl and AUTOLOAD
by tlm (Prior) on Apr 08, 2005 at 16:44 UTC | |
Re: OO Perl and AUTOLOAD
by shemp (Deacon) on Apr 08, 2005 at 16:54 UTC | |
by tucano (Scribe) on Apr 14, 2005 at 11:48 UTC | |
Re: OO Perl and AUTOLOAD
by tilly (Archbishop) on Apr 08, 2005 at 18:43 UTC |
Back to
Seekers of Perl Wisdom