Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: Why breaking can() is acceptable

by tilly (Archbishop)
on Apr 06, 2004 at 04:56 UTC ( #342835=note: print w/ replies, xml ) Need Help??


in reply to Re: Why breaking can() is acceptable
in thread Why breaking can() is acceptable

Personally, it's the module author's responsability to provide and support the implied interface that we are all accustomed to. If you do funky stuff with AUTOLOAD, then you are obligated to do the exact same funky stuff with can(). It's that simple.

Is it also the module author's responsibility to not break multiple inheritance?

The way that you described doing things underscores my point. If your funky class is in a later chain of inheritance with multiple inheritance, then you just broke can! Exactly what you said that it was your responsibility not to do!

Depending on how yhou implemented it, you may have broken can with single inheritance as well. (Particularly if the subclass tries to use AUTOLOAD. But one could argue that it is that subclass' responsibility to understand the class that it is overriding.)

Which is one of the reasons why I kept on saying UNIVERSAL::can. Overriding can in any other location leads to potential breakage. Personally I'm OK with that as long as you're clear on what breaks, and why. (I'm not a big fan of multiple inheritance, so I don't grieve when it dies. Others might.)

The dominant reason why I said it that way was to keep people from having to figure out whether I was talking about can as a noun or a verb in the same sentences. However the other issue was in the can of worms that I referred to if you try to override can locally. (Hrm, make that a verb or which noun? :-)


Comment on Re: Re: Why breaking can() is acceptable
Re: Re: Re: Why breaking can() is acceptable
by stvn (Monsignor) on Apr 06, 2004 at 05:16 UTC
    tilly

    First, I will say that if you are using multiple-inheritance in the presence of AUTOLOAD then you are already in trouble. Sure, you should be able to do it, but IMO thats being idealistic. Part of the difficulty of multiple inheritance is managing the dispatching of methods and name clashes, if your methods aren't even implemented (and handled with AUTOLOAD) your just asking for it.

    As for how to implement local can without breaking it elsewhere, I don't see what the problem is. This (untested) code below (written waaaay past my bedtime) should serve as a stating point:

    sub can { # ------------------------------------ # this is the code tilly is talking about below # sorry , it was late # ------------------------------------ # my ($calling_package) = caller(); # if ($calling_package eq __PACKAGE__) # ------------------------------------ # however, this is what I meant ... my ($self, $method_name) = @_; # if this is called with by an object specifically # blessed into this __PACKAGE__, then we # will handle this because of AUTOLOAD if (ref($self) eq __PACKAGE__) { return my_special_can_that_plays_nice_with_AUTOLOAD(@_); } else { return $self->SUPER::can($method_name); } }
    Now I am making the assumption that SUPER::can will work, but its alot easier to control where you inherit from then it is who inherits from you. If SUPER::can doesn't work, then do something that does work.

    I will say again, can should work, period, end of story. If it doesn't work (because you used AUTOLOAD or even some symbol table madness) then you need to re-think your design.

    -stvn
      With your code snippet, if someone has an object of your class (or a class that inherits from your class) and wants to know if you can foo, where it can foo because of your AUTOLOAD, then your can will say that it can't foo because the person calling can is not making that call from within your class.

      Again, this is not as easy as it looks.

      Furthermore allow me to point out that AUTOLOAD predates can by many years, and is far more widely used. Is code that was written before they decided to start populating UNIVERSAL:: in Perl 5.004 broken because Perl 5.004 came out? Does code written by someone familiar with Camel ed. 2 need to be rethunk because p5p decided that people might like to have a feature that most people don't use?

      My belief is that p5p adding to Perl should not invalidate accepted practices. My further belief is that of AUTOLOAD and can, AUTOLOAD matters more to most Perl developers. Going further, my impression is that people who are familiar with other dynamic languages are more likely to want to reach for something like AUTOLOAD (perhaps they'll call it method_missing, but they expect it to be there) than they will can.

      My conclusion, therefore, is that someone who wants developers to not break can with AUTOLOAD is fighting an uphill battle. Furthermore, knowing everything that I do, my sympathies are with the person who wants to use AUTOLOAD to full effect. (Though admittedly I'm more likely to creating a bunch of functions by typeglobbing some closures rather than use AUTOLOAD. Unless I have a reason to use AUTOLOAD.)

        With your code snippet, if someone has an object of your class (or a class that inherits from your class) and wants to know if you can foo, where it can foo because of your AUTOLOAD, then your can will say that it can't foo because the person calling can is not making that call from within your class

        LOL, you are right, I updated the code to reflect my dumb mistake. Once again, waaaay past my bedtime.

        As for how it might affect subclasses (and please note I specifically did not handle subclasses in the code). It is your responsibilty when subclassing to take care of stuff like this. Sure that might be a little to "white-box" for some, but again, if you are using AUTOLOAD in the presence of inheritance, you are already in trouble, so you should expect to get your hands dirty.

        My belief is that p5p adding to Perl should not invalidate accepted practices. My further belief is that of AUTOLOAD and can, AUTOLOAD matters more to most Perl developers.
        This may be so, but again I dont like it much, so I am biased. No matter how old AUTOLOAD is though, I am wary of using it too much with inheritance, to me it just sounds like a bad idea. But again, these are my opinions, nothing more.
        Going further, my impression is that people who are familiar with other dynamic languages are more likely to want to reach for something like AUTOLOAD (perhaps they'll call it method_missing, but they expect it to be there) than they will can.
        I dont know if i agree with this, I can see where AUTOLOAD can be a nice tool, but I wonder if you are not restricting your view of can too much. In alot of ways they are similar, can being just a more manual approach. I think people who might be comfortable with using class reflection would be more likely to go for can, where as those more familiar with run-time code generation would reach for AUTOLOAD. Both are common aspects of dynamic languages though.
        My conclusion, therefore, is that someone who wants developers to not break can with AUTOLOAD is fighting an uphill battle.
        Alot of this depends upon in what context this is all being used/implemented, we are debating a wide open field here. But I do not think it is unreasonable to expect that can is not broken in an OO module. IMO, one should not play lightly with AUTOLOAD and objects. More imperative-style modules is another story, which I would guess (although I dont know) is the more common usage of AUTOLOAD historically.

        -stvn
Re: Why breaking can() is acceptable
by Abigail-II (Bishop) on Apr 06, 2004 at 09:56 UTC
    Is it also the module author's responsibility to not break multiple inheritance?
    Is there any module on CPAN that doesn't potentially break with multiple inheritance? Any object that uses a reference to a hash, and stores its attributes in that hash potentially breaks multiple inheritance. In more than one way.

    Now, if everyone just used inside-out objects.... (but they don't magically solve the AUTOLOAD/can mess).

    Abigail

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-07-29 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (212 votes), past polls