On Saturday I spoke at the first Belgian Perl Workshop. I extracted the section on OO Perl from the teach-in. I've put the slides online.
I had a great time. It was good to meet up with a few European Perl Mongers. Thanks to Gilles and his crew for organising it. I hope to be invited back next year (did I mention that I love Brussels?)
I'm sorry that I had to leave at about lunchtime. I had brought the whole family to Brussels with me and we had an appointment with Mini-Europe :-)
First, let me say that's a great presentation -- I really like the focus on design issues, rather than the usual construction issues. I also like how you avoided the "religious" wars on getter/setter styles and just advised "pick a method and stick with it".
However, while I know you were intentionally using simplified examples, the inside-out examples have a subtle, but potentially dangerous flaw:
$name{$self} = shift;
This will silently "break" if $self is overloaded to stringify as something other than the memory address of the object. As that could happen in a subclass, it's always a bad idea to use raw $self as the index into the attribute. ("0 + $self" has the same problem if numification is overloaded.)
I always teach people to use "refaddr $self" -- it's the only safe way to do memory-based indices.
You also didn't note thread-safety issues, but that might be too obscure a point to be making in an introductory lesson.
-- dagolden
Re:Dangerous inside-out examples
davorg on 2007-10-29T12:50:08
You're absolutely right about the problems (of course). But, in my defence, I didn't have the time to go into that level of detail. All I was trying to do was to give people a high-level overview of the concepts.
I think that in future, I'll end that section with something along the lines of "that's basically how it works, but don't try to build classes like these yourself - instead use one of these modules as a base as they solve a number of problems that I haven't had the time to explain here."
Re:Dangerous inside-out examples
Aristotle on 2007-10-29T15:31:05
You don’t have to explain the issues in any detail. Something like the following should suffice: “That
refaddr
there gives us the memory address of the reference, much like if you stringify it – but it guards against some issues I don’t have the time to expound on. Note that this isn’t thread-safe due to the use of addresses, but can be made so – again, I don’t have time to explain how. In practice, just use the Foo module and that’ll take care of these things for you.”That’s under half a minute.