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?)
Reblessing is sooo 90's :-)
Why not use role composition to achieve your goal?
role MyWay {
method say(|) {
note "whee";
nextsame
}
}
my $*OUT = PROCESS::<$OUT> but MyWay;
$*OUT.say("foo") # whee\nfoo
This basically creates a new dynamic $*OUT
based on the existing $*OUT
but with a new method say
mixed in.
To answer your questions as well as I can:
Date
).Since say
internally uses print
, it would be better to actually mix in your own print
method:
role MyWay {
method print(|) {
note "whee";
nextsame
}
}
my $*OUT = PROCESS::<$OUT> but MyWay;
say "foo"; # whee\nfoo
This has the added benefit of no longer needing to call say
as a method, because the sub version of say
will pick up the altered $*OUT
dynamically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With