in reply to net::ldap ->singel valued attributes and multivalued attributes
Within this hash, attribute values are always an array, i.e. single valued attributes are returned in an array containing a single value. ... this would require to know, which attribute is single valued and which is multivalued. Is there a way to figure this out?
I'm not an LDAP expert, but are you referring to attributes like objectclass in the following example? Going with that assumption, in this example, the way to check is to de-reference the array reference with @{...} (perlreftut and perlref), and use the array in scalar context to get the number of elements it contains, e.g. if ( @array > 1 ) { print "More than one element in the array.\n" }. You can walk the data structure as returned by as_struct, or, probably better, you can walk the Net::LDAP::Entry objects returned by Net::LDAP::Search. I've shown examples of both in the following.
use warnings; use strict; use Data::Dump; use Net::LDAP; my $ldap = Net::LDAP->new('ldap.forumsys.com') or die "$@"; my $mesg = $ldap->bind( 'cn=read-only-admin,dc=example,dc=com', password => 'password' ); $mesg->code and die $mesg->error; my $srch = $ldap->search( base => "dc=example,dc=com", filter => "(uid=test)" ); $srch->code and die $srch->error; # walk with perl my $struct = $srch->as_struct; dd $struct; for my $entryname (sort keys %$struct) { my $entry = $struct->{$entryname}; for my $attr (sort keys %$entry) { print "$attr is ", @{$entry->{$attr}}>1 ? "multi" : "single", "-valued\n"; } } print "---\n"; # walk with Net::LDAP for my $entry ($srch->entries) { for my $attr ( $entry->attributes ) { my @values = $entry->get_value($attr); print "$attr is ", @values>1 ?"multi" : "single","-valued\n"; } } $mesg = $ldap->unbind; $mesg->code and die $mesg->error; __END__ { "uid=test,dc=example,dc=com" => { cn => ["Test"], displayname => ["Test"], gidnumber => [0], givenname => ["Test"], homedirectory => ["home"], initials => ["TS"], o => ["Company"], objectclass => ["posixAccount", "top", "inetOrgPerson"], sn => ["Test"], uid => ["test"], uidnumber => [24601], }, } cn is single-valued displayname is single-valued gidnumber is single-valued givenname is single-valued homedirectory is single-valued initials is single-valued o is single-valued objectclass is multi-valued sn is single-valued uid is single-valued uidnumber is single-valued --- objectClass is multi-valued gidNumber is single-valued givenName is single-valued sn is single-valued displayName is single-valued uid is single-valued initials is single-valued homeDirectory is single-valued cn is single-valued uidNumber is single-valued o is single-valued
|
|---|