J.I. the parent while loop/for loop is not the reason the second version
works and the first does not.  The solution is in the line:
print "$_\n" for $entry->get_value( "$_[1]" );  # this will work in
either loop
get_value returns an array in a list context or the first attribute in a
scalar context.  Read the documentation on Net::LDAP::Entry.

It might make more sense to you if you use:
my @attr_values = $entry->get_value($_[1]);

Don

-----Original Message-----
From: J.I. Asenjo [mailto:[EMAIL PROTECTED] 
Sent: Thursday, December 20, 2007 1:41 PM
To: perl-ldap@perl.org
Subject: stupid problem with Net::LDAP

hi,

I have this snippet:


#!/usr/bin/perl

use strict;
use warnings;

my $ldapserver = "127.0.0.1";
my $basedn = "dc=asenjo,dc=nx";

search("car","mail");
sub search {
use Net::LDAP;
my $ldap = Net::LDAP->new($ldapserver) or die "$0";
# at home I do not need binding, otherwise here bind
my $msg = $ldap->search (base => $basedn,
                         filter => "(|(cn=*$_[0]*)(uid=*$_[0]*))",
                         attrs => ["$_[1]",'cn']);

while (my $entry = $msg->shift_entry){
        my $attr = $entry->get_value("$_[1]");
        my $cn = $entry->get_value("cn");
        next unless (defined($entry->get_value("$_[1]")));
        print "$cn\t$attr\n";
}
}
        
and it works a little bit. It does find addresses, but not all of them.
Some objects have several addresses, but this snippet only gets one of
them. Whereas if I try the same but like this:

$msg->code && die $msg->error;
foreach my $entry ($msg->all_entries) {
        if ($entry->get_value("$_[1]")) {
                print "$_\n" for $entry->get_value( "$_[1]" );
        } 
 }
        
then it does get all the addresses right. i prefer using a while loop
than a for loop, but if with the while loop it does not work, well, no
problem. I would like to know what I do wrong, though.

-- 
Groeten,
J.I.Asenjo

Reply via email to