A blog about Modern Perl, bioinformatics and anything else that I feel like rambling about.

Monday, May 25, 2009

Superloading Devel::REPL

Today I bumped into Class::Autouse, pointed by App::Cmd's docs. Essentially, it lets you defer the importing of a module until the code actually tries to use it. So, for instance, if you have:


The program will load Deps 1..10 before executing Dep:2's constructor. However, with


Only Dep::2 will be use-d before the instantiation of $thing.

This can be very useful in cases where an application's flow can take several different routes, and might not use all of its functionality in a single invocation. Padre, the Perl IDE, for example, makes extensive use of this module exactly for this reason.

But another cool feature of this library is its ':superloader' option. It allows you to import dependencies on the fly without explicitly saying so. For example:


Here, we don't predeclare any class in particular, we just use it. Notice that I said "class" and not "module"; this only works for object oriented libraries, ie., classes.

What's most useful about this is that it can be very convenient in cases where you don't know beforehand what classes you will be needing. In particular, I think this is a perfect addition to my Devel::REPL configuration file:


and then:


Since I'm mostly doing exploration and debugging when using the REPL, correctness is not an issue, and not having to load things beforehand increases the whipuptitude and overall improves the coding experience.

2 comments:

  1. Jon Rockway wrote a Devel::REPL plugin, LoadClass, with a similar goal. It catches the "Foo::Bar is not loaded" error, loads Foo::Bar, then re-executes the line. I think it's slightly less nasty than superloader's injecting of UNIVERSAL::AUTOLOAD. :)

    ReplyDelete
  2. Ooo, didn't know about that! Will definitely try it out, thanks!

    ReplyDelete