"Charles K. Clarkson" wrote:

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

Looks like you are doing fine.  The aspect of the posted code that was
problematic was the:
ref($this) part.

That construct would be used to get the class to which an object belongs.  The
point here is that it is not a good idea to use the an object to call a
constructor.  The code below is for a constructor called directly from a package
name, which is a much more direct and clear approach.

The post above was a reiteration of a caution raised by Randal Schwartz.  The
ref($this) || $this construct tends to be used as a covert clone method.  It is
better to simply name a clone function clone, and to keep clone distinct from
the constructor.

The code below looks great.  The undef assignments are not needed, since new
data members can be defined at any time, but they do serve as useful comments to
indicate the intended shape of the data.  It definitely shows good design sense
to defer the fine deails for initialize to fill in.

Joseph

FWIW, I think there were further posts on that thread.  Again, I'm on my way to
the day job, so it will be awhile before I can hunt down message references.
You should be able to see them from one of the threaded archives, though.

> 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;
> }
>
> TIA,
>
> Charles K. Clarkson
> --
> Mobile Homes Specialist
> 254 968-8328


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