> -----Message d'origine----- > De : news [mailto:[EMAIL PROTECTED] De la part de Christopher J. Bottaro > Envoyé : jeudi 5 août 2004 19:39 > À : [EMAIL PROTECTED] > Objet : cryptic error messages in modules > > 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. > >
I made a copy-paste of your above code, added the following to call f(): package main; My::Class::f(); And got the error message below: Global symbol "$s" requires explicit package name at try.pl line 31. Execution of try.pl aborted due to compilation errors. Which tells exactly what is going wrong: missing to declare $s in DESTROY(). I'm wondering why you have got an other message. Double check ... José. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>