Cari di Perl 
    Perl Tutorial
Daftar Isi
(Sebelumnya) Perl TiePerl interprocess communicatio ... (Berikutnya)
Language Reference

Perl DBM Filters

Daftar Isi

NAME

perldbmfilter - Perl DBM Filters

SYNOPSIS

  1. $db = tie %hash, 'DBM', ...
  2. $old_filter = $db->filter_store_key ( sub { ... } );
  3. $old_filter = $db->filter_store_value( sub { ... } );
  4. $old_filter = $db->filter_fetch_key ( sub { ... } );
  5. $old_filter = $db->filter_fetch_value( sub { ... } );

DESCRIPTION

The four filter_* methods shown above are available in all the DBMmodules that ship with Perl, namely DB_File, GDBM_File, NDBM_File,ODBM_File and SDBM_File.

Each of the methods works identically, and is used to install (oruninstall) a single DBM Filter. The only difference between them is theplace that the filter is installed.

To summarise:

  • filter_store_key

    If a filter has been installed with this method, it will be invokedevery time you write a key to a DBM database.

  • filter_store_value

    If a filter has been installed with this method, it will be invokedevery time you write a value to a DBM database.

  • filter_fetch_key

    If a filter has been installed with this method, it will be invokedevery time you read a key from a DBM database.

  • filter_fetch_value

    If a filter has been installed with this method, it will be invokedevery time you read a value from a DBM database.

You can use any combination of the methods from none to all four.

All filter methods return the existing filter, if present, or undefif not.

To delete a filter pass undef to it.

The Filter

When each filter is called by Perl, a local copy of $_ will containthe key or value to be filtered. Filtering is achieved by modifyingthe contents of $_. The return code from the filter is ignored.

An Example: the NULL termination problem.

DBM Filters are useful for a class of problems where you alwayswant to make the same transformation to all keys, all values or both.

For example, consider the following scenario. You have a DBM databasethat you need to share with a third-party C application. The C applicationassumes that all keys and values are NULL terminated. Unfortunatelywhen Perl writes to DBM databases it doesn't use NULL termination, soyour Perl application will have to manage NULL termination itself. Whenyou write to the database you will have to use something like this:

  1. $hash{"$key\0"} = "$value\0";

Similarly the NULL needs to be taken into account when you are consideringthe length of existing keys/values.

It would be much better if you could ignore the NULL terminations issuein the main application code and have a mechanism that automaticallyadded the terminating NULL to all keys and values whenever you write tothe database and have them removed when you read from the database. As I'msure you have already guessed, this is a problem that DBM Filters canfix very easily.

  1. use strict;
  2. use warnings;
  3. use SDBM_File;
  4. use Fcntl;
  5. my %hash;
  6. my $filename = "filt";
  7. unlink $filename;
  8. my $db = tie(%hash, 'SDBM_File', $filename, O_RDWR|O_CREAT, 0640)
  9. or die "Cannot open $filename: $!\n";
  10. # Install DBM Filters
  11. $db->filter_fetch_key ( sub { s/\0$// } );
  12. $db->filter_store_key ( sub { $_ .= "\0" } );
  13. $db->filter_fetch_value(
  14. sub { no warnings 'uninitialized'; s/\0$// } );
  15. $db->filter_store_value( sub { $_ .= "\0" } );
  16. $hash{"abc"} = "def";
  17. my $a = $hash{"ABC"};
  18. # ...
  19. undef $db;
  20. untie %hash;

The code above uses SDBM_File, but it will work with any of the DBMmodules.

Hopefully the contents of each of the filters should beself-explanatory. Both "fetch" filters remove the terminating NULL,and both "store" filters add a terminating NULL.

Another Example: Key is a C int.

Here is another real-life example. By default, whenever Perl writes toa DBM database it always writes the key and value as strings. So whenyou use this:

  1. $hash{12345} = "something";

the key 12345 will get stored in the DBM database as the 5 byte string"12345". If you actually want the key to be stored in the DBM databaseas a C int, you will have to use pack when writing, and unpackwhen reading.

Here is a DBM Filter that does it:

  1. use strict;
  2. use warnings;
  3. use DB_File;
  4. my %hash;
  5. my $filename = "filt";
  6. unlink $filename;
  7. my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH
  8. or die "Cannot open $filename: $!\n";
  9. $db->filter_fetch_key ( sub { $_ = unpack("i", $_) } );
  10. $db->filter_store_key ( sub { $_ = pack ("i", $_) } );
  11. $hash{123} = "def";
  12. # ...
  13. undef $db;
  14. untie %hash;

The code above uses DB_File, but again it will work with any of theDBM modules.

This time only two filters have been used; we only need to manipulatethe contents of the key, so it wasn't necessary to install any valuefilters.

SEE ALSO

DB_File, GDBM_File, NDBM_File, ODBM_File and SDBM_File.

AUTHOR

Paul Marquess

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Perl TiePerl interprocess communicatio ... (Berikutnya)