> -----Original Message-----
> From: Charles K. Clarkson [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, 25 February 2004 5:05 PM
> To: [EMAIL PROTECTED]
> Subject: Constructors (was: RE: deleting a hash ref's contents)
> 
> Hello all,
> 
>     The following thread was part of a reply written a while back.
> 
> 
> R. Joseph Newton <[EMAIL PROTECTED]> wrote:
> : 
> : Gary Stainburn wrote:
> : 
> : > sub new {
> : >   my $this=shift;                # allow for CLASS->new()
> : >   my $class=ref($this) || $this; # or $obj->new();
> : 
> : The docs that suggested this are in the process of being 
> : deprecated.  It is not a good idea to have objects create
> : other objects with new().
> : I'll have more later on this, if others don't fill in the
> : blanks in the meantime.
> 
> 
>     I didn't notice anyone filling in the blanks here. As a
> practical exercise, I'll create an object oriented version of
> File::Basename::fileparser(). I'll call the module
> MyFileBasename.pm.
> 
>     When writing the new constructor, do I just ignore $class
> or is there some better (or more preferred) way to write it?
> 
> 
> package MyFileBasename;
> 
> use File::Basename;
> 
> sub new {
>     my $class = shift;
>     my $self = {
>         file_name   => undef,
>         base        => undef,
>         path        => undef,
>         type        => undef,
>     };
>     bless $self;
>     $self->initialize( @_ ) if @_;
>     return $self;
> }
> 

What you are doing here (in MyFileBasename) is implementing a new class
which 'use's the File::Basename module.  You don't even give an example
of
using its functions.  (since File::Basename is not an 'oo' module)


I always believed that the point of the two parameter constructor
was for my module to implement 'new' for itself (P), even if another
module
Inherits it, and overrides interface methods.

Contrived example below:

---------------------------------------------
P.pm:
---------------------------------------------

package P;
require Exporter;
@ISA=qw(Exporter);

sub new
{
        my $class = shift;
        bless {}, ref($class)||$class
}

sub baz
{
        my $self = shift;
        print "P baz".$/;
}

sub foo
{
        my $self = shift;
        print "foo. calling baz".$/;
        $self->baz();
}

1;


---------------------------------------------
Q.pm
---------------------------------------------


package Q;
use P;
require Exporter;
@ISA=qw(P);

sub baz
{
        my $self = shift;
        print "Q baz".$/;
}

1;

---------------------------------------------
tester.pm
---------------------------------------------

use Q;

my $a = new Q;  <--- Uses 'P::new' to create a 'Q' object

$a->foo();              <--- Calls 'P::foo' which calls 'Q::baz'

$b = $a->new;   <--- Calls 'P->new' through inheritance, VIA a 'Q'
object, hence a
                             new 'Q' object is created.

$b->foo();

---------------------------------------------

This will produce as output:

foo. calling baz
Q baz
foo. calling baz
Q baz

The important lesson here it,
1) The 'new' metho was able to create an object even if the object was
   a subclass. (2 parameter form of bless())
2) The 'new' method was able to instantiate an object of unknown type
just
   by having an existing reference. (use of ref($class)||$class )

The value in (2) is that a copy constructor for example could call an
objects
'new' method, and effectively retrieve a new object of the same type
without
knowing the original objects type in the first place.  It would then
call the
interface methods it knows about, without needing to know whether they
have been
overridden in a sub-class or not.

Ie, this is one of the features of perl(5) which makes polymorphism
possible.


Cheers.
David le Blanc

--              
Technical Specialist     <http://www.identity-solutions.com.au/>        
I d e n t i t y   S o l u t i o n s     
        
Level 1  365 Camberwell Road Melbourne Vic 3124 
Ph 03 9813 1388 Fax 03 9813 1688 Mobile 0417 595 550
Email  [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]>      

The information transmitted  is intended only for the recipient(s) to
which
it is addressed and  may  contain confidential  and/or privileged
material.
Any review, retransmission, dissemination or other use of, or taking of
any
action in  reliance upon, this  information by  persons or  entities
other
than  the  intended recipient is  prohibited. If  you have received this
in
error, please contact the sender and then delete it. Identity Solutions
has
taken precautions to minimise the risk of transmitting software viruses,
but
we advise you to carry out your own virus checks on any attachment to
this
message. Identity Solutions cannot accept liability for any loss or
damage
caused by software viruses.




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