1 Replies - 7883 Views - Last Post: 17 August 2012 - 06:44 AM

#1 smilykoch  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 23-April 12

TCP JSON in perl? and defining headers?

Posted 22 July 2012 - 10:59 AM

Hey Guys..

I'm currently working on a project, where i have a webpage (local), and i want to interface with a program, on serverside (local server) through this webpage, this would be done through TCP requests in JSON format..

I have learned, that this is not possible through jQuery/Javascript/HTML5 etc., but i have been told that it should be possible to do in Perl, but i desperately need a hint on how?

This is the program i want to interface with:

http://h3x.se[...]

And it is to be used in a local webpage i have made in my home to control my multi-media system

Cheers Mathias.

Is This A Good Question/Topic? 0
  • +

Replies To: TCP JSON in perl? and defining headers?

#2 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 46
  • View blog
  • Posts: 419
  • Joined: 28-September 08

Re: TCP JSON in perl? and defining headers?

Posted 17 August 2012 - 06:44 AM

I have a website where I'm using Perl and JSON to do this sort of thing. Basically I have a list of cars I'm looking at buying. When I access the web site it serves up the car listings from my database. The name of the script is called pullcars.pl and the web site is optionsdb.html:

options.db
<?xml version="1.1" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
   <head>
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
      <script type="text/javascript">
         window.onload 
          = function()
            {
            
               var htmlString = "";

               $.ajax(
               {
                  type: "GET",
                  url: "/cgi-bin/pullcars.pl", // URL of the Perl script
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  error: function() 
                  { 
                     alert('Error retrieving car data from database. Consult web admin.');
                  },
                  success: function(data)
                  {
                     if (data.error)
                     {
                        alert('The server is listening, but the data is invalid.');
                     }
                     else // Good to go. Populate html div.
                     {

                        // TODO: embed within a table.
                        $.each(data, function(key, value){
                           $.each(value, function(key, value){
                              htmlString += "<pre>";
                              htmlString += value.id + ".) </br>";
                              htmlString += "year: " + value.year + "</br>";
                              htmlString += "make: " + value.make + "</br>";
                              htmlString += "model: " + value.model + "</br>";
                              htmlString += "cost: " + value.cost + "</br>";
                              htmlString += "mileage: " + value.mileage + "</br>";
                              htmlString += "mpg: " + value.mpg + "</br>";
                              htmlString += "<a href=" + value.link + "\>";
                              htmlString += value.model + " link</a></br>";
                              htmlString += "</pre>"; 
                           });
                        });
                        $('div#populate').append(htmlString);
                     }
                  }
                        
               });
               
            }               
      </script>
   </head>
   <body>
   </body>
<html>

<pre>
<h3>Important Links:</h3>
<a href="www.kbb.com">Kelly Blue Book</a>
<a href="http://www.fueleconomy.gov/">Mileage Reports Site</a>
</pre>

<pre>
<b>Non-Honda Notes:</b>
Out of all the above the Prius has the best mileage and MPG. It is however the most expensive.
The Elantra isn't to bad. It has an MPG of 26. Cost is cheap.
</pre>

<pre>
<b>Honda Notes:</b>
The fourth Honda has been driven for about 65,000 miles and is a bit more expensive.
I really like the price of the one you mentioned from ebay. That's 3 years newer than
the 2003. These two cars (1 and 2) have similar prices. I talked to a sales representative
of the car at 6. This vehicle is one year older than option 1 and a little bit more 
expensive. It is however a similar match.
</pre>

<h3>Used Cars (Honda and Non-Honda):</h3>
<div id="populate"> <!-- This will be populated by the jQuery script and Perl CGI. -->
</div>

</html>



pullcars.db (I blocked out my database password):
#!/usr/bin/perl -T
#####
# Serve up car data from MySQL database.
#####
use CGI;
use DBI;
use strict;
use warnings;

# read the CGI params
my $cgi = CGI->new;

# connect to the database

# TODO: encrypt this password.
my $dbh = DBI->connect("DBI:mysql:database=PRIMARY_DB;host=localhost;port=3306","root", "###########") 
    or die $DBI::errstr;

# Prepare and execute the generic query.
my $statement = qq{SELECT * FROM USED_CARS};
my $sth = $dbh->prepare($statement)
    or die $dbh->errstr;
$sth->execute()
    or die $sth->errstr;

# Returned values from USED_CARS table in database.
my $id      = 0;
my $year    = 0;
my $make    = "";
my $model   = "";
my $cost    = 0;
my $mileage = 0;
my $mpg     = 0;
my $link    = ""; # url

# Wrap the queried data into a JSON object and transport back to client.

my $json = "{ \"usedcars\": [";
while ( ($id, $year, $make, $model, $cost, $mileage, $mpg, $link) = $sth->fetchrow_array() ) 
{
    $json .= "{\"id\":\"$id\",\"year\":\"$year\",\"make\":\"$make\",\"model\":\"$model\",\"cost\":\"$cost\",\"mileage\":\"$mileage\",\"mpg\":\"$mpg\",\"link\":\"$link\"},"; # car entry
}

# Chop off the last character of ',' so the JSON is properly formatted.
$json = substr($json, 0, -1); 

# Complete the JSON object.
$json .= "]}";

# $userID will be 0 if the username and password don't select.
#print "Content-type:text/html\n\n;"; # return straight html
#($userID) ? 
#    print "<div>Welcome User</div>" :
#    print "<div>Invalid Credentials</div>";

# return JSON
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1