TQuid has asked for the wisdom of the Perl Monks concerning the following question:

I have some new code that searches my company's LDAP directory, returning the old classics of name, phone number, email, and so on. The problem is, if an attribute can have more than one value, I can only seem to get at one of them. How do I access the 2nd, 3rd, etc. values in an attribute if there's more than one? Is there a way to tell that there *is* more than one value in a given attribute? Here's a code snippet--again, this works, but only grabs the first value . . .
my @entries = $mesg->entries; if ( $layout eq 'tel' ) { my @attributes = qw(cn uid pager telephoneNumber); my $format = "%-20s %-12s %-15s %s\n"; printf $format, qw(Name E-Mail Pager Phone); print '-' x 75, "\n"; foreach my $entry ( @entries ) { my @line; foreach my $attribute ( @attributes ) { push @line, ( $entry->get_value($attribute) || 'none' ); } printf $format, @line; } }
Multiple calls to $entry->get_value('telephoneNumber'), for example, don't get successive values. I'd be most grateful for any help that anyone may be able to give.

--TQuid

Replies are listed 'Best First'.
Re: Multiple values per attribute from a Net::LDAP query?
by merlyn (Sage) on Nov 27, 2000 at 22:29 UTC
    According to perldoc Net::LDAP::Entry:
    get_value ( ATTR [, OPTIONS ] ) Get the values for the attribute ATTR. In a list con- text returns all values for the given attribute, or the empty list if the attribute does not exist. In a scalar context returns the first value for the attribute or undef if the attribute does not exist.
    You're calling get_value in a scalar context! Stop that.

    You're also calling printf with a variable number of arguments. That won't work very well until you change the format string.

    -- Randal L. Schwartz, Perl hacker

Re: Multiple values per attribute from a Net::LDAP query?
by TQuid (Sexton) on Nov 27, 2000 at 22:29 UTC
    Well, upon a helpful suggestion, I actually rtfm'ed, and here we are (from perldoc Net::LDAP::Entry):
    get_value ( ATTR [, OPTIONS ] ) Get the values for the attribute ATTR. In a list context returns all values for the given attribute, or the empty list if the attribute does not exist. In a scalar context returns the first value for the attribute or undef if the attribute does not exist. The return value may be changed by OPTIONS, which is a list of name => value pairs, valid options are :-
    Unfortunately, there's little to do but read everything that can hold more than one value into an array. Ack. If anyone knows an elegant way to determine whether an attribute *can* hold multiple values (it looks as if they all may, at least potentially), I'm all ears.

    --TQuid

Re: Multiple values per attribute from a Net::LDAP query?
by wardk (Deacon) on Nov 27, 2000 at 22:28 UTC

    sorta answered this in the chatterbox, came back and no replies yet...so here goes...

    try putting the get_value results in a array.

    @vals = $entry->get_value($attribute)

    then process the array.

Re: Multiple values per attribute from a Net::LDAP query?
by noname (Sexton) on Aug 14, 2015 at 07:25 UTC
    Hi, an alternative would be to get the whole structure as a hash. I do the following:
    my %href; my ($uid) = @_; $mesg = $ldap->search( # perform a search base => "ou=People,dc=male", filter => "(&(objectClass=posixAccount)(uid=$uid))", ); $mesg->code && die $mesg->error; foreach my $entry ($mesg->entries) { foreach my $attr ($entry->attributes) { $href{"$attr"} = $entry->get_value($attr, asref => 1); } } return \%href
    note "asref=>1" as get_value() option. This way I get all values for an attribute, like I get multiple objeckClasses for one objectClass attribute. Having the whole ldap structure is nice, but getting the values out of hash is a bit tricky, use Data::Dumper to see the real picture. This is how I get the values from the hash for an attribute:
    sub getattrval { my ($hashref,$attr) = @_; if ( %$ld{$attr} ) { my $tmp_href = %$ld{$attr}; # return comma delimited string instead of array return join (",",@$tmp_href) if scalar @$tmp_href > 1; return @$tmp_href[0]; } return undef; }