in reply to Re: Modules for autogenerating accessor/mutator methods
in thread Modules for autogenerating accessor/mutator methods

Laziness?

the lowliest monk

  • Comment on Re^2: Modules for autogenerating accessor/mutator methods

Replies are listed 'Best First'.
Re^3: Modules for autogenerating accessor/mutator methods
by srdst13 (Pilgrim) on Jun 04, 2005 at 21:36 UTC
    Hey, that was my answer! However, these modules exist and are definitely being used. Class::DBI is a prime example. I really don't get much out of writing simple accessors/mutators. If they get more complicated, I would of course like to have the Module move over and facilitate my creating a custom method, which most seem to offer as an option. I think the question still stands as to which, if any, is "better" than the next (or what less-than-obvious advantages exist in one versus the next).

    Sean
Re^3: Modules for autogenerating accessor/mutator methods
by wazoox (Prior) on Jun 04, 2005 at 21:36 UTC
    Really I'm not sure. An accessor/mutator is often very simple, so where is laziness? writing the quick'n'dirty one that "just works" or use a complex module that provides complex options and try to learn how it works --and may bite?

      I hear you; I have not found an accessor-generating module that was worth the trouble. But to be fair, I haven't looked too hard since I don't do much module writing (most of my coding is scripts), so my needs in this regard are very modest. For the few modules I write (which are usually relatively simple) I get by with AUTOLOAD, which I still consider lazier than typing out a dozen accessors, even if the "typing" amounts to as little as a brief exercise in cutting and pasting.

      the lowliest monk

        You're using a sledgehammer to swat fleas.

        Typeglob a bunch of closures. Save AUTOLOAD for a problem that needs the power. Here is an untested version.

        package Whatever::Makes::Sense; sub create_accessors { my $class = caller(); foreach my $accessor (@_) { no strict 'refs'; *{"$class\::$accessor"} = sub { my $self = shift; $self->{$accessor} = shift if @_; return $self->{$accessor}; }; } } # And elsewhere... Whatever::Makes::Sense::create_accessors(qw(foo bar baz));
        Drawbacks? You have to write the line of code that lists the accessors. Advantages? You leave AUTOLOAD free to do something else, you don't get things that you didn't intend to AUTOLOAD being AUTOLOADed, your methods are visible to UNIVERSAL::can, missing methods don't get accidentally interpreted as accessors (a good AUTOLOAD can address this, but requires more code)...
        Could you please explain a bit the AUTOLOAD magic? I don't get it :)