in reply to Re^5: Which internal DSL are there in Perl? (Domain Specific Languages - Part 1)
in thread Which internal DSL are there in Perl? (Domain Specific Languages - Part 1)

I'm not sure why you are trying to load subs from the outer package as methods but you could basically import to the target DSL package without exporting to the calling package.

You basically can export the whole DSL logic and anything else to "Page" and reuse the package multiple times.

package Pack; use strict; use warnings; sub bla { warn ((caller(0))[3]); } use Import2Package 'DTD' => 'Page'; package Page { Page->bla(); }

exec "perl import2package.pl" unless caller(); use strict; use warnings; package Import2Package; sub import { my ($self, $dtd, $pack) =@_; my $super = (caller())[0]; no strict 'refs'; push @{"${pack}::ISA"}, $super; } 1;

Pack::bla at import2package.pl line 7.

update

of course TIMTOWTDI.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^7: Which internal DSL are there in Perl? (Domain Specific Languages - Part 1)
by LanX (Saint) on Aug 06, 2017 at 13:32 UTC
    > of course TIMTOWTDI.

    Autoload rules! =)

    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;

    <table><tr><td>1</td></tr><tr><td>Calling OuterPkg::foo()</td></tr></t +able>

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!