Michael Schwern gave a two hour talk on refactoring at the Portland Perl Mongers meeting last night. It was a great talk and he finished it with a 19 slide presentation of a step-by-step refactoring of some code posted to a mailing list. All in all, it was a great talk.
One of the interesting things he brought up was the automatic refactoring tools that many languages have. For example, some tools allow you to highlight a section of code and select "make a subroutine out of this". Try, just try, to write a Perl parser that can do that. Oh wait! There is one! It's called PPI and it purports to be a pure Perl parser. From the description by Schwern, it sounds very robust (it parsed MakeMaker, for example). However, the distribution has no docs. Of course, that's not really a problem for a wannabe XP guy. I just need to read the tests.
Oops. No tests.
It looks absolutely fascinating and if it's as powerful as I understood, I want to play with it, but I can't. If anyone has more info, please let me know.
On another note, someone asked at the talk if moving a bunch of stuff into functions would slow the code down as function calls are so expensive. Schwern, to his credit, did not breathe a word about premature optimization. Instead, he pointed out that by refactoring, we can generate cleaner code and start profiling it, thus finding out where the real bottlenecks are. Much better to optimize clean code than spaghetti.
While thinking about the function calls, I started wondering what it would take to have Perl automatically inline function calls when compiling to byte code. chromatic mentioned that he had done some work with this and had some truly elegant code that segfaults quite nicely. Then, Schwern and chromatic started pointing out the problems that scoping causes with trying to inline functions. Further, it's not always obvious what's going to happen when you inline. What happens if you shift something off of @_ and then reuse @_? Or worse, if the code diddles with @_, since it's an alias, I can imagine all sorts of nightmares.
I had briefly thought that I had an original idea, only to discover that others have trod this path before.
Also, I should mention that this is Portland Perl Monger's one-year anniversary of holding regular meetings. Next month, I've invited someone from the local Ruby group to give a brief Ruby tutorial. Much fun!