A web service to unshorten short URLs, in modern Perl
blog.jrock.us
blog.jrock.us
I don't even write Python, but looking at it has never given me a seizure. Not that your code is seizure-inducing, mind you.
In contrast, Python seems to have more style conventions designed into the syntax and the language, and when I last did much with ruby (pre-rails) it seemed pretty intuitive overall (something about "principle of least surprise").
I don't have much skin in this game: I'm a sysadmin by trade and tend to stick to the traditional Unix tools, but I would like any suggestions you have for people who want to develop good taste in Perl syntax and program design.
I've thought about writing a non-insane style guide for Perl, but the more I think about it, the less important I think style is. I can read code regardless of the style, and honestly, a style that's different from mine doesn't slow me down at all.
It's a shame that people think PBP is "the Perl style guide", though, and I'd really like to correct that misconception... but it just doesn't matter at all.
Next we'll hear that Lisp has parentheses.
Here are some interesting recent blogs for non Perl echo chamber members....
* http://www.bofh.org.uk/2009/02/20/marking-time
* http://www.bofh.org.uk/2009/02/22/moose-for-ruby-programmers
* http://www.bofh.org.uk/2009/03/11/writing-parsers-for-fun-an...
* http://www.bofh.org.uk/2009/03/13/magic-vs-mundane-keeping-t...
* http://www.modernperlbooks.com/mt/2009/03/working-with-testc...
def expand_url(url):
return urllib2.urlopen(url).geturl()The equivalent to what you describe is:
LWP::UserAgent->new->get($url)->request->uriHowever, I appreciate your post for enlightening what modern Perl looks like. After a quick skim I thought I was looking at Rails.
The point of the article is not the service, but rather what the code looks like. Returning "Hello world" is almost as instructive, but not quite, so I chose a dumb example instead.
It's also about Kiouku , POE and Moose's Modern Object system. And how Perl5 is still evolving and being actively modernized.
Edited for asyncronous feedback.
Used as so: http://localhost:9242/http://tinyurl.com/unicycles
import urllib2, cherrypy.wsgiserver
cache = {}
def app(environ, start_response):
start_response('200 OK', [('Content-type','text/plain')])
url = environ['PATH_INFO'].lstrip('/')
yield 'Looking up: ' + url + '...'
try:
result = cache.get(url)
if result is None:
result = urllib2.urlopen(url).geturl()
cache[url] = result
except urllib2.HTTPError:
result = "URL Doesn't exist"
except ValueError:
result = "Invalid Url"
yield result
cherrypy.wsgiserver.CherryPyWSGIServer(('127.0.0.1', 9242), app).start()