Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.