#this example uses the library Net::LDAP -- use CPAN to install
use Net::LDAP;

# connect to the ldap server
$ldap = Net::LDAP->new ( "ldap.service.uci.edu" ) or die "$@";

# do an anonymous bind
$mesg = $ldap->bind ( version => 3 );          # use for searches

# list attributes to be queries
my @Attrs = ( );               # request all available attributes
                                # to be returned.
# do search, see sub below
my $result = LDAPsearch ( $ldap, "campusid=000000137118", \@Attrs );

# get entries from result object
my @entries = $result->entries;

# step through entries and print
my $entr;
foreach $entr ( @entries ) {
   print "DN: ", $entr->dn, "\n";

   my $attr;
   foreach $attr ( sort $entr->attributes ) {
     # skip binary we can't handle
     next if ( $attr =~ /;binary$/ );
     print "  $attr : ", $entr->get_value ( $attr ) ,"\n";
   }

   print "#-------------------------------\n";
}

# unbind (disconnect) from server
$ldap->unbind;

sub LDAPsearch
{
   my ($ldap,$searchString,$attrs,$base) = @_;

   # if they don't pass a base... set it for them

   if (!$base ) { $base = "ou=University of California Irvine,o=University of California,C=US"; }

   # if they don't pass an array of attributes...
   # set up something for them

   if (!$attrs ) { $attrs = [ 'cn','type' ]; }

   my $result = $ldap->search ( base    => "$base",
                                scope   => "sub",
                                filter  => "$searchString",
                                attrs   =>  $attrs
                              );
}