<?xml version="1.0" encoding="windows-1252"?>
<node id="971379" title="bitingduck's scratchpad" created="2012-05-18 21:38:05" updated="2012-05-18 21:38:05">
<type id="182711">
scratchpad</type>
<author id="424801">
bitingduck</author>
<data>
<field name="doctext">
&lt;p&gt;here's a simple example of looking into a remote database to find a book and getting a piece of information (the url for the book) from the record that is returned.  Adding a new record could be as simple as checking to see if anything is returned, and if nothing, construct a new http request to submit the information to create a new record.&lt;/p&gt;


&lt;code&gt;
#!/usr/bin/perl
# simple example program that pulls a list of ISBNs out of a database
# then searches the iBookstore to find them 
# iBookstore returns the data in a JSON record, so we use JSON
# to get the piece we want

#use strict and warnings so perl will catch errors:
use strict;
use warnings;
use DBI;    #DBI is a database interface library
use JSON;   #JSON is a library for dealing with the returned data
use LWP::Simple;  #LWP is a very powerful library to simulate a browser.  LWP::Simple is a simple interface to it
use Data::Dumper;  # A lazy way to display test data

#base query for getting records from isbn:
my $itunesq='http://itunes.apple.com/lookup?isbn=';

#connect to the database
my $dbh=DBI-&gt;connect("DBI:mysql:host=localhost;database=boson_db","root","password",{PrintError=&gt;0, RaiseError=&gt;1});
				
#This sets up the database query		
my $sth= $dbh-&gt;prepare("SELECT e_isbn13 from metadata where e_isbn13&gt;0");

#then we execute it. $sth then points to the returned object
#that contains all the data $sth-&gt;fetchrow_array returns a
# row from the $sth object as an array, but without me
#having to worry about how it's stored in $sth
$sth-&gt;execute();

while ( my @row = $sth-&gt;fetchrow_array ) {
	my $itunesrec=get("$itunesq@row");  #get the url
	my $json = JSON-&gt;new-&gt;allow_nonref;
	my $out=$json-&gt;decode($itunesrec)-&gt;{'results'}[0];
	#print Dumper($out);
	print "$out-&gt;{'trackViewUrl'}\n";
	
	for (keys %$out)
    {
        print "$_ =&gt; $out-&gt;{$_}\n";
    }
	
    #print "$itunesq@row\n";
    #print $itunesrec."\n";
  }
&lt;/code&gt;
&lt;br /&gt;[id://3628]</field>
</data>
</node>
