rootcho has asked for the wisdom of the Perl Monks concerning the following question:

hi, want to have AUTOLOAD method calling (I'm using Moose), but I really would hate to use AUTOLOAD :[.

"handles" would not work too, because I know the eventual method names only at runtime and they may change.

It is for RecordSet like class, where I wan't to have $rs->field_name(), instead of something like $rs->field('field_name')

any ideas

Replies are listed 'Best First'.
Re: Moose and AUTOLOAD
by stvn (Monsignor) on Nov 03, 2010 at 15:41 UTC
    "handles" would not work too, because I know the eventual method names only at runtime and they may change.

    Moose doesn't have anything to help you out with this, and honestly, changing method list at runtime is a serious design smell. You don't want the method list of RecordSet to be continually changing every time you make a new instance of a different table.

    Also, why aren't you using one of the perfectly good ORMs out there? DBIx::Class? Fey::ORM? Rose::DB::Object?

    -stvn
      I like ORM, but they are not well suited for every task. I rather steal some of the ideas and make my own. ;)

      Which is lighter and more pure-SQL-query centric, with all the benefits and drawbacks of having more control.

      thanx
Re: Moose and AUTOLOAD
by kcott (Archbishop) on Nov 03, 2010 at 15:48 UTC

    Would $rs->$field_name() do what you want? It's not especially Moose-ish but it's fairly standard Perl reflection.

    -- Ken

      nice :) thanx for the idea

      ;( wont help cause I hold the current row of data in $rs->{row} ... so it has to be $rs->{row}{field_name}
        my $method = $$rs{row}{field_name}; my $rv = $object->$method();

        or

        my ($rv) = map { $object->$_() } $$rs{row}{field_name};

        or

        use List::Util qw( first ); # core since Perl 5.7.3 my $rv = first { $object->$_() } $$rs{row}{field_name};

        or

        $object->$_() for $$rs{row}{field_name};, if you don't need to save its return value.

Re: Moose and AUTOLOAD
by zwon (Abbot) on Nov 03, 2010 at 16:18 UTC

    You can create methods and whole classes at runtime using Class::MOP

      Moose is a subclass of Class::MOP, so this means you can do this with Moose too