in reply to Re^2: How could we "implement" Open Classes in Perl ?
in thread How could we "implement" Open Classes in Perl ?

Sorry, but you'd have to expand that a little before I'll understand your reasonimg?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re^3: How could we "implement" Open Classes in Perl ?

Replies are listed 'Best First'.
Re^4: How could we "implement" Open Classes in Perl ?
by Alien (Monk) on Jun 27, 2008 at 08:02 UTC
    I like to design as I code. I'm not really the adept of "thinking first,and coding second" :)

      You are still coding your methods in your source code, so they are built at compile time, not run time, so you are not utilising any benefit of "open classes".

      In order to make effective use of open classes, you need to dynamically determine not just the code that sits behind any given method name, but what that piece of code actually does. That is:

      If you have something like:

      package class; sub metthod { ... } sub constructor or method { ... if( some condition) { *{class::method} = sub { ... }; } }

      Then the function of both possible implementations is being determined at compile-time. The only runtime decision is which implementation is associated with the method named 'method'. And that means that you could just as easily, and far less obscurly, place both implementations inside alternate paths of an if statement conditional upon 'some condition' and the result would be exactly the same. You have made no useful use of the 'open class' concept.

      In order to make use of open classes, the actual code that sits behind the method name has to be generated at runtime, based upon some piece of information not available at compile-time, not just selected. And I have yet to see a single instance of this in a real program.

      As an example of what I mean, you might code the read method of a 'secure' transmission protocol so:

      sub read { my( $self, $socket ) = @_; my $key = <$socket>; my $transform = $self->getTransform( $key ); *{__PACKAGE_::read} = eval <<"EOC"; sub { (my \$in = <\$_[ 1 ]>) =~ tr[\x20-\xfe][$transform]; \$in; } EOC return $self->read( $socket ); }

      The first time this method is called, it reads a key from the socket argument and derives a decryption transform from that key.

      It then replaces itself with a method that uses that derived transform to decode all subsequent input from the socket. As the tables for tr are built at compile time, but the key is not available until runtime, there is no simpler way of doing this.

      This would be an effective use of an open class, but I've yet to see a live example that couldn't be replaced with a compile-time equivalent.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        In Javascript, I have used the fact that the objects are open in order to simplify logic in a 2-D grid. Instead of having the neighbors (left, right, up, and down) as attributes, I close over them in the accessors. Something like:
        for ( var i in x ) { for ( var j in x[i] ) { if ( i > 0 ) buildLeft( x[i][j], x[i][j-1] ) else buildLeft( x[i][j], null ); } } function buildLeft ( obj, target ) { obj['getLeft'] = function () { re +turn target; }; }
        And so forth. This means that I can have simplified code later on down the line.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?