develooper Front page | perl.perl5.porters | Postings from August 2013

Re: [perl #119455] [EXPERIMENT] pluggable keyword API

Thread Previous | Thread Next
From:
Lukas Mai
Date:
August 25, 2013 11:32
Subject:
Re: [perl #119455] [EXPERIMENT] pluggable keyword API
Message ID:
5219EB48.8060908@gmail.com
On 25.08.2013 11:31, Zefram wrote:
> Ricardo SIGNES wrote:
>> perl 5.12.0 introduced the "pluggable keyword" API.  Although perlexperiment
>> has mostly excluded experimental APIs, pluggable keywords have a higher level
>> of general interest and use already.
> 
> That API turned out to be a poor design, and we should ultimately remove
> it.  A much better design for the same job is the per-sub parser hook,
> as implemented by Devel::CallParser.  I'd like to put the call-parser
> hook into the core and then remove the keyword plugin API.  It has to
> be in that order, because Devel::CallParser is currently implemented
> via the keyword plugin mechanism.

Devel::CallParser solves a different problem. I can only use it to
implement custom argument syntax for subroutines, but what I actually
want is custom keywords.

For example: if I want to reimplement 'sub', I need to accept a bareword
(subroutine name) followed by a block (subroutine body). None of the
parse_args_* functions can be used for this.

Assuming I can do this manually by bypassing D:CP and calling perl's
lexer/parser functions directly, I still have to return an argument
list. This isn't needed because all the effects I want happen at
runtime, but the API insists on constructing a sub call.

Finally, the whole thing won't be a statement by itself, so the user
will need to add a ';'.

I'm thinking of this:

  sub mysub {}
  # ... somehow use Devel::CallParser to attach custom parsing code
  # to mysub ...

  mysub plus {
    return $_[0] + $_[1];
  };  # <- semicolon required

  # compiled as:
  # BEGIN { *plus = sub { return $_[0] + $_[1] }; }
  # # ^ define &plus during call parsing
  # &mysub();
  # # ^ dummy call constructed by D:CP

Also, this would make something like

  my $wtf = mysub foo { return 42; };

valid syntax. (Equivalent to:

  BEGIN { *foo = sub { return 42; }; }
  my $wtf = &mysub();

)

All of this is unlike the 'sub' builtin, which can be approximated very
well using a pluggable keyword.

In short: If I haven't misunderstood how D:CP works, then the pluggable
keyword API may be a poor design but D:CP simply doesn't do the same job.

(And I still want the KEYWORD_PLUGIN_NOOP patch to go in because it
would make Keyword::Simple much more powerful.)

-- 
Lukas Mai <plokinom@gmail.com>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About