docs::a...R::Table(3)

πŸ“¦ version 2.0.13, in nixos 26.05, nixpkgs unstable

πŸ’– NAME πŸ’–

APR::Table β€” Perl API for manipulating APR opaque string-content tables.

This is the docs::a...R::Table(3) page, bestie β€” a User Contributed Perl Documentation moment, and it's all about <rainbow>opaque string-content tables</rainbow> 🫢.


✨ 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";
}

πŸ¦‹ Description πŸ¦‹

Okay so APR::Table lets you manipulate opaque string-content tables. That's the whole assignment, and it ate πŸ’….

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

On the Perl level, that means 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. Periodt. ✨

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, heads up: as of the current implementation, if more than one 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.


πŸ’… API πŸ’…

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

πŸŽ€ add πŸŽ€

Add data to a table, regardless of whether there is another element with the same key. It's giving "just squeeze it in" energy πŸ“.

$table->add($key, $val);
  • obj: $table ("APR::Table object") β€” the table to add to.
  • arg1: $key (string) β€” the key to use.
  • arg2: $val (string) β€” the value to add.
  • ret: no return value.
  • since: 2.0.00.

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

🌸 clear 🌸

Delete all of the elements from a table. Poof, gone, clean slate 🧚.

$table->clear();
  • obj: $table ("APR::Table object") β€” the table to clear.
  • ret: no return value.
  • since: 2.0.00.

⭐ compress ⭐

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

$table->compress($flags);
  • obj: $table ("APR::Table object") β€” the table to compress.
  • arg1: $flags ("APR::Const constant")
    • APR::Const::OVERLAP_TABLES_MERGE β€” to merge
    • APR::Const::OVERLAP_TABLES_SET β€” to overwrite
  • ret: no return value.
  • since: 2.0.00.

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:

  • APR::Const::OVERLAP_TABLES_SET β€” start with table $table:
foo => "one"
foo => "two"
foo => "three"
bar => "beer"

which is done by:

use APR::Const    -compile => ':table';
my $table = APR::Table::make($r->pool, TABLE_SIZE);

$table->set(bar => 'beer');
$table->set(foo => 'one');
$table->add(foo => 'two');
$table->add(foo => 'three');

Now compress it using APR::Const::OVERLAP_TABLES_SET:

$table->compress(APR::Const::OVERLAP_TABLES_SET);

Now table $table contains:

foo => "three"
bar => "beer"

The value three for the key foo, that was added last, took over the other values.

  • APR::Const::OVERLAP_TABLES_MERGE β€” start with table $table:
foo => "one"
foo => "two"
foo => "three"
bar => "beer"

as in the previous example, now compress it using APR::Const::OVERLAP_TABLES_MERGE:

$table->compress(APR::Const::OVERLAP_TABLES_MERGE);

Now table $table contains:

foo => "one, two, three"
bar => "beer"

All the values for the same key were merged into one value.

πŸ“ copy πŸ“

Create a new table and copy another table into it. Dupe it, but make it official πŸ’–.

$table_copy = $table->copy($p);
  • obj: $table ("APR::Table object") β€” the table to copy.
  • arg1: $p ("APR::Pool object") β€” the pool to allocate the new table out of.
  • ret: $table_copy ("APR::Table object") β€” a copy of the table passed in.
  • since: 2.0.00.

🧚 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);
  • obj: $table ("APR::Table object") β€” the table to operate on.
  • arg1: $sub (CODE ref/string) β€” a subroutine reference or name to be called on each item in the table. The subroutine can abort the iteration by returning 0 and should always return 1 otherwise.
  • opt arg3: @filter (ARRAY) β€” if passed, only keys matching one of the entries in f@filter will be processed.
  • ret: no return value.
  • since: 2.0.00.

Examples:

  • This filter simply prints out the key/value pairs and counts how many pairs did it see.
use constant TABLE_SIZE => 20;
our $filter_count;
my $table = APR::Table::make($r->pool, TABLE_SIZE);

# populate the table with ascii data
for (1..TABLE_SIZE) {
    $table->set(chr($_+97), $_);
}

$filter_count = 0;
$table->do("my_filter");
print "Counted $filter_count elements";

sub my_filter {
    my ($key, $value) = @_;
    warn "$key => $value\n";
    $filter_count++;
    return 1;
}

Notice that my_filter always returns 1, ensuring that do() will pass all the key/value pairs.

  • This filter is similar to the one from the previous example, but this time it decides to abort the filtering after seeing half of the table, by returning 0 when this happens.
sub my_filter {
    my ($key, $value) = @_;
    $filter_count++;
    return $filter_count == int(TABLE_SIZE)/2 ? 0 : 1;
}

πŸ’œ get πŸ’œ

Get the value(s) associated with a given key. After this call, the data is still in the table. No take-backs, it's still there 🫢.

$val = $table->get($key);
@val = $table->get($key);
  • obj: $table ("APR::Table object") β€” the table to search for the key.
  • arg1: $key (string) β€” the key to search for.
  • ret: $val or @val
    • In scalar context, the first matching value is returned (the oldest in the table, if there is more than one value). If nothing matches, undef is returned.
    • In list context, the whole table is traversed and all matching values are returned. An empty list is returned if nothing matches.
  • since: 2.0.00.

πŸ‘‘ make πŸ‘‘

Make a new table. Fresh table, main character energy πŸ‘‘.

$table = APR::Table::make($p, $nelts);
  • obj: $p ("APR::Pool object") β€” the pool to allocate the pool out of.
  • arg1: $nelts (integer) β€” the number of elements in the initial table. At least 1 or more. If 0 is passed APR will still allocate 1.
  • ret: $table ("APR::Table object") β€” the new table.
  • since: 2.0.00.

This table can only store text data.

🌈 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);
  • obj: $table ("APR::Table object") β€” the table to search for the data.
  • arg1: $key (string) β€” the key to merge data for.
  • arg2: $val (string) β€” the data to add.
  • ret: no return value.
  • since: 2.0.00.

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:

  • Start with a pair:
merge => "1"

and merge "a" to the value:

$table->set(  merge => '1');
$table->merge(merge => 'a');
$val = $table->get('merge');

Result:

$val == "1, a";
  • Start with a multivalued pair:
merge => "1"
merge => "2"

and merge "a" to the first value;

$table->set(  merge => '1');
$table->add(  merge => '2');
$table->merge(merge => 'a');
@val = $table->get('merge');

Result:

$val[0] == "1, a";
$val[1] == "2";

Only the first value for the same key is affected.

  • Have no entry and merge "a";
$table->merge(miss => 'a');
$val = $table->get('miss');

Result:

$val == "a";

πŸ”₯ 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);
  • obj: $table_a ("APR::Table object") β€” the table to add the data to.
  • arg1: $table_b ("APR::Table object") β€” the table to iterate over, adding its data to table $table_a.
  • arg2: $flags (integer) β€” how to add the table to table $table_a.

When $flags == "APR::Const::OVERLAP_TABLES_SET", if another element already exists with the same key, this will over-write the old data.

  • APR::Const::OVERLAP_TABLES_MERGE β€” when $flags == "APR::Const::OVERLAP_TABLES_MERGE", the key/value pair from $table_b gets added no matter what, even if $table_a already has another element with that same key. No deduping, no drama, it just stacks them πŸ’…

ret: no return value since: 2.0.00

Grab those constants like this:

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

or be extra and name them:

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

Okay so bestie, here's the thing β€” this function is highly optimized, and it uses less memory and fewer CPU cycles than a function that just loops through table $table_b calling other functions. She's efficient, she's booked, she's not doing the slow thing 😌

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 β€” and this is the whole point β€” it's more efficient (less space and cpu-time), especially when $table_b has many elements. Like, that's the flex πŸ”₯

Heads up about 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, so you're probably fine, but don't come crying to me if you mix pools πŸ™ˆ

Examples:

  • "APR::Const::OVERLAP_TABLES_SET"

Start with table $base:

foo => "one"
foo => "two"
bar => "beer"

and table $add:

foo => "three"

which is done by:

use APR::Const    -compile => ':table';
my $base = APR::Table::make($r->pool, TABLE_SIZE);
my $add  = APR::Table::make($r->pool, TABLE_SIZE);

$base->set(bar => 'beer');
$base->set(foo => 'one');
$base->add(foo => 'two');

$add->set(foo => 'three');

Now overlap using "APR::Const::OVERLAP_TABLES_SET":

$base->overlap($add, APR::Const::OVERLAP_TABLES_SET);

Now table $add is unmodified and table $base contains:

foo => "three"
bar => "beer"

The value from table "add" has overwritten all previous values for the same key both had (foo). This is the same as doing overlay() followed by compress() with "APR::Const::OVERLAP_TABLES_SET". Overwrite era πŸ’€

  • "APR::Const::OVERLAP_TABLES_MERGE"

Start with table $base:

foo => "one"
foo => "two"

and table $add:

foo => "three"
bar => "beer"

which is done by:

use APR::Const    -compile => ':table';
my $base = APR::Table::make($r->pool, TABLE_SIZE);
my $add  = APR::Table::make($r->pool, TABLE_SIZE);

$base->set(foo => 'one');
$base->add(foo => 'two');

$add->set(foo => 'three');
$add->set(bar => 'beer');

Now overlap using "APR::Const::OVERLAP_TABLES_MERGE":

$base->overlap($add, APR::Const::OVERLAP_TABLES_MERGE);

Now table $add is unmodified and table $base contains:

foo => "one, two, three"
bar => "beer"

Values from both tables for the same key were merged into one value. This is the same as doing overlay() followed by compress() with "APR::Const::OVERLAP_TABLES_MERGE". Friendship merger, no cap 🫢

✨ "overlay" ✨

Merge two tables into one new table. The resulting table may have more than one value for the same key. She's generative, she makes new things, she does not touch the originals πŸ’–

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

obj: $table_base ( "APR::Table object" ) The table to add at the end of the new table.

arg1: $table_overlay ( "APR::Table object" ) The first table to put in the new table.

arg2: $p ( "APR::Pool object" ) The pool to use for the new table.

ret: $table ( "APR::Table object" ) A new table containing all of the data from the two passed in.

since: 2.0.00

Examples:

  • Start with table $base:
foo => "one"
foo => "two"
bar => "beer"

and table $add:

foo => "three"

which is done by:

use APR::Const    -compile => ':table';
my $base = APR::Table::make($r->pool, TABLE_SIZE);
my $add  = APR::Table::make($r->pool, TABLE_SIZE);

$base->set(bar => 'beer');
$base->set(foo => 'one');
$base->add(foo => 'two');

$add->set(foo => 'three');

Now overlay using "APR::Const::OVERLAP_TABLES_SET":

my $overlay = $base->overlay($add, APR::Const::OVERLAP_TABLES_SET);

That resulted in a new table $overlay (tables "add" and $base are unmodified) which contains:

foo => "one"
foo => "two"
foo => "three"
bar => "beer"

Three foos living their best lives, iconic 🌈

πŸŽ€ "set" πŸŽ€

Add a key/value pair to a table β€” but if another element already exists with the same key, this will over-write the old data. Polite but firm, she takes the spot πŸ‘‘

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

obj: $table ( "APR::Table object" ) The table to add the data to.

arg1: $key ( string ) The key to use.

arg2: $val ( string ) The value to add.

ret: no return value since: 2.0.00

When adding data, this function makes a copy of both the key and the value. Not a reference, a whole copy, don't @ me πŸ’…

πŸ¦‹ "unset" πŸ¦‹

Remove data from the table. Bye bestie πŸ‘‹

$table->unset($key);

obj: $table ( "APR::Table object" ) The table to remove data from.

arg1: $key ( string ) The key of the data being removed.

ret: no return value since: 2.0.00


🌸 TIE Interface 🌸

"APR::Table" also implements a tied interface, so you can work with the $table object as a hash reference. Yes literally just %$table, go off πŸ”₯

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".

Like, this is the whole multivalue situation, and each() is the main character here. values() in 5.6 is not invited to the function 😭

⭐ "EXISTS" ⭐

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

obj: $table ( "APR::Table object" ) arg1: $key ( string ) ret: $ret ( integer ) true or false

since: 2.0.00

πŸ’œ "CLEAR" πŸ’œ

$table->CLEAR();

obj: $table ( "APR::Table object" ) ret: no return value since: 2.0.00

✨ "STORE" ✨

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

obj: $table ( "APR::Table object" ) arg1: $key ( string ) arg2: $val ( string ) ret: no return value since: 2.0.00

πŸŽ€ "DELETE" πŸŽ€

$table->DELETE($key);

obj: $table ( "APR::Table object" ) arg1: $key ( string ) ret: no return value since: 2.0.00

πŸ“ "FETCH" πŸ“

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

obj: $table ( "APR::Table object" ) arg1: $key ( string ) ret: $ret ( string ) since: 2.0.00

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

See that !!!? That's her tracking the iterator, and it's giving main character energy β€” FETCH follows the cursor, not the key. Then when the iterator runs out she resets back to 1. The way that works is genuinely so satisfying ngl 😌


🌈 See Also 🌈

mod_perl 2.0 documentation. That's the whole list, but it's the good one, iykyk.


mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0. Legal bestie said what she said πŸ’…


🫢 Authors 🫢

The mod_perl development team and numerous contributors. Legends, every single one of them.


And that's the page, bestie β€” you made it to the end πŸŽ‰ This was perl v5.42.3, dated 2023-10-21, and honestly? She ate. Go forth and overlap your tables responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from