develooper Front page | perl.beginners | Postings from December 2002

Re: help with classes...

Thread Previous | Thread Next
December 10, 2002 13:26
Re: help with classes...
Message ID:
I assume you have done your required reading:

in the perldocs:
         perlboot            Perl OO tutorial for beginners
         perltoot            Perl OO tutorial, part 1
         perltootc           Perl OO tutorial, part 2
         perlobj             Perl objects
         perlbot             Perl OO tricks and examples
         perltie             Perl objects hidden behind simple variables

:-) In that case you may want to check out Object Oriented Perl by Damian Conway....

As to your other question, if I understand correctly, basically you are using a closure so that the data item is accessible and updateable through the method but not by any direct name space means.  If it is not my'd then it is accessbile through the package name using a direct name, but if it is my'd then it goes out of scope and is destroyed, but if it is in a closure then it is not accessible through the namespace of the package, but doesn't get destroyed when you go beyond hte scope of the package. If that makes sense, it is all covered in the above materials, and very well in the book mentioned.

On Tue, 10 Dec 2002 15:10:25 -0600, christopher j bottaro <> wrote:

> hmm, i have more questions now.  how does one make class methods vs object 
> methods?
> using the example from the tutorial...
> package Person;
>     use strict;
>     sub new {
>         my $self  = {};
>         $self->{NAME}   = undef;
>         $self->{AGE}    = undef;
>         $self->{PEERS}  = [];
>         bless($self);           # but see below
>         return $self;
>     }
>     sub name {
>         my $self = shift;
>         if (@_) { $self->{NAME} = shift }
>         return $self->{NAME};
>     }
> what if we did Person->name()?  would it implicitly pass anything as the 1st 
> argument?  or does that only happen when an object invokes the method?  like 
> this $person->name().
> i don't see how Person->name() could pass a ref of it self as the first 
> argument cuz there >is< no object.  thus sub name() could be rewritten as:
> sub name() {
> 	my $self = shift;
> 	if (!ref($self)) { return undef; }
> 	if (@_) { $self->{NAME} = shift }
> 	return $self->{NAME};
> }
> which returns undef if it is invoked as a class method, essentially making 
> name() an object method only.
> am i making sense or am i just waaaaaay off?
> thanks,
> christopher
> P.S.  any tips on the section in the perltoot "Class Data"?  i'm not making 
> much sense of it.  so class data are just vars declared with "my" in scope of 
> the class (package)?  in the example in perltoot, $Census is like that.  but 
> the constructor for Person adds a ref of $Census into the hash.  why?  it has 
> something to do with destruction but i'm not getting it.........
> -- 
> To unsubscribe, e-mail:
> For additional commands, e-mail:

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About