Subject: | Pod example for anonymous class does not work |
I tried the example for creating an anonymous class, just added a dummy
base class and the use statement for Class::Define:
#!/usr/bin/perl
{package Book; our $VERSION = 0.01; }
use Class::Define;
my $anonymous_class = Class::Define->define({
base => 'Book',
methods => {
price => sub { };
}
});
my $obj = $anonymous_class->new;
__END__
This leads to the error:
syntax error at /tmp/pluginconf2.pl line 8, near "}"
Execution of /tmp/pluginconf2.pl aborted due to compilation errors.
Obviously there's an ";" in the Pod where it should be a ",".
Fixing this leads to another error:
Can't locate object method "new" via package
"Class::Define::AnonymousClass::12677114114141349" at
/usr/share/perl5/Class/Define.pm line 149.
OK, so a new method probably also needs to be defined. So I did that and
tried the following, slightly compressing the code:
#!/usr/bin/perl
{package Book; our $VERSION = 0.01; }
use Class::Define;
my $obj = Class::Define->define({
methods => {
new => sub { bless {}, shift },
price => sub { },
}
})->new;
warn $obj->price;
__END__
This also failed:
Can't locate object method "price" via package
"Class::Define::AnonymousClass::12677119342597603" (perhaps you forgot
to load "Class::Define::AnonymousClass::12677119342597603"?) at
/tmp/pluginconf2.pl line 10.
Looking at the source code I found out that the created class is removed
when the class object is out of scope. Maybe there's a way to unload the
class only if the last instance is removed? Or at least document it that
it works this way.
Regards,
Slaven