in reply to Parsing barewords as sub/method calls?

This looks interesting but I do not fully understand.

You do not want to pre-declare also not to pollute the callers naming space. Does this mean that for the following code

BLA 1,2,3; within_myDSL { BLA 1,2,3 }

the first BLA should raise an error, whereas the second one would call AUTOLOAD?

That looks difficult to achieve.

I thought to move the pre-declaration into the package like this:

use subs qw( BLA ); package iDSL; # package code 1;

but that would pollute the callers space as well.

More generally, how would one define such an internal DSL, so that code like within_myDSL { >>different rules here<< } is possible?

Replies are listed 'Best First'.
Re^2: Parsing barewords as sub/method calls?
by LanX (Saint) on Nov 23, 2013 at 18:25 UTC
    > That looks difficult to achieve.

    yeah not trivial, you need to temporarily redefine AUTOLOAD in the callers package:

    use strict; use warnings; use feature 'say'; package DSL; our $AUTOLOAD; sub within (&) { my $c_block=shift; #say my $call_pkg=((caller)[0]); # use B::Deparse; # say B::Deparse->new()->coderef2text($c_block); no warnings qw/redefine once/; no strict qw/refs/; local *{ ${call_pkg} . "::AUTOLOAD" } = sub { say "Autoload called as $AUTOLOAD(@_)" }; $c_block->(); } package TEST; DSL::within { &BLA; BLUB(2,3,4); }; # with import sub within (&); *within=\&DSL::within; within { &BLAi; BLUBi(2,3,4); }; BLX(); __END__ Autoload called as TEST::BLA() Autoload called as TEST::BLUB(2 3 4) Autoload called as TEST::BLAi() Autoload called as TEST::BLUBi(2 3 4) Undefined subroutine &TEST::BLX called at /home/lanx/perl/exp/DSL.pl l +ine 48.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    updates

    code extended