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


In reply to Re: My first attempt at inheritance by perl-diddler
in thread My first attempt at inheritance by BillKSmith

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.