Re: perl OOP
- From: Uri Guttman <uri@xxxxxxxxxxxxxxx>
- Date: Tue, 03 Apr 2007 13:38:43 -0400
"XC" == Xiong Changnian <please@xxxxxxxxxx> writes:
XC> Most methods are object methods. They are expect params of the form:
XC> package Bottle;
XC> sub name_me {
XC> my $self = shift;
XC> my $name = shift;
XC> ... and Perl considerately passes $object as the first param in @_,
XC> later to be shifted into $self. Note that the return value can be thrown
XC> away. The method acts on the $object passed in.
XC> sub new {
XC> my $class = shift;
XC> my $name = shift;
it is much simpler and more consistant to say that the class or object
is always passed to any method as its first argument. it is the act of
making a method call (class or object doesn't matter) that does this.
XC> It may be more clear to use this alternate syntax, with the same effect:
XC> my $object = new Bottle ($name);
no, that is considered a bad idea. it is deprecated in the docs and was
even mentioned recently here. i always forget which doc discusses why
indirect method calls are bad.
XC> In any case, the effect is the same. The $class (Bottle) is what is
XC> passed as the first param in @_, to be used to bless the new object. The
XC> object is what is returned by the method.
if you always use a direct call then the rule is even easier. the part
before the -> is ALWAYS passed as the first argument. it can be either
an object or a class name. simple.
XC> Two key points of OO technique bear underlining:
XC> * The $object is (usually) a reference to an anonymous hash. Inside our
no. it is always a blessed reference. what reference type is independent
and there are reasons to use types other than hashes. inside-out objects
usually use blessed scalar refs which actually have no data in
them. again this topic has been discussed here many times.
XC> methods above, we may assign this reference to $self but the reference
XC> still points to a single "thing". With conventional subs, we generally
XC> pass a param and act on its value only; the original param is untouched.
XC> The intent of OO technique is to act on the object directly.
not if you pass a ref param to a sub. even you discussed this in the
threads about @_, aliasing and pass by ref/value. the real diff is that
objects are always passed as a ref since only refs can be blessed. so in
general method operate on data in the object and can modify it. plain
subs can be passed anything and they may or may not modify its args
depending on the API and such.
XC> * It all hinges on bless(). The purpose of the new method is to bless
XC> $self, the reference to the anon hash -- to tell Perl to associate
XC> $class with the underlying object. Future calls, say, to name_me or any
s/object/class/. blessing a ref makes it know what class it belongs too
and the ref is now an object.
uri
--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.
- References:
- perl OOP
- From: a
- Re: perl OOP
- From: Xiong Changnian
- perl OOP
- Prev by Date: Re: parsing text file into array
- Next by Date: Re: Internalisation support and dictionaries
- Previous by thread: Re: perl OOP
- Next by thread: Re: perl OOP
- Index(es):
Relevant Pages
|