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

Dear fellow monks,

I have looked at the documentation with Net::LDAP and have been able to come up with the following solution so far. This seems kind of clunky to me and I would really like to find a better way to use the information returned from the LDAP search.

use Net::LDAP; $id='1234'; $ldap = Net::LDAP->new($host,$port) or die "$@"; $ldap->bind; $mesg = $ldap->search (base => $base, filter => "employee_number=$id", attrs => [firstname,lastname,employee_number,department,ci +ty]); my $max = $mesg->count; # put into a local hash for later use... for($i=0;$i<$max;$i++){ my $entry = $mesg->entry($i); foreach my $attr($entry->attributes){ my $printstring = join("\| ",$attr, $entry->get($attr)); my ($attrkey,$attrvalue) = split(/\|/,$printstring); $userinfo{$attrkey}=$attrvalue; } } # test the hash... foreach $key(keys %userinfo){ print "$key\t $userinfo{$key}\n"; }

Mick

Replies are listed 'Best First'.
Re: Is there a better solution: data from Net::LDAP?
by clscott (Friar) on Nov 24, 2000 at 08:59 UTC
    Hi Mick,

    Besides me recommending that you use strict I notice that in your main loop you have a completely unnecessary flip flop from individual scalars to 1 scalar and back again (join then split on lines 15 && 16)

    It also that there are better methods to use in the wonders that are Net::LDAP (like entries to avoid the $max stuff). Are you using the most recent version? This is the documentation I'm using. Unfortunately I don't have an LDAP server to test against so the best test I can do is a perl -c which I can't do because ppm can't find the ppd from Activestate (grr windows).

    Here's my shot:

    use Net::LDAP; use strict; my $id ='1234'; my $ldap = Net::LDAP->new('host','port') or die "$@"; $ldap->bind; my $mesg = $ldap->search (base => $base, filter => "employee_number=$id", attrs => [firstname,lastname,employee_number,department,ci +ty]); my %userinfo; foreach my $entry ($mesg->entries){ foreach my $attr ($entry->attributes){ $userinfo{$attr} = $entry->get($attr); } #$entry->dump; # saw it in the examples but no docs! } # test the hash... foreach my $attr (keys %userinfo){ print "$attr\t$userinfo{$attr}\n"; } $ldap->unbind;

    I saw where in the documentation you got your example but I think this works a little more efficiently (if it works at all)

    Sorry about the lack of testing, normally I wouldn't do this (post untested code).

    --
    Clayton
    Long time reader, first time poster.

Re: Is there a better solution: data from Net::LDAP?
by ChOas (Curate) on Nov 23, 2000 at 17:10 UTC
    Hi!

    I don't know much about that module (nothing), but here's
    something:
    while (my $Current=$mesg->pop_entry) { foreach my $Attr($Current->attributes) { $UserInfo{$Attr}=$Current->get_value($Attr); } };

    Don't shoot me for this one, I have NO clue to this module,
    just read the man page to find the pop_entry, and got rid
    of the join and split because I didn't understand why those
    were there

    Code is untested ofcourse ;)))

      ChOas

    upd. changed the curly bracy thingies
    (they were: get_value{$Attr};
      Nope. Didn't work. Thanks for the attempt, though...
      Can't use subscript on subroutine entry at ldaptest1.pl line 26, near +"$Attr}" (Did you mean $ or @ instead of &?) Execution of ldaptest1.pl aborted due to compilation errors.
      I think I'll keep what I already have since it's easy to manipulate, unless thee's another solution out there.

      Mick
        Ahem.. I think I used curlies ('{ }') when I should
        have use thingies ('( )') ;)))

        I can't test, don't have the module ;))

        Good luck!!