Net::LDAP::Express - Simplified interface for Net::LDAP


Net::LDAP::Express - Simplified interface for Net::LDAP


  use Net::LDAP::Express;
  eval {
    my $ldap =
      Net::LDAP::Express->new(host => 'localhost',
                              bindDN => 'cn=admin,ou=People,dc=me',
                              bindpw => 'secret',
                              base   => 'ou=People,dc=me',
                              searchattrs => [qw(cn uid loginname)],
                              %parms) ; # params for Net::LDAP::new
  } ;
  if ($@) {
    die "Can't connect to ldap server: $@" ;
  my $filter = '(|(loginname=~bronto)(|(cn=~bronto)(uid=~bronto)))' ;
  my $entries ;
  # These all return the same array of Net::LDAP::Entry objects
  $entries = $ldap->search(filter => $filter) ; # uses new()'s base
  $entries = $ldap->search(base   => 'ou=People,dc=me',
                           filter => $filter) ;
  $entries = $ldap->simplesearch('bronto') ; # uses new()'s searchattrs
  # Now elaborate results:
  foreach my $entry (@$entries) {
    modify_something_in_this($entry) ;
  # You often want to update a set of entries
  foreach my $entry (@$entries) {
    die "Error updating entry" unless defined $ldap->update($entry) ;
  # but I think you'll prefer this way:
  my $result = $ldap->update(@$entries) ;
  unless (@$result == @$entries) {
    print "Error updating entries: ",$ldap->error,
          "; code ",$ldap->errcode,".\n\n" ;
  # Add an entry, or an array of them, works as above:
  die $ldap->error unless $ldap->add_many(@some_other_entries) ;
  # rename an entry: sometimes you simply want to change a name
  # and nothing else...
  $ldap->rename($entry,$newrdn) ;
  # Ask for just a few attributes, sort results
  $ldap = Net::LDAP::Express->new(host        => $server,
                                  port        => $port,
                                  base        => $base,
                                  bindDN      => $binddn,
                                  bindpw      => $bindpw,
                                  onlyattrs   => \@only,
                                  sort_by     => \@sortby,
                                  searchattrs => \@search) ;
  my $entries = $ldap->simplesearch('person') ;


Net::LDAP::Express is an alternative interface to the fantastic Graham Barr's Net::LDAP, that simplifies the tasks of adding and deleting multiple entries, renaming them, or searching entries residing in a common subtree.

Net::LDAP is a great module for working with directory servers, but it's a bit overkill when you want to do simple short scripts or have big programs that always do the same job again and again, say: open an authenticated connection to a directory server, search entries against the same attributes each time and in the same way (e.g.: approx search against the three attributes cn, uid and loginname). With Net::LDAP this would mean:

With Net::LDAP::Express this is done with:


Creates a Net::LDAP::Express object. Accepts all the parameters that are legal to Net::LDAP::new but the directory server name/address is specified via the host parameter. Specific Net::LDAP::Express parameters are therefore:
the name or IP address of the directory server we are connecting to. Mandatory.

bind DN in case of authenticated bind

bind password in case of authenticated bind

base subtree for searches. Mandatory.

attributes to use for simple searches (see the simplesearch method);

boolean operator in case that more than one attribute is specified with searchattrs; default is '|' (boolean or); allowed boolean operators are | and &.

By default, an 'approx' search is performed by simplesearch(); for those directory servers that doesn't support the ~= operator it is possible to request a substring search specifying the value 'substr' for the searchmatch parameter.

A list of attributes that should be returned in addition of the default ones.

At the opposite of searchextras: if you need just a few attributes to be returned for each entry, you can specify them here. Note that it doesn't make much sense to include both searchextras and onlyattrs.

If you specify this parameter with a list of attributes, the simplesearch method will return the entries sorted by the attributes given. Note that if you also specify onlyattrs and there are attributes in sort_by that are not in onlyattrs, they will be added to allow the Net::LDAP::Search::sorted method to work.


All Net::LDAP methods are supported via inheritance. Method specific in Net::LDAP::Express or that override inherited methods are documented below.

search works exactly as Net::LDAP::search(), with a few changes:

search() returns a Net::LDAP::Search object, thus mantaining an almost complete compatibility with the parent class interface.


Takes one or more Net::LDAP::Entry objects, returns a reference to an array of Net::LDAP::Entry objects that successfully made it on the directory server. You can check if every entry has been added by comparing the length of the input list against the length of the output list. Use the error and/or errorcode methods to see what went wrong.

Works the same way as add_many, but it deletes entries instead :-)

Renames an entry; $entry can be a Net::LDAP::Entry or a DN, $newrdn is a new value for the RDN. Returns $entry for success, undef on failure.

update takes a list of Net::LDAP::Entry objects as arguments and commits changes on the directory server. Returns a reference to an array of updated entries.

Searches entries using the new()'s search* and base parameters. Takes a search string as argument. Returns a list of entries on success, undef on error.

Returns last error's name

Returns last error's code


Marco Marongiu, <>


the Net::LDAP manpage.

 Net::LDAP::Express - Simplified interface for Net::LDAP