couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Update of "GettingStartedWithPerl" by NoahSlater
Date Thu, 27 Mar 2008 00:05:07 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The following page has been changed by NoahSlater:
http://wiki.apache.org/couchdb/GettingStartedWithPerl

The comment on the change is:
Copied from the original CouchDB wiki

New page:
Getting started with Perl and the CouchDB API.

The following shows the basics of working with the raw CouchDB REST api from Perl. If you
want a richer interface that more tightly maps Couch documents into Perl (so you don't have
to deal with JSON manually, for example), you should check out the [http://search.cpan.org/~dgl/Net-CouchDb-0.01/lib/Net/CouchDb.pm
Net::!CouchDb] API.

Note that these examples all use the new version of !CouchDB, with a JSON interface rather
than XML.

== Example Wrapper Class ==

You can save this to a file named ''CouchDB.pm'', then just use it from your program with
''use CouchDB;''. But if you're going to do anything more than mess around, I would recommend
getting the Net::!CouchDb module from CPAN.

{{{
package CouchDB;

use strict;
use warnings;

use LWP::UserAgent;

sub new {
  my ($class, $host, $port, $options) = @_;

  my $ua = LWP::UserAgent->new;
  $ua->timeout(10);
  $ua->env_proxy;

  return bless {
                ua       => $ua,
                host     => $host,
                port     => $port,
                base_uri => "http://$host:$port/",
               }, $class;
}

sub ua { shift->{ua} }
sub base { shift->{base_uri} }

sub request {
  my ($self, $method, $uri, $content) = @_;

  my $full_uri = $self->base . $uri;
  my $req;

  if (defined $content) {
    #Content-Type: application/json

    $req = HTTP::Request->new( $method, $full_uri, undef, $content );
    $req->header('Content-Type' => 'application/json');
  } else {
    $req = HTTP::Request->new( $method, $full_uri );
  }

  my $response = $self->ua->request($req);

  if ($response->is_success) {
    return $response->content;
  } else {
    die($response->status_line . ":" . $response->content);
  }
}

sub delete {
  my ($self, $url) = @_;

  $self->request(DELETE => $url);
}

sub get {
  my ($self, $url) = @_;

  $self->request(GET => $url);
}

sub put {
  my ($self, $url, $json) = @_;

  $self->request(PUT => $url, $json);
}

sub post {
  my ($self, $url, $json) = @_;

  $self->request(POST => $url, $json);
}

1;
}}}

== Creating a Database ==

To create a database called ''foo'':

{{{
my $db = CouchDB->new('localhost', '5984');
$db->put("foo");
}}}

== Deleting a Database ==

To delete a database called ''foo'':

{{{
my $db = CouchDB->new('localhost', '5984');
$db->delete("foo");
}}}

== Creating a Document ==

To create a document in the database ''foo'' with the id ''document_id'':

{{{
my $db = CouchDB->new('localhost', '5984');
$db->put("foo/document_id", <<JSON);
{
 "value":
 {
   "Subject":"I like Perl",
   "Author":"Rusty",
   "PostedDate":"2006-08-15T17:30:12-04:00",
   "Tags":["plankton", "perl", "decisions"],
   "Body":"I decided today that I don't like plankton. I like perl. Or DO I?"
 }
}
JSON
}}}

== Reading a Document ==

To read a document from database ''foo'' with the id ''document_id'':

{{{
my $db = CouchDB->new('localhost', '5984');
my $json = $db->get("foo/document_id");
}}}

Mime
View raw message