Subject: | Unable to over-ride Acme::Ook::_compile in child classes (patch attached) |
G'day Jarkko,
Firstly, thank-you for Acme::Ook, which has made me smile on a number of
occasions. Today I stumbled upon yet another chance to harness the
power of Ook, and I'm hoping you can help me. I'll try to explain...
I'm presently at OSDC (www.osdc.com.au) and I'm writing a lightning talk
on which is based upon a language which is a simple transform of Ook.
This is part of an ongoing joke that's appeared in OSDC lightning talks
for the last few years, and is certain to get a lot of smiles from the
audience.
In order to accomodate this in the most amusing way possible, I'm trying
to sub-class Acme::Ook and over-ride the _compile() method.
Unfortunately, the Acme::Ook::compile() calls _compile() as a subroutine
rather than as an object method, so even if a child over-rides it that
child method will never be called. I'm hoping this can be changed.
Also, in order to leverage the existing code as much as possible, I'd
also love for the %Ook hash to be accesible from other classes, either
by turning it into a package variable, or a new method that returns it.
I've attached a patch that implements both of these changes. My hope is
to get them onto the CPAN before my presentation, so that I can upload
my own module to CPAN that leverages it.
The conference lunchbreak is from 8th Dec 2am - 3am GMT (1pm - 2pm
Melbourne time) and this is when I hope to write my talk. If these
changes can make it to CPAN by then I can assure you of a huge thank-you
in my talk credits, and many pints of beverage should we ever meet. ;)
Please feel free to e-mail me at pjf@perltraining.com.au or pjf@cpan.org
should you have any questions or comments, or if the changes make it to
CPAN.
Many many many thanks!
Paul
Subject: | ook-patch.txt |
--- Acme-Ook-0.10/lib/Acme/Ook.pm 2002-12-31 11:36:59.000000000 +1100
+++ Acme-Ook-0.10_1/lib/Acme/Ook.pm 2006-12-07 17:06:52.609375000 +1100
@@ -1,10 +1,10 @@
package Acme::Ook;
use strict;
-use vars qw($VERSION);
+use vars qw($VERSION %Ook);
$VERSION = '0.10';
-my %Ook = (
+%Ook = (
'.' => {'?' => '$Ook++;',
'.' => '$Ook[$Ook]++;',
'!' => '$Ook[$Ook]=read(STDIN,$Ook[$Ook],1)?ord$Ook[$Ook]:0;'},
@@ -32,6 +32,7 @@
}
sub _compile {
+ shift;
chomp $_[0];
$_[0] =~ s/\s*(Ook(.)\s*Ook(.)\s*|(\#.*)|\S.*)/$;=$Ook{$2||@@}{$3};$;?$;:defined$4?"$4\n":die"OOK? $_[1]:$_[2] '$1'\n"/eg;
return $_[0];
@@ -40,20 +41,20 @@
sub compile {
my $self = shift;
my $prog;
- $prog .= _compile($$self, "(new)", 0) if defined $$self && length $$self;
+ $prog .= $self->_compile($$self, "(new)", 0) if defined $$self && length $$self;
if (@_) {
local *OOK;
while (@_) {
my $code = shift;
if (ref $code eq 'IO::Handle') {
while (<$code>) {
- $prog .= _compile($_, $code, $.);
+ $prog .= $self->_compile($_, $code, $.);
}
close(OOK);
} else {
if (open(OOK, $code)) {
while (<OOK>) {
- $prog .= _compile($_, $code, $.);
+ $prog .= $self->_compile($_, $code, $.);
}
close(OOK);
} else {
@@ -63,7 +64,7 @@
}
} else {
while (<STDIN>) {
- $prog .= _compile($_, "(stdin)", $.);
+ $prog .= $self->_compile($_, "(stdin)", $.);
}
}
return '{my($Ook,@Ook);local$^W = 0;BEGIN{eval{require bytes;bytes::import()}}' . $prog . '}';