package OuterPkg;
use strict;
use warnings;
use Carp qw/cluck/;
sub foo {
my $sub =(caller(0))[3];
$"=",";
return "Calling $sub(@_)";
}
use Import2Package 'DTD' => 'myDSL';
package myDSL {
print
TABLE {
TR {
TD {1}, TD{foo()},
}
}
}
# TABLE(); # not visible in outer Package
####
exec "perl OuterPkg.pl" unless caller();
use strict;
use warnings;
use Carp qw/cluck/;
package Import2Package;
sub delegate_subs {
my ( $pkg, $super )=@_;
use vars '$AUTOLOAD';
no strict 'refs';
*{${pkg}."::AUTOLOAD"} =
sub {
my $delegate = $AUTOLOAD;
$delegate =~ s/.*:://; # strip packagename
goto &{"${super}::${delegate}"};
}
}
sub install_vocab {
my ( $pkg, $super ) = @_;
my @vocab = qw/TABLE TR TD/;
my $level = 0;
no strict 'refs';
for my $CMD (@vocab) {
*{"${pkg}::$CMD"} = sub (&) {
$level++;
my @inside = $_[0]->();
$level--;
my $indent = "\n" . "\t" x $level;
my $tag =lc($CMD);
return join "",map {"<$tag>$_$tag>"} @inside;
}
}
*{"${pkg}::render"} = sub { print @_ };
}
sub import {
my ($self, $dtd, $pkg) =@_;
my $super = (caller())[0];
delegate_subs $pkg => $super;
install_vocab $pkg, $super;
}
1;
####