develooper Front page | perl.perl5.porters | Postings from May 2008

Adding new keywords

Thread Next
Simon Wistow
May 4, 2008 18:01
Adding new keywords
Message ID:
Recently I figured I've been doing Perl long enough that I should really 
start to try and understand the core a little so this weekend I've been 
playing around with some stuff and trying to feel my way through the 
various bits.

One of the things I wanted to try just a learning project was adding a 
new keyword 'method' which was just an alias to 'sub' and this is what 
I've worked out so far - any advice is much appreciated.

1) First I need to patch - I'm not sure if there's a way 
to automatically merge the result into toke.c and t/op/cproto.t though.

2) Failing some automagic way of merging things I need to then splice 
the generated  Perl_keyword() into toke.c 

3) I can then hack the 'method' case to return KEY_sub 

Which, unless I'm mistaken, should be sufficent. And lo and behold it 
works and ./perl t/op/cproto.t passes its addition test and ./perl -w -e 
'method foo{}' is fine and dandy. So, the first of my questions - 

 * Are there any other tests I should copy?

So that was easy enough but now I want to start doing something 
different if it was declared with 'method' rather than 'sub' so, 
presumably the best way is

0) Patch and run it 

1) Manually patch the "KEY_format/KEY_sub" case in toke.c to add 

2) Note whether we came in by KEY_method or KEY_sub and then return 
TOKEN(SUB) or TOKEN(SUBMETHOD) as appropriate (ignoring MYSUB and 
ANONSUB for the moment)

3) Copy the relevant 'SUB' parts of perly.y as 'SUBMETHOD' equivalents 
with appropriate translations of subrout to submethrout or something.

4) Rerun

And lo and behold

% ./perl -w -e 'method foo { print "Hello World!\n"}; foo'
Hello World!

So, I'm pretty sure I've not done things correctly and that I've missed 
out updating some file or something. What am I missing.

To be honest I was quite surprised it was that easy - it only took me 
about 30 minutes and a cup of tea to work that out and I'm not exactly 
firing on all cylinders today. I mean, I suppose it could be made easier 
still but it's not every day that you add a new keyword so it's probably 
not worth the effort.

This was written as a stream of conciousness as I hack so if it seems a 
bit disjointed then, well, it is. 

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About