Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Net::LDAP doesn't return until end of loop

by lachoy (Parson)
on Jun 28, 2003 at 16:14 UTC ( #269890=note: print w/replies, xml ) Need Help??

in reply to Net::LDAP doesn't return until end of loop

The docs say that the second object passed to the callback for a search will be an entry found:

If the request is a search then multiple packets can be received from the server. Each entry is received as a separate packet. For each of these the subroutine will be called with a Net::LDAP::Entry object as the second argument.

So I think the callback should look like this:

sub callback { my ($searchobj, $entry) = @_; # note: no 'shift' if ($entry) { print $entry->dn, "\n"; print $entry->get_value('plan'); print $entry->get_value('ipaddress'); print $entry->get_value('username'); } else { warn "No entry passed in for callback...\n"; } }

This will at least let you know that your callback is being called the appropriate number of times.

M-x auto-bs-mode

Replies are listed 'Best First'.
Re: Re: Net::LDAP doesn't return until end of loop
by ypcat (Beadle) on Jun 30, 2003 at 22:01 UTC
    I changed my callback to be without the shifting of entry. What happens is all looped data including the 10th mac address return "No entry passed...", however the 10th also returns the values from ldap that were queried for. This doesn't make any sense because its acting like for the first 9 Mac's no entry is passed, this I believe, but then on the 10th, no entry is passed, but data is returned ?!? Please advise.

      Two things: First, you probably want to pass \@attrs or [ @attrs ] instead of what you've got. You might also need to set scope => 'sub' in the search attributes.

      Second, I'd try a different strategy, using this idiom to run through the results of a search:

      my $ldap_msg = $ldap->search( ... ); if ( my $code = $ldap_msg->code ) { die "Error running search: ", Net::LDAP::Util::ldap_error_text( $code ); } while ( my $entry = $ldap_msg->shift_entry ) { # use $entry }

      It's been a while since I used LDAP and I just cribbed this from SPOPS::LDAP, which I wrote when I was using LDAP :-)

      M-x auto-bs-mode

        I re-did the code to match you're strategy, cause I am not against changing what I am doing. This is my first time with Perl/LDAP and it is a time pressed issue so I really appreciate all you're help and guidance. After re-writing the code it is still experiencing the same issue. Here is the new code.
        sub ldapsearch { my $mac = shift; print "$mac\n"; my $searchobj = $connection->search( base => 'o=ldap', scope => 'sub', filter => "cn=$mac", attrs => @attrs, #note I al +so tried \@attrs ); $searchobj->code && die $searchobj->error; while ( my $entry = $searchobj->shift_entry) { print $entry->dn, "\n"; print $entry->get_value('plan'); print $entry->get_value('ipaddress'); print $entry->get_value('username'); } }

        Any suggestion ?

        After reviewing this thread for the 100th time I figured out what my issue was. In the first reply archon states your not comping your $mac, boy was he ever right. This was fixed by throwing a chomp $mac into my for each loop before the sub routine ldapsearch() is called. Thanks again for everyones help. Atleast now my code is a little more refined and works !

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://269890]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2022-05-25 03:22 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (84 votes). Check out past polls.