As a first step towards subclassable, well, everything, I've patched
POE::Preprocessor to allow someone to import the macros and constants
defined by another module.
Syntax:
use POE::Preprocessor (isa => 'POE::SomeModule');
you can also override the constants and macros coming from SomeModule
simply by defining your own.
This includes a documentation update. a test patch will follow when i
fully grok the POE test suite :) POE passes all tests with this patch.
patch follows sig.
--------
Matt Cashner
Web Applications Developer
The Creative Group (http://www.cre8tivegroup.com)
[EMAIL PROTECTED] | Codito, ergo sum
---- PATCH STARTS ----
--- Preprocessor.pm 2001/07/25 15:28:24 1.23
+++ Preprocessor.pm 2001/09/08 23:19:14
@@ -23,10 +23,13 @@
#sub DEBUG_INVOKE () { 1 }
#sub DEBUG_DEFINE () { 1 }
+#sub WARN_DEFINE () { 1 }
+
BEGIN {
defined &DEBUG or eval 'sub DEBUG () { 0 }'; # preprocessor
defined &DEBUG_INVOKE or eval 'sub DEBUG_INVOKE () { 0 }'; # macro invocs
defined &DEBUG_DEFINE or eval 'sub DEBUG_DEFINE () { 0 }'; # macro defines
+ defined &WARN_DEFINE or eval 'sub WARN_DEFINE () { 0 }'; # macro/const
+redefinition warning
};
# text_trie_trie is virtually identical to code in Ilya Zakharevich's
@@ -126,20 +129,53 @@
my (%constants, %macros, %const_regexp, %macro);
sub import {
- # Outer closure to define a unique scope.
- { my $macro_name = '';
+
+ my $self = shift;
+ my %args;
+ if(@_ > 1) {
+ %args = @_;
+ }
+
+ # Outer closure to define a unique scope.
+ { my $macro_name = '';
my ($macro_line, $enum_index);
my ($package_name, $file_name, $line_number) = (caller)[0,1,2];
my $const_regexp_dirty = 0;
my $state = STATE_PLAIN;
+
+ my @isas;
+
+ if($args{isa}) {
+ if(ref $args{isa} eq 'ARRAY') {
+ foreach my $isa (@{$args{isa}}) {
+ push @isas, $isa;
+ }
+ } else {
+ push @isas, $args{isa};
+ }
+ foreach my $isa (@isas) {
+ eval "use $isa";
+ croak "Unable to load $isa : $@" if $@;
+
+ foreach my $const (keys %{$constants{$isa}}) {
+ $constants{$package_name}->{$const} = $constants{$isa}->{$const};
+ $const_regexp_dirty = 1;
+ }
+
+ foreach my $macro (keys %{$macros{$isa}}) {
+ $macros{$package_name}->{$macro} = $macros{$isa}->{$macro};
+ }
+ }
+ }
+
$conditional_stacks{$package_name} = [ ];
$excluding_code{$package_name} = 0;
my $set_const = sub {
my ($name, $value) = @_;
- if (exists $constants{$package_name}->{$name}) {
+ if (exists $constants{$package_name}->{$name} && WARN_DEFINE) {
warn "const $name redefined at $file_name line $line_number\n"
unless $constants{$package_name}->{$name} eq $value;
}
@@ -336,7 +372,7 @@
$macro{$package_name}->[MAC_CODE] =~ s/^\s*//;
$macro{$package_name}->[MAC_CODE] =~ s/\s*$//;
- if (exists $macros{$package_name}->{$macro_name}) {
+ if (exists $macros{$package_name}->{$macro_name} && WARN_DEFINE) {
warn( "macro $macro_name redefined at ",
"$file_name line $line_number\n"
)
@@ -556,6 +592,8 @@
use POE::Preprocessor;
+ # use POE::Preprocessor ( isa => 'POE::SomeModule' );
+
macro max (one,two) {
((one) > (two) ? (one) : (two))
}
@@ -676,6 +714,19 @@
Conditional includes are experimental pending a decision on how useful
they are.
+=head1 IMPORTING MACROS/CONSTANTS
+
+ use POE::Preprocessor ( isa => 'POE::SomeModule' );
+
+This method of calling Preprocessor causes the macros and constants of
+C<POE::SomeModule> to be imported for use in the current namespace.
+These macros and constants can be overriden simply by defining items
+in the current namespace of the same name.
+
+Note: if the macros in C<POE::SomeModule> require additional perl
+modules, any code which imports these macros will need to C<use>
+those modules as well.
+
=head1 DEBUGGING
POE::Preprocessor has three debugging constants which may be defined
@@ -693,6 +744,10 @@
To see macro, constant, and enum definitions:
sub POE::Preprocessor::DEBUG_DEFINE () { 1 }
+
+To see warnings when a macro or constant is redefined:
+
+ sub POE::Preprocessor::WARN_DEFINE () { 1 }
=head1 BUGS
---- PATCH STOPS ----