Fancy but untested oneliners
You know the cool new oneliner module ojo? It’s cool, especially with Mojo::DOM:
$ perl -Mojo -le 'print g("heise.de")->dom->at("h1 a")->text'
Oracle kehrt OpenSolaris den Rücken
It’s also a server, if you want:
$ perl -Mojo -e 'a("/" => {text => "AWESOME!"})->start' daemon
Unfortunately, ojo isn’t tested very well, so I wrote tests for its server and client part.
But there’s one issue - the error handling of the client part. The return value of a g() or f() call is a Mojo::Message::Response object, which works great for most cases and makes your oneliners short. The big Mojo::Client returns Mojo::Transaction objects, which contain request and response objects. That’s important if there’s an error on the request side, for example an invalid host. If you call the error method of transaction objects, you’ll get the request error; but with ojo we only have the response object, which is empty in case of request errors.
Two possible solutions:
- let
ojoreturn transaction objects, so user code can check its error method, which gets all request and response errors. Disadvantage: you’ll have to write->resafter almost all request function calls. In addition, its uncommon for oneliners to have an explicit error handling. - let
ojodie on request errors, which is fine for oneliners. The Klingon say
It’s better to die than to come back in failure
They may be right.