Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Parsing barewords as sub/method calls?

by hdb (Monsignor)
on Nov 23, 2013 at 18:01 UTC ( [id://1064060]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1064060]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found