develooper Front page | perl.beginners | Postings from May 2007

OOP

From:
Octavian Rasnita
Date:
May 20, 2007 05:18
Subject:
OOP
Message ID:
000301c79ad8$e97ee750$c2a2b451@teddy
Hi,

I am trying to subclass the module Path::Class::Dir but without success.

If I create a simple program like the following, the subdir() method works 
fine, but it doesn't work if I create a subclass of the module and try to 
use that method.

use Path::Class::Dir;

my $dir = Path::Class::Dir->new('c:/windows/temp');
my $subdir = $dir->subdir("perl");
print "$dir\n$subdir";

This prints:
C:\windows\temp
C:\windows\temp\perl

But the following program prints the same dir for both the dir and the 
subdir:

use MyDir;
my $dir = MyDir->new('c:/windows/temp');
$dir->process;

#and the module:

package MyDir;

use base 'Path::Class::Dir';

sub new {
my $class = shift;
$class = ref($class) || $class;
my $path = shift;
my $self = $class->SUPER::new($path);
return bless $self, $class;
}

sub process {
my $self = shift;
my $subdir = $self->subdir("perl");
print "$self\n$subdir";
}

1;

This prints:
C:\windows\temp
C:\windows\temp

And I have seen that the new() method is called twice.

Please tell me what am I doing wrong.
Is Path::Class::Dir specially made for not beeing able to subclass it?

Thanks.

Octavian




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About