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

Re^7: OO automatic accessor generation

by eric256 (Parson)
on Nov 12, 2009 at 15:12 UTC ( [id://806742]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^8: OO automatic accessor generation
by WizardOfUz (Friar) on Nov 12, 2009 at 17:08 UTC
    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 ;-)

      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?

      Yes, you are correct, but even that would not work exactly the same. The OP's code (as well as your refactoring) would return an array count in scalar context (or that odd fraction looking string that you get when you do scalar %foo that tells you about the buckets and keys of the HV), where auto_deref in Moose would return an array reference or hash reference. Honestly, this to me is just another reason to use Moose since this behavior is sane, documented and tested, all a user would need to do would be to check those docs. Using the hand-rolled code, a simple accident of context (a common gotcha for Perl beginners) could lead to a very confusing debugging session and non-obvious perldoc hunt. I am sure even you would agree that seeing something like "HASH(0xDECAF)" is easier to figure out then "3/12".

      One of the things about Moose that I suspect you are not seeing is that it brings consistency to Perl OO and that is a good thing. If 100 programmers write a Perl classes using Moose and use auto_deref, then any of those 100 programmers could look at any of those 100 classes and know exactly what auto_deref means and how it will behave. Additionally all these 100 classes would be (within reason) compatible with one another since they would all be using the same underlying object system making it easier for people to reuse them.

      If just 10 programmers write a Perl class using their hand-rolled object system the odds that any of those 10 systems would be compatible with one another are not very good. The odds are very good that at least one of those hand-rolled object systems would have issues with inheritance and some of them will make mistakes like the OP and do something like regenerate accessors in the constructor. Writing an object system in Perl is easy and fun, but writing a robust and flexible object system in Perl is hard and is only fun if your a masochist.

      -stvn

      I wasn't even attempting to duplicate the behavior and I always work with refs for arrays and hashses, so that felt like the natural use here. The point I was making wasn't about your memory. Say you make 5 scripts and hand roll the OO implementation each time. Now instead of one central documented module you have 5 that might have small difference depending on the needs of each. There is no centralized code or process for these OO modules you have created (yes you could create one, and tests and documentation, and that might be the right solution for some cases). So now for each script i have to go read your implementation of the getter/setter and watch for small changes that will bite me. If instead I use Moose then I can read documentation in one location and understand all of the scripts.

      Either way my point wasn't USE MOOSE, or anything along those lines. Think of it more as, hey, there might be a solution already built and this one looks pretty spiffy, check it out and see if it fits your needs or not. I've used Moose, and hand rolled OO for different cases and different needs, so it might not be the right tool in this case, but it might be, and for that reason there is no reason not to mention it here in response to the OP.


      ___________
      Eric Hodges

        I'm sorry. The first paragraph of my previous post was an (obviously failed) attempt to be humorous. I absolutely agree with everything you have written in your last post. I just believe that we should encourage beginners to learn the basics of Perl OOP instead of blindly referring them to Moose. I've seen this kind of dialogue one too many times:

        ImaNewbie: Hi! I've just started using Perl and it's great! I want to get into this OO thing now. I've written some code, but it doesn't work and I really need your help ...

        ... 200 lines of code which took ImaNewbie two hours to write ...

        FatOldMonk: Moose.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found