[VPM] stupid stupid stupid

Peter Scott Peter at PSDT.com
Sun Apr 27 01:39:41 CDT 2003


At 11:57 AM 4/25/2003 -0700, Nathanael Kuipers wrote:
>*sigh*
>
>I'm writing a parser module for a research tool that we use.  I've sort of
>been "raised" in OO Perl, so by default I write OO modules.  Here is my
>synopsis for what I want to be able to do:
>
>my $struct_ref = BIO::vmatchIO->new(*FH)->parse();
>
>And here's the constructor:
>
>sub new () {
>         my ($class, $fh) = @_;
>         bless (\FileHandle->new($fh), (ref($class) || $class));
>}
>
>Later, the parse method calls a support method, _check_format(), to decide
>what output format it's looking at, and the following line throws an error
>("can't locate object method getline via package BIO::vmatchIO..."):
>
>while (my $line = $this->getline()) {}
>
>Messing with @ISA (via use base pragma) just gets me a "Not a GLOB reference
>at ...Handle.pm line 407" when I test the module with a script where I pass a
>reference to an opened filehandle to the constructor above.
>
>Is it clear what I am trying to do and how Perl isn't DWIM?  I've never tried
>re-blessing an object into my own package before, what's the best way to
>accomplish this?

I thought I saw a reply on this one but if so, accidentally deleted it 
before reading it. Hope this isn't repetitive or inadequate.

It's not entirely clear to me what YM, but the above has certainly 
reblessed the FileHandle into your class, and therefore it won't find the 
getline() method. No time to test the @ISA (I'm in the UK... supposedly on 
vacation), but I would have thought it would have worked.  Regardless, I 
find object composition to be better than object inheritance for handling 
this sort of thing.  I.e.,

sub new {
   my ($class, $fh) = @_;
   bless { fh => $fh }, ref($class) || $class;
}

sub getline {
   my $self = shift;
   $self->{fh}->getline;
}

with various optimizations if you want to call more FH methods besides 
getline().  But the inheritance should have worked... you just didn't show 
us what you tried.  Hmm... try taking the \ out of the constructor first 
perhaps.

>And then in a fit of curiosity I took out all the OO stuff and instead put 
>the
>parse() method in @EXPORT.  In the test script that uses BIO::vmatchIO,
>calling parse() threw an error because there was no parse method in the 
>main::
>package (obviously).

Nah, that's not a good direction to go in, as you realized.


--
Peter Scott
peter at psdt.com
http://www.perldebugged.com




More information about the Victoria-pm mailing list