mod_perl logo perl icon
previous page: APR::String - Perl API for manipulating APR UUIDspage up: mod_perl 2.0 APInext page: APR::ThreadMutex - Perl API for APR thread mutexes

APR::Table - Perl API for manipulating APR opaque string-content tables






Writing Apache Modules with Perl and C

Writing Apache Modules with Perl and C

By Lincoln Stein, Doug MacEachern
Embedding Perl in HTML with Mason

Embedding Perl in HTML with Mason

By Dave Rolsky, Ken Williams
mod_perl2 User's Guide

mod_perl2 User's Guide

By Stas Bekman, Jim Brandt
Practical mod_perl

Practical mod_perl

By Stas Bekman, Eric Cholet
The mod_perl Developer's Cookbook

The mod_perl Developer's Cookbook

By Geoffrey Young, Paul Lindner, Randy Kobes
mod_perl Pocket Reference

mod_perl Pocket Reference

By Andrew Ford


Table of Contents

Synopsis

  use APR::Table ();
  
  $table = APR::Table::make($pool, $nelts);
  $table_copy = $table->copy($pool);
  
  $table->clear();
  
  $table->set($key => $val);
  $table->unset($key);
  $table->add($key, $val);
  
  $val = $table->get($key);
  @val = $table->get($key);
  
  $table->merge($key => $val);
  
  use APR::Const -compile qw(:table);
  $table_overlay = $table_base->overlay($table_overlay, $pool);
  $table_overlay->compress(APR::Const::OVERLAP_TABLES_MERGE);
  
  $table_a->overlap($table_b, APR::Const::OVERLAP_TABLES_SET);
  
  $table->do(sub {print "key $_[0], value $_[1]\n"}, @valid_keys);
  
  #Tied Interface
  $value = $table->{$key};
  $table->{$key} = $value;
  print "got it" if exists $table->{$key};
  
  foreach my $key (keys %{$table}) {
      print "$key = $table->{$key}\n";
  }


TOP

Description

APR::Table allows its users to manipulate opaque string-content tables.

On the C level the "opaque string-content" means: you can put in '\0'-terminated strings and whatever you put in your get out.

On the Perl level that means that we convert scalars into strings and store those strings. Any special information that was in the Perl scalar is not stored. So for example if a scalar was marked as utf8, tainted or tied, that information is not stored. When you get the data back as a Perl scalar you get only the string.

The table's structure is somewhat similar to the Perl's hash structure, but allows multiple values for the same key. An access to the records stored in the table always requires a key.

The key-value pairs are stored in the order they are added.

The keys are case-insensitive.

However as of the current implementation if more than value for the same key is requested, the whole table is lineary searched, which is very inefficient unless the table is very small.

APR::Table provides a TIE Interface.

See apr/include/apr_tables.h in ASF's apr project for low level details.



TOP

API

APR::Table provides the following functions and/or methods:



TOP

add

Add data to a table, regardless of whether there is another element with the same key.

  $table->add($key, $val);

When adding data, this function makes a copy of both the key and the value.



TOP

clear

Delete all of the elements from a table.

  $table->clear();


TOP

compress

Eliminate redundant entries in a table by either overwriting or merging duplicates:

  $table->compress($flags);

Converts multi-valued keys in $table into single-valued keys. This function takes duplicate table entries and flattens them into a single entry. The flattening behavior is controlled by the (mandatory) $flags argument.

When $flags == APR::Const::OVERLAP_TABLES_SET, each key will be set to the last value seen for that key. For example, given key/value pairs 'foo => bar' and 'foo => baz', 'foo' would have a final value of 'baz' after compression -- the 'bar' value would be lost.

When $flags == APR::Const::OVERLAP_TABLES_MERGE, multiple values for the same key are flattened into a comma-separated list. Given key/value pairs 'foo => bar' and 'foo => baz', 'foo' would have a final value of 'bar, baz' after compression.

Access the constants via:

  use APR::Const -compile qw(:table);

or an explicit:

  use APR::Const -compile qw(OVERLAP_TABLES_SET OVERLAP_TABLES_MERGE);

compress() combined with overlay() does the same thing as overlap().

Examples:



TOP

copy

Create a new table and copy another table into it.

  $table_copy = $table->copy($p);


TOP

do

Iterate over all the elements of the table, invoking provided subroutine for each element. The subroutine gets passed as argument, a key-value pair.

  $table->do(sub {...}, @filter);

Examples:



TOP

get

Get the value(s) associated with a given key. After this call, the data is still in the table.

  $val = $table->get($key);
  @val = $table->get($key);


TOP

make

Make a new table.

  $table = APR::Table::make($p, $nelts);

This table can only store text data.



TOP

merge

Add data to a table by merging the value with data that has already been stored using ", " as a separator:

  $table->merge($key, $val);

If the key is not found, then this function acts like add().

If there is more than one value for the same key, only the first (the oldest) value gets merged.

Examples:



TOP

overlap

For each key/value pair in $table_b, add the data to $table_a. The definition of $flags explains how $flags define the overlapping method.

  $table_a->overlap($table_b, $flags);

Access the constants via:

  use APR::Const -compile qw(:table);

or an explicit:

  use APR::Const -compile qw(OVERLAP_TABLES_SET OVERLAP_TABLES_MERGE);

This function is highly optimized, and uses less memory and CPU cycles than a function that just loops through table $table_b calling other functions.

Conceptually, overlap() does this:

  apr_array_header_t *barr = apr_table_elts(b);
  apr_table_entry_t *belt = (apr_table_entry_t *)barr-E<gt>elts;
  int i;
  
  for (i = 0; i < barr->nelts; ++i) {
      if (flags & APR_OVERLAP_TABLES_MERGE) {
          apr_table_mergen(a, belt[i].key, belt[i].val);
      }
      else {
          apr_table_setn(a, belt[i].key, belt[i].val);
      }
  }

Except that it is more efficient (less space and cpu-time) especially when $table_b has many elements.

Notice the assumptions on the keys and values in $table_b -- they must be in an ancestor of $table_a's pool. In practice $table_b and $table_a are usually from the same pool.

Examples:



TOP

overlay

Merge two tables into one new table. The resulting table may have more than one value for the same key.

  $table = $table_base->overlay($table_overlay, $p);

Examples:



TOP

set

Add a key/value pair to a table, if another element already exists with the same key, this will over-write the old data.

  $table->set($key, $val);

When adding data, this function makes a copy of both the key and the value.



TOP

unset

Remove data from the table.

  $table->unset($key);


TOP

TIE Interface

APR::Table also implements a tied interface, so you can work with the $table object as a hash reference.

The following tied-hash function are supported: FETCH, STORE, DELETE, CLEAR, EXISTS, FIRSTKEY, NEXTKEY and DESTROY.

Note regarding the use of values(). APR::Table can hold more than one key-value pair sharing the same key, so when using a table through the tied interface, the first entry found with the right key will be used, completely disregarding possible other entries with the same key. With Perl 5.8.0 and higher values() will correctly list values the corresponding to the list generated by keys(). That doesn't work with Perl 5.6. Therefore to portably iterate over the key-value pairs, use each() (which fully supports multivalued keys), or APR::Table::do.



TOP

EXISTS

  $ret = $table->EXISTS($key);


TOP

CLEAR

  $table->CLEAR();


TOP

STORE

  $table->STORE($key, $val);


TOP

DELETE

  $table->DELETE($key);


TOP

FETCH

  $ret = $table->FETCH($key);

When iterating through the table's entries with each(), FETCH will return the current value of a multivalued key. For example:

  $table->add("a" => 1);
  $table->add("b" => 2);
  $table->add("a" => 3);
  
  ($k, $v) = each %$table; # (a, 1)
  print $table->{a};       # prints 1
  
  ($k, $v) = each %$table; # (b, 2)
  print $table->{a};       # prints 1
  
  ($k, $v) = each %$table; # (a, 3)
  print $table->{a};       # prints 3 !!!
  
  ($k, $v) = each %$table; # (undef, undef)
  print $table->{a};       # prints 1


TOP

See Also

mod_perl 2.0 documentation.



TOP

Copyright

mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.



TOP

Authors

The mod_perl development team and numerous contributors.






TOP
previous page: APR::String - Perl API for manipulating APR UUIDspage up: mod_perl 2.0 APInext page: APR::ThreadMutex - Perl API for APR thread mutexes