Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^5: OO automatic accessor generation

by eric256 (Parson)
on Nov 11, 2009 at 21:12 UTC ( #806594=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^5: OO automatic accessor generation
Download Code
Re^6: OO automatic accessor generation
by eric256 (Parson) on Nov 11, 2009 at 21:17 UTC

    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 [qw/ lengths decimals signed allownull default usequote/] => (is => 'rw', isa => 'ArrayRef[Int]'); 1;


    ___________
    Eric Hodges
Re^6: OO automatic accessor generation
by WizardOfUz (Friar) on Nov 12, 2009 at 10:38 UTC

    I wouldn't call the code you posted a simplification. It is shorter, yes. But it is definitely not simple.

    Even the most experienced Perl hacker would have a hard time understanding it without ever having looked at the Moose documentation before. The code posted by the OP (and my little exercise) on the other hand, should be understandable even to Perl beginners who just skimmed over the Camel book.

    Just to prove my point: Can you tell me, without looking at the Moose documentation, what the ArrayRef accessors return in list context? A single array reference? Or the content of the associated array-typed attribute? And if they return an array reference, is that reference a reference to the actual attribute slot or just a reference to a copy of it? And what about scalar context?

    Please don't get me wrong. I have nothing against Moose. I just don't think it should be the starting point for Perl beginners.

      Can you tell me the answers to those questions without checking the implementation? If i have to choose between checking the implementation and checking documentation i'm going to pick documentation. That I think IS a lesson worth teaching as a starting point for begginners. Yes you can look at the implementation, but when the application becomes large enough finding the implementation becomes more of a challenge than finding the documentation. I'm all for reinventing the wheel as a learning experience, but part of that experience for me also involves finding all the existing wheels and see what lessons there are to learn from them. In this case Moose is a good wheel too look into, hence sample code.


      ___________
      Eric Hodges
        Can you tell me the answers to those questions without checking the implementation?

        Yes, of course. I've just written that code. I'm somewhat forgetful, but it's not that bad ...

        Apart from that, maybe my hint was too subtle, but I think your sample code behaves quite differently from the OP's code. Shouldn't you have set the auto_deref option to true for your ArrayRef and HashRef accessors? Then again, maybe I'm not remembering this correctly. You know ... that memory thing ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2014-09-19 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls