Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: