hi monks,

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,$/;
a 'new' method is missing, and a default if you don't want to write the get/set yourself.

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.


In reply to Objects as arrays but with autogenerated getters + setters by tinita

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.