On Feb 25, 2004, at 12:05 AM, 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?

There is a better way. What's being depreciate is the whole ref($class) || $class trick, to build a constructor that is also a clone method. Randal has covered why that's a bad idea here before.


However, you should still feed $class to bless(), for the sake of inheritance.

package MyFileBasename;

use File::Basename;

sub new {
    my $class = shift;
    my $self = {
        file_name   => undef,
        base        => undef,
        path        => undef,
        type        => undef,
    };
    bless $self;

bless $self, $class;


Hope that helps.

James


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