in reply to My first attempt at inheritance

If you are going to use IO::Dir::pl->new you need to have defined IO::Dir::pl before you used it.

I.e. you can't just take a random class name and use it as an object or perl will complain:

perl -we'use strict; my $foo=Data::Vars->new();' Can't locate object method "new" via package "Data::Vars" (perhaps you + forgot to load "Data::Vars"?) at -e line 1. > grep 'sub.*new' lib/Data/Vars.pm sub new (;$) { # return blessed objects and init pre-define +d fields w/args
you have to "use" (or have it defined first) a class (so perl can know where to attach things) BEFORE you use it.

That's the 1st problem.

Second problem is -- what are you trying to do?

Your first routine calls IO::DIR::pl->new, but I don't see any 'new' in IO::dir::pl.

You *can't rely* on inheritance yet, as you don't have a Blessed Object!

Third problem is you call "read_pl", in your class, but have no read_pl. (did you mean read?) So if you fix those, things, it works, but not well:

#!/usr/bin/perl -w use strict; use warnings; use IO::Dir; my $dh=bless {}, 'IO::Dir::pl'; #my $dh = IO::Dir::pl->new('.'); while (my $file_name = $dh->read()){ print STDOUT "$file_name\n"; exit; } { package IO::Dir::pl; use strict; use warnings; our @ISA; @ISA = qw(IO::Dir); sub read{ my $self = shift; warn "Ok We got here.\n"; return undef; } } > /tmp/p Ok We got here. (in cleanup) Not a GLOB reference at /usr/lib/perl5/5.16.2/x86 +_64-linux-thread-multi/IO/Dir.pm line 43.
If you Just switch around things so your definition occurs before you use it:
#!/usr/bin/perl -w { package IO::Dir::pl; use strict; use warnings; our @ISA; @ISA = qw(IO::Dir); sub read{ my $self = shift; warn "Ok We got here.\n"; return undef; } } use strict; use warnings; use IO::Dir; my $dh = IO::Dir::pl->new('.'); while (my $file_name = $dh->read()){ print STDOUT "$file_name\n"; exit; } > /tmp/p Ok We got here.
That does what you want...

(I think?)

Replies are listed 'Best First'.
Re^2: My first attempt at inheritance
by tobyink (Canon) on Jun 01, 2013 at 09:52 UTC

    "Your first routine calls IO::DIR::pl->new, but I don't see any 'new' in IO::dir::pl.

    "You *can't rely* on inheritance yet, as you don't have a Blessed Object!"

    Not true. Inheritance works on class methods too. Look ma, no blessed objects!

    use v5.12; use strict; use warnings; require File::Spec; @File::Spectacles::ISA = "File::Spec"; say File::Spectacles->tmpdir;

    The OP's entire problem was that he was trying to rely on IO::Dir::pl's inheritance, before he'd assigned anything to @ISA.

    Adding @IO::Dir::pl::ISA = "IO::Dir" to the beginning of the file suffices to fix it. (Though it's probably not the solution I'd use personally.)

    Update: sorry - fixed code.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Um...did you try that...doesn't work here...
      > perl -e ' use v5.12; use strict; use warnings; @File::Spectacles::ISA = "File::Spec"; say File::Spectacles->tmpdir;' Can't locate package File::Spec for @File::Spectacles::ISA at -e line +7. Can't locate package File::Spec for @File::Spectacles::ISA at -e line +7. Can't locate object method "tmpdir" via package "File::Spectacles" at +-e line 7.
      Vs. if you put the use in:
      > perl -e ' use File::Spec; use v5.12; use strict; use warnings; @File::Spectacles::ISA = "File::Spec"; say File::Spectacles->tmpdir;' /tmp
      I've always had to put in a 'use' before it would work... But your perl may be different (works without pre-declaring things.)...hmmm.... I'm using 5.16.2...BTW...maybe this is something that changed, though I seem to remember it being around for a while...?? But then I remember the easter bunny just like it was yes...er...scratch that.

      Um... So did you actually try your example? -- didn't work here. Sorry... :-(