> 
> Hello Li
> 
> About the _permitted hash key, the perltoot tutorial
> says
> 
> + I could have avoided the "_permitted" field
> entirely, but I wanted to
> + demonstrate how to store a reference to class data
> on the object so you
> + wouldn't have to access that class data directly
> from an object method.
> 
> So the point of the class data %fields is to
> restrict autoloaded methods to the
> expected ones, while the point of putting a
> reference to it in the object data
> is, as Tom says, to demonstrate how class data can
> be accessed indirectly from
> within an object method.
> 
> Preventing the module from autoloading non-permitted
> methods has the same
> purpose as 'use strict "vars"', i.e. to prevent a
> misspelled name producing
> obscure runtime bugs. Without the _permitted hash, I
> could write:
> 
>    my $me = new Person;
>    $me->nane('Rob');
>    die unless $me->name eq 'Rob';
> 
> and I could be perplexed for a while over why it
> kept dying. Adding the check,
> the AUTOLOAD method would croak and immediately
> explain the problem.
> 
> With regard to the reference to class data, later on
> in the tutorial there is a
> class Person with a class scalar $Census which keeps
> track of the number of
> objects that have been created. This scalar can be
> accessed via the 'population'
> method. Using the same technique, the 'new' and
> 'population' methods could be
> rewritten:
> 
>    my $Census = 0;
> 
>    sub new {
> 
>      my $class = shift;
> 
>      my $self  = {
>        NAME  => undef,
>        AGE   => undef,
>        PEERS => [],
>        _census => \$Census,
>      };
> 
>      ${$self->{_census}}++;
> 
>      bless ($self, $class);
>    }
> 
>    sub population {
> 
>      my $self = shift;
> 
>      return ${$self->{_census}};
>    }
> 
> so that the access method has no need to refer
> directly to the package variable,
> in the same way that your AUTOLOAD method needn't
> use the %fields hash
> explicitly. I believe this is what Mumia meant about
> inheritance: if the method
> had directly accessed %fields hash then a subclass
> like Employee would have no
> way of reaching it, but once a reference to it
> appears in the object data then
> all is well once more.
> 
> I hope this makes things clearer for you.
> 
> Rob
> 
> (After reading the tutorial further I realise that
> Tom Christiansen has made a
> very similar adaptation to the one I have described
> above. The main difference
> is that the 'population' method is written so that
> it can also be called as the
> class method Person::population as well as an object
> method $person->population.
> I hope this doesn't confuse you.)
> 

Thanks Rob. I think now I get the points.

Li

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to