Click to Play

Google Spices Up Enterprise...
Along with the HTML5 and Google TV announcements at Google I/O, there were also several that pertained to business, involving enterprise and...

Recent Articles

Perl's Staying Power Advertised Again
ActiveState is a company that specializes in dynamic languages, and whether or not you've heard of it, it's quite successful, with a long list of customers that includes impressive organizations like Cisco, HP, and...

10 Perl Foundation/Parrot Foundation Projects...
Not too long ago, we talked about Google's annual Summer of Code program and its potential importance to Perl. Now, that importance has been more or less confirmed, as the Perl Foundation and the...

Perl 5.12.0 Released
A new version of Perl has arrived, and it should prove deserving of a very warm welcome. Perl 5.12.0 was a long time coming, and it represents a significantly more advanced offering than what was available...

BioPerl Makes Google Summer Of Code Cut
Google's annual Summer of Code is a rather big deal, bringing together excellent students from all over the world to work on open source software projects.

Perl-AdWords Crisis Foreseen, Averted
It looks like the Perl community suffered a close call at the hands of a certain search giant. Although allowances have now been made, John Napiorkowski stated early yesterday, "on April 22nd SIGNIFICANT....



06.01.10



A Whole New World (Wide Web)

By Michael Marr

Although Perl may not be able to produce flying carpets, blue genies, or beautiful Arab princesses, it can be used for nearly limitless other applications. One of Perl's greatest strengths is playing nice with other programming languages, and thus why it may be valuable to experiment with wrapping popular APIs into a Perl script that can be used by the application(s) of your choice.

You won't need a charming monkey sidekick to access APIs with Perl. Instead, you'll need the nice little LWP module. Using this module, we're able to rub the lamp and connect to the nearly limitless possibilities of the World Wide Web (there are some rules limiting your World Wide Web requests, i.e. although many sites on the web would disagree with this, the Web can't make someone fall in love with you). Instead, for many APIs on the web, you will need to authenticate your request. This can often be done via the following call:my $ua = LWP::UserAgent->new;
$ua->credentials('api-host.website.com:port', 'Some Realm/API Keyword', 'username', 'password');

Using the above code will setup HTTP authentication compliant with RFC 2616, and is often required when accessing a public API. The second and/or alternative authentication step you may run into is adding special headers to your request:

use HTTP:Headers;

$ua->default_headers->push_header('Header Field' =>'special value');


After getting your all your "open sesame"s in place, you're ready to push out your request. Normally, this will be accomplished by building an appropriate URI and pushing it out the API. Once you have sent your HTTP request, you wait for the matching response, and handle accordingly.

my $response = $ua->post('http://api-host.website.com/api_request/get_data.xml');

Many APIs give you options on the return response, whether it be in XML, JSON, or other format. Once you have your response in the format of your choice, use an appropriate Perl module to manipulate your data, and fully wield the power of the true LAMP(erl). Here's one short example utilizing Twitter's API:
#! /usr/bin/perl -w


# demo of accessing an API (Twitter, in this case) with Perl
# see more on Twitter's API: http://apiwiki.twitter.com/

my $username = 'mikemarr33';
my $password = 'topsecretz';

use LWP;
use HTTP::Headers;


my $ua = LWP::UserAgent->new;

$ua->credentials('twitter.com:80', 'Twitter API', $username, $password);


# we don't need any special headers here - Twitter's API does not require it.
# $ua->default_headers->push_header();

# we are using the HTTP request method get instead of post, as this
# API request requires
my $response = $ua->get('http://twitter.com/statuses/friends.xml');


# here, we could manipulate the response however we desire, but for
# conciseness and demonstration purposes, we'll print to see that
# we're getting relevant information
print $response->content;

Although some may say that you always have been and always will be a street rat, I am optimistic that you can use the proper tools to get out and be productive in that big World Wide Web.


About the Author:
Michael Marr is a IT staff Writer for WebProNews.
About PerlProNews
PerlProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of Perl environments. News and Advice for Perl Professionals





PerlProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
PerlProNews.com SQLProNews.com
SysAdminNews DevWebPro.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITCertificationNews.com






-- PerlProNews
is an iEntry, Inc. publication --
iEntry, 2549 Richmond Rd. Lexington KY, 40509
2010 iEntry, Inc. All Rights Reserved Privacy Policy Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article



Database Forum News and Advice for Perl Professionals PerlProNews News Archives About Us Feedback PerlProNews.com About Article Archive News Downloads WebProWorld Forums iEntry Advertise Contact Jayde