CouchDB::View::Document - CouchDB design document abstraction


  my $doc = CouchDB::View::Document->new({
    _id => "_design/mystuff",
    views => {
      by_name => sub {
        my ($doc) = @_;
        dmap($doc->name, $doc);
      by_whatsit => sub {
        my ($doc) = @_;
        require Whatsit::Parser;
        dmap(Whatsit::Parser->parse($doc), $doc);

  # use with a hypothetical client
    '/mydatabase/' . $doc->uri_id,


CouchDB::View::Document provides a Perlish interface to creating CouchDB views. It uses Data::Dump::Streamer to serialize coderefs, which are deserialized and used by CouchDB::View::Server.


Read the CouchDB wiki page on views if you have not already. Only Perl specifics will be mentioned here.

The map function is already used in Perl. Instead, use dmap() (as in the "SYNOPSIS") to map keys to values.

Perl does not have null. Use undef.

All the limitations of Data::Dump::Streamer apply to your view functions. In particular, if they use external modules, they will need to require or use them explicitly (see the Whatsit::Parser example in the "SYNOPSIS"). Likewise, closed-over variables will be dumped, but external named subroutines will not, so this won't work:

  sub elide {
    my $str = shift;
    $str =~ s/^(.{10}).+/$1.../;
    return $str;

  my $doc = CouchDB::View::Document->new({
    views => {
      elided => sub {
        dmap(elide($doc->{title}), $doc);

The definition of elide is not transferred to the view server.



  my $doc = CouchDB::View::Document->new(\%arg);

Create a new design document. See the CouchDB view API for the details of \%arg, though language is ignored (always 'text/perl').


  print $doc->as_json;

Use as_hash (below) and JSON::XS to encode the result. This is suitable for passing directly to a PUT to CouchDB.


  print $doc->uri_id;
  # '_design%2Fmyview'

Convenience method for the document name, since '/' must be escaped.


  print Dumper($doc->as_hash);

Return a hashref suitable for serializing to JSON, including serialized coderefs.


This method is called with a coderef and is expected to return a serialized representation of it. You probably don't need to use this unless you're subclassing CouchDB::View::Document.


CouchDB::View::Server CouchDB::View