in reply to Re^9: Beyond Inside-Out
in thread Beyond Inside-Out

But that's my point - by asking for foo() or bar() you're not asking for the same thing - the fact that they both use the key name to store their values internally is irrelevant.

foo is trying to violate bar's encapsulation by accessing the value directly. Instead it should be using the provided API.

clint

Replies are listed 'Best First'.
Re^11: Beyond Inside-Out
by xdg (Monsignor) on May 30, 2007 at 18:51 UTC

    The if $obj is a MyClass object (with no superclasses), the call to $obj->foo is equivalent to MyClass::foo($obj).

    Note that this has nothing to do with the package Plugin -- so why does ego->{name} in MyClass::foo go to a Plugin storage slot? Because the foo function was first compiled in Plugin.

    Whether you think it's good design or not, importing functions from some other .pm file and using them as methods is a common practice.

    Of course, 'regular' inside-out classes wouldn't allow a plugin to attempt this -- the imported function wouldn't be able to access the lexical hash storing the property and it wouldn't compile. A plugin would be forced to use the accessor API.

    What Alter does isn't 'wrong' per se, it's just sufficiently different to what people are used to that it's another potential source of unexpected bugs.

    Put differently, it's yet another object paradigm for Perl.

    • Store data in the reference
    • Store data in lexical storage keyed to the reference (or an index)
    • Store data in references keyed to the package name that an accessor is first compiled in, regardless of the package name used to invoke an accessor.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Whether you think it's good design or not, importing functions from some other .pm file and using them as methods is a common practice.

      Agreed. I would say that by using a module from a plugin, you're joining what "should" be two black boxes, and those black boxes should interact via their API, and if you ignore encapsulation, then here be dragons.

      What happens if the internals of the plugin change, or the internals of the module? This would be the same issue regardless of the object model.

      That said, I'm sure that there are many modules that do exactly this, the authors probably wanting to save the expense of a method call, but I think it is Alter that is in the right here, and the plugin in the wrong.

      clint