I.e. you can't just take a random class name and use it as an object or perl will complain:
you have to "use" (or have it defined first) a class (so perl can know where to attach things) BEFORE you use it.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
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:
If you Just switch around things so your definition occurs before you use it:#!/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.
That does what you want...#!/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.
(I think?)
In reply to Re: My first attempt at inheritance
by perl-diddler
in thread My first attempt at inheritance
by BillKSmith
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |