Click here to Skip to main content
15,354,171 members
Articles / Programming Languages / SQL
Posted 9 Aug 2013


1 bookmarked

Integrating Perl REST service with jQuery and a database

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
9 Aug 2013CPOL4 min read
This article is a novice level demonstration of using jQuery consuming a REST Web Service written in Perl.


This article is a novice level demonstration of using jQuery consuming a REST Web Service written in Perl, that performs a database operation. This is just a demonstration to show the inter-operability of these technologies. There are several features of all of these technologies far and above those portrayed here.

We would showcasing only the GET method to hit the web service.



Representational State Transfer (REST) defines a set of architectural principles by which you can design Web services that focus on a system's resources, including how resource states are addressed and transferred over HTTP by a wide range of clients written in different languages. If measured by the number of Web services that use it, REST has emerged in the last few years alone as a predominant Web service design model. In fact, REST has had such a large impact on the Web that it has mostly displaced SOAP- and WSDL-based interface design because it's a considerably simpler style to use.


jQuery is a multi-browser JavaScript library designed to simplify the client-side scripting of HTML. It was released in January 2006 at BarCamp NYC by John Resig. It is currently developed by a team of developers led by Dave Methvin. Used by over 65% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use today.

Perl and Perl Dancer

Perl is a family of high-level, general-purpose, interpreted, dynamic programming languages. The languages in this family include Perl 5 and Perl 6. Though Perl is not officially an acronym, there are various backronyms in use, such as: Practical Extraction and Reporting Language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier.

Dancer is an open source lightweight web application framework written in Perl and inspired by Ruby's Sinatra.


To keep this article as simple as possible, we would be using a CSV file as a database table. The extensive Perl libraries make this possible.

Before We Go Ahead

Before proceeding further, we need to address the minimum requirements for this article: (Refer to the Resources section for more details)

  1. Perl
  2. A Web Browser
  3. jQuery Library
  4. Perl Dancer Module
  5. Perl DBI and DBD::CSV Modules

Let's roll

There are a couple of parts to get this whole thing working.

First of all, we create a file with the following contents and save it with any name (users in this case). This would be treated as a database table with the columns "username", "password" and "role". Perl gives us the interface to run normal SQL queries on this flat file as if it were a full fledged database table. This table would be used as the data source for the length of this article.


Then we create the web service that would be using the CSV file created above as a database. This is the service that we would be using for the rest of this guide.

Here we are utilizing the DBI module that is used to connect with the CSV flat file and use it as a database table.

The Dancer module is the framework that gives us the tools to create a web service in no time. The serializer specifies that we would be expecting data in JSON format. We also have the option of using XML as another output format.

Here we specify that we would be expecting 2 parameters from the calling mechanism, viz., username and password. These parameters are then validated against the values in the database and then an appropriate message is drafted and returned from the code.

use Dancer;
use JSON;
use DBI;
set serializer => 'JSON'; 

get '/login/:name.:password' => sub {
    my $json = {};
    my $user = params->{name};
    my $password = params->{password};
    my ($dbh, $sth, $query);
    $dbh = DBI->connect ("dbi:CSV:", undef, undef, {csv_tables => { info => { 
              file => "users.csv" }}}) or die "Cannot connect: $DBI::errstr";

    $dbh->{RaiseError} = 1;
    $query = "select password, role from users where username = '" . $user ."'";
    $sth = $dbh->prepare ($query);
    while (my $row = $sth->fetchrow_hashref) {
        if ($row->{password} eq $password) {
            return {status => 'success', role => $row->{role}};
        } else {
            return {status => 'error', role => undef};

The jQuery shown below would be the core that would drive the UI.

  $("form#loginForm").submit(function() { // loginForm is submitted
    var username = $('#username').attr('value'); 
    var password = $('#password').attr('value'); 

    if (username && password) { 
        type: "GET",
        cache: false,
        url: "http://localhost:3000/login/" + username + "." + password,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        error: function(XMLHttpRequest, textStatus, errorThrown) { 
          $('div#loginResult').text("responseText: " + XMLHttpRequest.responseText 
            + ", textStatus: " + textStatus 
            + ", errorThrown: " + errorThrown);
        success: function(data){
        if (data.role) {
            $('div#loginResult').text("Login successful" + ", Role: " + data.role);
          else { 
            $('div#loginResult').text("Login error");
    else {
      $('div#loginResult').text("Enter username and password");
    return false;

Here we create a web page in the form of a simple login form. This form takes in a username and a password. These values are then passed on to the webservice. The webservice, as depicted above, runs a query to authenticate these vales and responds with an appropriate message. This message is interpreted by the jQuery and presented in a visually distinctive form. Combining the jQuery into a HTML page gives us the following.

<html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Integrating Perl REST service with JQuery and a database</title>
    <link rel="stylesheet" type="text/css" media="screen, projection" 
      href="" />
    <link rel="stylesheet"  type="text/css" media="screen, projection"
      href="" />
    <link rel="stylesheet" type="text/css" media="print" 
      href="" />
    <!--[if IE]><link rel="stylesheet" type="text/css" media="screen, projection" 
    <script type="text/javascript" 
    <script type="text/javascript" src="login.js"></script>
    <style type="text/css">
      #loginContent { width: 350px; margin: 100px auto; }
      button[type] { margin: 0.5em 0; }
    <div id="loginContent" class="container">
      <div id="loginResult" style="display:none;">
      <form id="loginForm" name="loginForm" method="post" action="">
          <legend>Enter information</legend>
            <label for="username">Username</label>
            <br />
            <input type="text" id="username" name="username" class="text" size="20" />

            <label for="password">Password</label>
            <br />
            <input type="password" id="password" name="password" class="text" size="20" />
            <button type="submit" class="button positive">
             <img alt="ok" src="" /> 

The HTML page would look something like

Image 1

Let's try this out.

Here, we provide a correct login information and the web service response is interpreted and displayed

Image 2

Also, if we provide a incorrect login, the response is interpreted and displayed

Image 3


Please note that this article is purely for demonstration purposes. This is not recommended to be used in any production environment. It merely shows the capacity of various technologies to be connected.


  • Most of this article is an extension of a similar article on IBM DeveloperWorks
  • jQuery is a cross-browser JavaScript library which, among other capabilities, simplifies Ajax interactions for rapid web development.
  • JSON is a lightweight data format (compared to XML) for the exchange of information between the browser and server.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sudeep Hazra
Software Developer
India India
No Biography provided

Comments and Discussions

GeneralMy vote of 1 Pin
Carlos Luis Rojas Aragonés2-Oct-13 15:41
professionalCarlos Luis Rojas Aragonés2-Oct-13 15:41 
GeneralRe: My vote of 1 Pin
Sudeep Hazra18-Oct-13 8:40
MemberSudeep Hazra18-Oct-13 8:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.