Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

bitingduck's scratchpad

by bitingduck (Friar)
on May 19, 2012 at 01:38 UTC ( #971379=scratchpad: print w/ replies, xml ) Need Help??

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.

#!/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 browse +r. 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->connect("DBI:mysql:host=localhost;database=boson_db","roo +t","password",{PrintError=>0, RaiseError=>1}); #This sets up the database query my $sth= $dbh->prepare("SELECT e_isbn13 from metadata where e_isbn13>0 +"); #then we execute it. $sth then points to the returned object #that contains all the data $sth->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->execute(); while ( my @row = $sth->fetchrow_array ) { my $itunesrec=get("$itunesq@row"); #get the url my $json = JSON->new->allow_nonref; my $out=$json->decode($itunesrec)->{'results'}[0]; #print Dumper($out); print "$out->{'trackViewUrl'}\n"; for (keys %$out) { print "$_ => $out->{$_}\n"; } #print "$itunesq@row\n"; #print $itunesrec."\n"; }

Newest Nodes
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-08-31 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls