tinita has asked for the wisdom of the Perl Monks concerning the following question:
I'm a big fan of objects that are implemented as arrays.
They save a bit cpu, they save a bit memory, and while it may not be much, the fact that it's saving cpu *and* memory attracts me =)
also, with arrays instead of hashes, I can't be tempted to access the attributes by accessing the hash directly.
I can't find a module on CPAN that does that for me. Class::Accessor and similar modules use hashes.
Is there a module that does that? Otherwise I would like to write a little module myself (I know, there are already a lot of these kind of modules...). Here's a starter:
package Class::Accessor::Eval; use Carp qw(croak carp); use strict; use warnings; sub import { my ($self, %args) = @_; my $get = $args{get}; my $set = $args{set}; my $names = $args{names}; my $class = caller(); for my $i (0..$#$names) { my $name = $names->[$i]; my $get = $get; my $set = $set; for ($get, $set) { s/\$name/$name/g; s/\$i/$i/g; } my $code = <<"EOM"; package $class; $get $set EOM eval $code; if ($@) { croak "Error compiling $name: $@\n"; } } } 1; ############# package Foo; use Class::Accessor::Eval get => <<'EOM', sub get_$name { $_[0]->[$i] } EOM set => <<'EOM', sub set_$name { $_[0]->[$i] = $_[1] } EOM names => [qw(name age city job salary ...)]; ##################### package main; my $foo = bless ["foo", 100], "Foo"; use Data::Dumper; warn Data::Dumper->Dump([\$foo], ['foo']); print $foo->get_age,$/; $foo->set_age(23); print $foo->get_age,$/;
any comments? If noone finds that a useful module it's probably worthless to upload it. I'm also seeking for a better name.
update: maybe Class::Accessor::Fast::Array would be a possibility. that would keep it simple and follow the standard of Class::Accessor
update2: marty (Class::Accessor) actually will release an additional module that would work like my C::A::F::Array idea. he has the code ready, it just needs to be tested and released.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Objects as arrays but with autogenerated getters + setters
by Solo (Deacon) on Jun 26, 2006 at 21:02 UTC | |
by tinita (Parson) on Jun 26, 2006 at 21:38 UTC | |
by Solo (Deacon) on Jun 26, 2006 at 22:34 UTC | |
by tinita (Parson) on Jun 26, 2006 at 23:01 UTC | |
Re: Objects as arrays but with autogenerated getters + setters
by HuckinFappy (Pilgrim) on Jun 26, 2006 at 21:35 UTC | |
by tinita (Parson) on Jun 26, 2006 at 21:41 UTC | |
Re: Objects as arrays but with autogenerated getters + setters
by ForgotPasswordAgain (Priest) on Jun 27, 2006 at 11:26 UTC | |
by tinita (Parson) on Jun 27, 2006 at 12:45 UTC |