How could I subclass Perl 6's IO::Handle?

Viewed 202

How would I subclass IO::Handle? For example, I want to be able to "autoflush" by calling flush after every say:

class MyIO is IO::Handle {
    multi method say(MyIO:D: **@text --> True) {
        nextsame;
        $*ERR.say: "Flushing\n";
        self.flush;
        }
    };

Metamodel::Primitives.rebless: $*OUT, MyIO;
put $*OUT.^name;

$*OUT.say: "This is standard out";

But, it appears that MyIO is never called.

I figure that I might wrap say to produce the effect, but I'm more interested in the technique of simple subclasses to override some behavior. But, having said that, if there's a more Perly 6 way that the design intends for people to use.

So, some questions:

  • Does Perl 6 actaully care that I reblessed it? How does it look up method names that might skip that?

  • Are builtin classes especially resistent to standard OO techniques because of their sorcery and NQPness?

  • Does Perl 6 discourage low level fiddling with handles, such as re-opening $*OUT on a file descriptor? (As in Does changing Perl 6's $*OUT change standard output for child processes?)

1 Answers
Related