Taisuke Yamada


 Tie::LDAP - Tie LDAP database to Perl hash.


 use Tie::LDAP;

 tie %LDAP, 'Tie::LDAP', {
     host => $host, # LDAP hostname (defaults to
     port => $port, # Port number (defaults to 389)
     user => $user, # Full DN used to access LDAP database
     pass => $pass, # Password used with above DN
     base => $base, # Base DN used for each/keys/values operation


This library allows you to tie LDAP database to Perl hash. Once tied, all hash operation will cause corresponding LDAP operation, as you would (probably) expect.

Referencing tied hash will return hash reference to named LDAP entry that holds lowercased attribute as hash key, and reference to ARRAY containing data as hash value.

Storing data is as easy as fetching: just push hash reference - with the same structure as fetched hash - back in.

Also, fetching/storing data into fetched hash reference will work as expected - it will manipulate corresponding field in fetched LDAP entry.


Here's a brief example of how you can use this module:

  use Tie::LDAP;

  ## connect
  tie %LDAP, 'Tie::LDAP', { base => 'o=IMASY, c=JP' };

  ## lookup entry for [dn: cn=tai, o=IMASY, c=JP]
  $info = $LDAP{q{cn=tai, o=IMASY, c=JP}};

  ## lookup each attributes
  $user = $info->{username}->[0];
  $mail = @{$info->{mailaddr}};

  ## update each attributes
  $info->{username} = ['newname'];
  $info->{mailaddr} = ['tai@imasy.or.jp', 'tyamada@tk.elec.waseda.ac.jp'];

  ## update entry
  $LDAP{q{cn=tai, o=IMASY, c=JP}} = {
    username => ['newname'],
    mailaddr => ['tai@imasy.or.jp', 'tyamada@tk.elec.waseda.ac.jp'],

  ## dump database (under base DN of [o=IMASY, c=JP]) in LDIF style
  while (my($dn, $hash) = each %LDAP) {
    print "dn: $dn\n";
    while (my($name, $list) = each %{$hash}) {
      foreach (@{$list}) {
        print "$name: $_\n";
    print "\n";

  ## disconnect
  untie %LDAP;


Doing each/keys/values operation to tied hash works (as shown in example), but could be _very_ slow, depending on the size of the database. This is because all operation is done synchronously.

Also, though this is not a bug, substituting empty array to tied hash will cause whole database to be cleared out.


Copyright 1998-2000, T. Yamada <tai@imasy.or.jp>. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.