For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2004 > cryptic error messages in modules









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author cryptic error messages in modules
Christopher J. Bottaro

2004-08-05, 3:56 pm

package My::Class;
use strict;
use warnings;
use Class::Struct;
use IO::File;

struct MyStruct => {
f1 => '$',
f2 => '$'
};

sub new {
my ($class, $ifname) = @_;
my $s = {};
$class = ref($class) || $class;
bless ($s, $class);
$s->{FILE} = new IO::File($ifname, "r");
if ( !defined($s->{FILE}) ) {
print("ERROR: $class: cannot open $ifname for reading\n");
exit(-1);
}
return $s;
}

sub f {
return MyStruct->new();
}

sub DESTROY {
$s->{FILE}->close();
}

----------------------------------------------------------------------------------

if i make a perl script that uses My::Class and i construct an instance of
My::Class and then call My::Class::f(), i get the following error:
Can't locate object method "new" via package "MyStruct" (perhaps you forgot
to load "MyStruct"?)

now obviously the problem is not that no new() method exists for MyStruct,
but rather that i forgot to declare $s in DESTROY().

it took me literally hours to figure out what the problem was. i mean,
could the error message be anymore unrelated to the real problem? what the
hell does $s not being declared in DESTROY() have anything to do with the
error message i was given?

thanks for the help and advice.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com