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

Re^2: OO automatic accessor generation

by WizardOfUz (Friar)
on Nov 11, 2009 at 16:05 UTC ( [id://806526]=note: print w/replies, xml ) Need Help??


in reply to Re: OO automatic accessor generation
in thread OO automatic accessor generation

I really don't think that referring beginners (who want to learn OO programming in Perl) to Moose is such a good idea. In doing so, they will learn nothing (well, almost nothing) because Moose quite effectively hides Perl's OO concepts/mechanisms from them.

Replies are listed 'Best First'.
Re^3: OO automatic accessor generation
by merlyn (Sage) on Nov 11, 2009 at 16:58 UTC
    I'll disagree. There's two levels of "learning" here. If you want to get stuff done, and you need to use objects and Perl, Moose is the perfect solution. If you want to learn the guts of Perl, and you know that Perl has some cool low-level technology for method dispatch, then yes, Moose is for later, not for now.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

      I (respectfully) disagree with your disagreement.

      Do you honestly think, looking at the OP's code, that he would benefit more from using Moose right now than from re-reading perlobj, perlboot, perltoot and perltooc?

      Maybe I'm an old fart, but I think that writing your own accessor factory is a rite of passage for every aspiring Perl hacker ...

        Maybe I'm an old fart, but I think that writing your own accessor factory is a rite of passage for every aspiring Perl hacker ...

        Sure it is, but Moose is so much more then an accessor factory and when it comes to getting real work done why add an object system to your maintenance burden?

        You can take eric256's example one step further and make 'tablename' required (meaning you *must* supply a value to the constructor), and set default values for all the other attributes. I also made the grouped attributes "lazy" which means that Moose will not initialize the slot until it absolutely has too.

        package DataTable; use Moose; has 'tablename' => (is => 'rw', isa => 'Str', required => 1); has 'columns' => (is => 'rw', isa => 'ArrayRef[Str]', default => sub + { [] }); has 'indices' => (is => 'rw', isa => 'HashRef[Str]', default => sub + { +{} }); has 'datatypes' => (is => 'rw', isa => 'ArrayRef[Str]', default => sub + { [] }); has [qw/ lengths decimals signed allownull default usequote /] => (is => 'rw', isa => 'ArrayRef[Int]', lazy => 1, default => sub { [ +] }); 1;
        This is not much more code for Moose, but adding it to your example, or the OPs example would start to get really hairy.

        And then to take it even one more step further using Moose::Meta::Attribute::Native you can add some delegated behavior to the 'columns' attribute like so:

        package DataTable; use Moose; has 'tablename' => (is => 'rw', isa => 'Str', required => 1); has 'columns' => ( traits => [ 'Array'], is => 'rw', isa => 'ArrayRef[Str]', default => sub { [] }, handles => { 'add_column' => 'push', 'remove_column_at_idx' => 'delete', # ... and many more } ); # rest of the class snipped off for brevity ...
        This can then be used like this ...
        my $dt = DataTable->new; $db->add_column( 'foo' ); $db->add_column( 'bar' ); # columns is now [ foo, bar ] $db->remove_column_at_idx( 0 ); # columns is now [ bar ]
        Honestly, would you want to write this into your own accessor generator? Would you want to maintain it? Moose has *thousands* of tests to make sure stuff like this Just Works.

        -stvn
        Do you honestly think, looking at the OP's code, that he would benefit more from using Moose right now than from re-reading perlobj, perlboot, perltoot and perltooc?

        Unless the OP wants to write a new implementation of an object system, I do. (I suspect most people using objects in Perl 5 do so because they have some project to finish, not because they want to understand how an object system works or the syntax for Perl 5 metaprogramming.)

        Depends on the coders goal.

        package DataTable; use Moose; has 'tablename' => (is => 'rw', isa => 'Str'); has 'columns' => (is => 'rw', isa => 'ArrayRef[Str]'); has 'indices' => (is => 'rw', isa => 'HashRef[Str]'); has 'datatypes' => (is => 'rw', isa => 'ArrayRef[Str]'); has 'lengths' => (is => 'rw', isa => 'ArrayRef[Int]'); has 'decimals' => (is => 'rw', isa => 'ArrayRef[Int]'); has 'signed' => (is => 'rw', isa => 'ArrayRef[Int]'); has 'allownull' => (is => 'rw', isa => 'ArrayRef[Int]'); has 'default' => (is => 'rw', isa => 'ArrayRef[Int]'); has 'usequote' => (is => 'rw', isa => 'ArrayRef[Int]'); 1;

        Thats a lot of simplification right there. And I wouldn't be surprised to find that there might be a short cut to creating a bunch of attributes with the same definition.


        ___________
        Eric Hodges
Re^3: OO automatic accessor generation
by leocharre (Priest) on Nov 25, 2009 at 19:41 UTC

    I can think of a simple way to put it. When someone's coding class method generators, it's time to point them in another direction.
    What this guy's talking about is not simple OO. It's great to do this- write your own accessor generator- I've done this a few times. Awesome- learned a ton- but really.. I wouldn't say that writing a class construction helper is part of knowing how to write a class! :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://806526]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found