15

I'm looking to play with perl parser manipulation. It looks like the various B::Hooks modules are what people use. I was wondering:

  1. Best place to start for someone who has no XS experience (yet). Any relevant blog posts?

  2. How much work would be involved in creating a new operator, for example:

    $a~>one~>two~>three

~> would work like -> but it would not try to call on undef and would instead simply return undef to LHS.

Although a source filter would work -- I'm more interested in seeing how you can manipulate the parser at a deeper level.

serenesat
  • 4,611
  • 10
  • 37
  • 53
LLFourn
  • 173
  • 8
  • 4
    That sounds like an interesting feature. :) – brian d foy May 23 '15 at 16:32
  • I thought of it while getting annoyed doing `if( $dom->at('div')->at('h1')) { ` in Mojo::DOM and getting can't call undefined becasue `div` didn't exist :) – LLFourn May 23 '15 at 16:50
  • I have not figured this out yet but this looks promising: https://metacpan.org/pod/B::Utils – LLFourn May 24 '15 at 16:05
  • 1
    Have you considered using `autobox`? A very simple package with a fundamental `AUTOLOAD` routine would suffice. You wouldn't have a new operator, but adding all possible methods there are to `undef`. This would probably have terrible performance impacts. Like this: `use autobox UNDEF => 'SilentUndef'; … your code… package SilentUndef; sub AUTOLOAD {sub{}}`. – Patrick J. S. May 24 '15 at 17:22
  • Thanks I hadn't seen autobox before. That looks like a good solution. Wouldn't it not be too bad performance wise because you are only 'autoboxing' undef (which shouldn't be called on anyway). But right now I'm actually just trying to learn about the parser. The "tentatively call" thing above that avoids undef was just an example. BTW I now think I have to use: https://metacpan.org/pod/B::Hooks::OP::Check. – LLFourn May 24 '15 at 17:31
  • I never spent the time to figure out how to use B::Hooks to do this in perl5. I ended up doing it in perl6: [Slang::Dotty](https://github.com/LLFourn/perl6-slang-dotty). In perl6 you can actually change the the syntax at runtime arbitrarily. Which is called a "slang". – LLFourn Jul 18 '15 at 16:02
  • 2
    When I did some XS in the past (which I completely forgot at this point) I started from http://perldoc.perl.org/perlxstut.html For adding new operators, I would probably start from http://perldoc.perl.org/perlguts.html#Custom-Operators – polettix Sep 01 '15 at 04:53
  • @polettix That looks exactly what I need. I'd say using B::Hooks::OP::Check and hooking the custom operator OP would be the place to start. I won't be able to test this theory for a month or so. – LLFourn Sep 01 '15 at 08:25

1 Answers1

1

I don't believe you can add infix operators (operators whose operands are before and after the operator), much less symbolic ones (as opposed to named operators), but you could write an an op checker that replaces method calls. This means you could cause ->foo to behave differently. By writing your module as a pragma, you could limit the effect of your module to a lexical scope (e.g. { use mypragma; ...}).

ikegami
  • 367,544
  • 15
  • 269
  • 518