Re: perl OOP



"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
.



Relevant Pages

  • Re: Trying to avoid passing params to subs through globals
    ... sub populate_worksheet { ... by reference and then modifying it. ... I'm not sure what you mean by 'nest subroutines'. ... returning a ref. ...
    (comp.lang.perl.misc)
  • Re: ref params
    ... The REFERENCE was passed by value. ... my DataTable param var is populated. ... that it was passed by ref, ... in C#, in order to declare a parameter as ref, you need to declare it as ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trying to avoid passing params to subs through globals
    ... things right) is to put the 'main body' of your script into a 'sub ... returning a ref. ... don't supply a reference to a reference to function ... Baby steps only for the hard of understanding. ...
    (comp.lang.perl.misc)
  • Re: ref params
    ... my DataTable param var is populated. ... C#, in order to declare a parameter as ref, you need to declare it as such. ... reference type, meaning that when you assign it, you pass the reference. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Microsoft 11.0 vs. 12.0 Object Library
    ... over a network when began to experiece a Reference Issue. ... Sub DebugPrintReferences() ... For Each ref In Access.References ... Tony Toews, Microsoft Access MVP ...
    (microsoft.public.access.modulesdaovba)