Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
A web service to unshorten short URLs, in modern Perl (jrock.us)
24 points by jrockway on April 6, 2009 | hide | past | favorite | 21 comments


I posted this so that people can see what Perl applications actually look like these days. A lot of folks here haven't looked at Perl since 1995, and assume it's the same now as it was then. It's not :)


It is impressively more readable than some legacy Perl code I've had the misfortune of working with. That being said, just on a quick once-over for aesthetics I'm not left thinking "Ahh, modern Perl sure looks less painful than modern Ruby or modern Python."

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.


I've had some problems diving into Perl land, and I'd appreciate your thoughts. While it's obviously very possible to have clean and elegant code in any language (corollary: you can write cobol in any language), I've found that when I'm stuck and I go to Google for a hint, the piece of code I'll end up with will never follow any common style guidelines -- it's always different.

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 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.


Yea, but that is not common day's Perl, this is extremely modern Perl! Just out of the CPAN oven Perl.


True. It's Perl that you could be writing, though.


Hang on, this wasn't satire? I wasn't 100% sure so I came to see the comments and.. it seems I'm wrong :)



What did you think was satire? That variable names have dollar-signs in front of them? Haha, that's so funny!

Next we'll hear that Lisp has parentheses.


No, not the sigils, but the level of over-engineering just to get going. Look at that first code example. Unintelligible or what. If this is where Perl's going, I'm glad I ditched it several years ago.


It's unintelligible to use a "class" keyword for defining classes?


No.. why? Was that a joke? It's a poor show of one's convictions when one has to resort to playground tactics to try and make someone look silly rather than discuss something like an adult :(


In modern Python:

    def expand_url(url):
        return urllib2.urlopen(url).geturl()


Sure, now do that asynchronously (including DNS lookups) and add a web frontend. Then you will have something close to what the article describes.

The equivalent to what you describe is:

    LWP::UserAgent->new->get($url)->request->uri


Including threading, cache, and rudimentary web front end.

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()


Very nice. Having real threads is convenient.


Haha.


Agreed, it is not the same, but I'm saying if I needed to resolve a shortened URL, I would do it myself instead of sending an HTTP request to you so that you can send an HTTP request on my behalf. At any rate, using your service would be slower because of the extra roundtrip, and more unreliable.

However, I appreciate your post for enlightening what modern Perl looks like. After a quick skim I thought I was looking at Rails.


One advantage of my "service" is that if the URL redirector goes down, you still have a chance at getting my cached copy.

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.


which is also most definitely not just about having TryCatch and Class syntax

It's also about Kiouku , POE and Moose's Modern Object system. And how Perl5 is still evolving and being actively modernized.


This perl is so modern, it's looked like this for over a decade. It's only gotten more featureful since then.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: