Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 318657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:35:23+00:00 2026-05-12T08:35:23+00:00

I’m trying to implement a RESTful API in Perl. My current idea is to

  • 0

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.

For example to retrieve info about user 1234 the RESTful client should find it at:

http://example.com/model.pl/users/1234

Below is the skeleton code of my first attempt at implementing a RESTful API:

model.pl :

#!/usr/bin/perl -w
use strict;
use CGI;

my $q = CGI->new();

print $q->header('text/html');

my $restfuluri  = $q->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->p("GET question : $object -> $value");
}

sub users
{
      my $object = shift;
      my $value  = shift;

      #This is a stub, spits out JSON or XML when implemented.
      print $q->p("GET user: $object -> $value");
}

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.

I can imagine, after a while, the if/else block would grow really large.

Looking forward to hear your views to make this code better.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:35:23+00:00Added an answer on May 12, 2026 at 8:35 am

    I would use something like CGI::Application::Dispatch, it lets me build a dispatch table with variables and REST methods, and lets you use CGI and CGI::Application modules from CPAN. E.g.:

    table => [
    '/questions/:id[get]'    => { rm => 'get_question' },
    '/users/:id[get]'        => { rm => 'get_user' }, # OR
    ':app/:id[post]'         => { rm => 'update' }, # where :app is your cgi application module
    ':app/:id[delete]'       => { rm => 'delete' },
    ],
    

    (or you can use auto_rest or auto_rest_lc)

    you can use a separate CGI::Application class for each type of thing (or just use classes in your cgi-app controller class methods).

    CGI::Application also comes with plugins for outputting XML, JSON or text generated from templates.

    cgi-app (and c::a::d) are are CGI applications and can be used with (little or) no change under CGI, FastCGI or mod_perl. C::A::D is also a mod_perl PerlHandler by default too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 349k
  • Answers 349k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could try... runOnUiThread(new Runnable(){ public void run() { setContentView(R.layout.main_layout);… May 14, 2026 at 6:49 am
  • Editorial Team
    Editorial Team added an answer This is a browser limitation - browsers instances share cookie… May 14, 2026 at 6:49 am
  • Editorial Team
    Editorial Team added an answer Ok, I gave up on above code, switching to a… May 14, 2026 at 6:49 am

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.