title: How can I implement a RESTful API in Perl?
body: <p>I'm trying to implement a RESTful API in Perl. My current idea is to simply parse the path_info with a regex then dispatch the request to the appropriate subroutine which will then spit out the JSON, XML or even XHTML for the requested resource.</p>
  
  <p>For example to retrieve info about user 1234 the RESTful client should find it at:</p>
  
  <p><strong><a href="http://example.com/model.pl/users/1234" rel="noreferrer">http://example.com/model.pl/users/1234</a></strong></p>
  
  <p>Below is the skeleton code of my first attempt at implementing a RESTful API:</p>
  
  <p><strong>model.pl</strong> :</p>
  
  <pre><code>#!/usr/bin/perl -w
  use strict;
  use CGI;
  
  my $q = CGI-&gt;new();
  
  print $q-&gt;header('text/html');
  
  my $restfuluri  = $q-&gt;path_info;
  
  if      ($restfuluri =~ /^\/(questions)\/([1-9]+$)/) { questions($1, $2); }
  elsif   ($restfuluri =~ /^\/(users)\/([1-9]+$)/)     { users($1, $2); }
  
  
  sub questions
  {
        my $object = shift;
        my $value  = shift;
  
        #This is a stub, spits out JSON or XML when implemented.
        print $q-&gt;p("GET question : $object -&gt; $value");
  }
  
  sub users
  {
        my $object = shift;
        my $value  = shift;
  
        #This is a stub, spits out JSON or XML when implemented.
        print $q-&gt;p("GET user: $object -&gt; $value");
  }
  </code></pre>
  
  <p>Before I proceed any further, I would like to hear from experienced Perl hackers whether I got the basic idea right and if there are any serious shortcomings with this approach in terms of performance. </p>
  
  <p>I can imagine, after a while, the if/else block would grow really large.</p>
  
  <p>Looking forward to hear your views to make this code better.</p>