UNIVERSAL::new for command line MU

Years ago, chromatic released Acme::UNIVERSAL::new. It was a joke. This is not.

Using object-oriented modules from the command line can be a PITA. I have to type the module name twice: once to load it and once to construct objects with it. In a program, that’s not huge overhead, but in a one-liner, it annoys me every time.

So I wrote UNIVERSAL::new and a handy alias U. It lets you call “new” on a module name and have it loaded on the fly. Like this:

$ perl -MU -we 'HTTP::Tiny->new->mirror("http://www.perl.org/", "perl.html")'

If the module can’t be loaded, or if the module doesn’t have a “new” method, things just die in the usual way.

Of course, messing with UNIVERSAL is naughty, so save this for the one-liners, OK?