Class::Struct, accessor overrides not called from constructor
From: Matthias Weckman (matthias_weckman_at_hotmail.com)
Date: 04/28/04
- Next message: Bill: "itunes m4p format decoder module name?"
- Previous message: Ilya Zakharevich: "Re: ANN: MP3::Tag alpha release"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 28 Apr 2004 14:28:33 +0200
Hello all,
I have a question regarding Class::Struct. It is a great module for
creating small, structlike objects.
Recently I ran into a problem with it, and I'm not sure if I should file
a bug report, or whether I should consider my usage of it inappropriate.
So my question to you is, do you think it's a bug?
Here's the issue: Class::Struct allows you to override the accessors it
creates, but it doesn't call them in its constructor.
In other words,
$struct->field('blah');
calls my override, but
$struct = structure->new('field' => 'blah');
doesn't. Class::Struct simply does
$r->{'field'} = $init{'field'}
(with a defined check), but it would be more useful if it did
$r->field($init{'field'})
See the sample code below for details.
#!/usr/bin/perl
use strict;
use warnings;
use Class::Struct;
use Data::Dumper;
### define struct
struct memory => {
swap => '$',
free => '$',
};
### override accessor for free
sub memory::free {
my $r = shift;
die 'Too many args to free' if @_ > 1;
if(@_) {
my $id = shift;
if( $id > 100 or $id < 0 ) {
warn "free `$id` should be between 0 and 100";
} else {
$r->{'memory::free'} = $id;
}
}
$r->{'memory::free'};
}
### create object and dump
my $m = memory->new(free=>110, swap => 100);
print Dumper($m); # free contains 110!
$m->free(120); # warns and doesn't update
print Dumper($m);
__END__;
Btw, this occurs both with perl 5.6.1 and 5.8.2.
If this you think this is a bug, what would your advice be? Should I
file one with perlbug?
Thanks,
Rhesa Rozendaal
- Next message: Bill: "itunes m4p format decoder module name?"
- Previous message: Ilya Zakharevich: "Re: ANN: MP3::Tag alpha release"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|