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

Hi monks, I'm working on a CGI program with Net::LDAP to update an OpenLDAP addressbook. I'm not an LDAP expert by any means but I worked with the command line tools so I would understand the basics. I'm having a repeated problem at first I thought I just didn't get it but I keep running into the same error with different chunks of Perl code.
my $ldap = Net::LDAP->new( "$ldap_svr" ) or die "$@"; my $search = $ldap->bind; $search = $ldap->search( base=> "$base", filter=>"mail=$name_email" ); my $max = $search->count; for ( my $index = 0; $index < $max ; $index++ ) { my $entry = $search->entry($index); my $dn = $entry->dn; my @attrs = $entry->attributes; foreach my $var (@attrs) { my $attr = $entry->get_value( $var, asref => 1 ); if ( defined($attr) ) { foreach my $value ( @$attr ) { print h2("$var: $value"),p; } } } } $search= $ldap->unbind;
I get the error:
Can't call method "count" without a package or object reference at c:\PROGRA~1\APACHE~1\apache\cgi-bin\PROCES~2.PL line 126
I was also getting an error before about using $search->entries method. As you can see I'm testing this on a windows XP machine with active state v5.8.2 and apache using perl as a cgi.

TIA,
chrisj

Replies are listed 'Best First'.
Re: Net::LDAP issues...
by Old_Gray_Bear (Bishop) on May 11, 2004 at 00:00 UTC
    It sounds like your bind() is failing. You need to check for an LDAP error/message returned after the bind() call. I'd also talk a good look at the value of $ldap_svr before the bind(), I've spent more than one debugging session discovering that my User (often Me) fat-fingered the server name. Net::LDAP is very good about not die-ing on errors and handing status back to the caller.

    ----
    I Go Back to Sleep, Now.

    OGB

      Got it. Thank You.
      bind() was failing. and I couldn't figure out the cryptic error messages.
      Works like a charm now...!
      Thanks again,
      chrisj
Re: Net::LDAP issues...
by sgifford (Prior) on May 10, 2004 at 23:38 UTC
    One thing that would help is checking error codes. That may tell you why things are failing. For example:
    my $search = $ldap->bind; $search->code && die $search->error;

    This is just a modified version of the code in the synopsis.

      I added
      $search->code && die $search->error;
      after:
      $search = $ldap->search( base=> "$base", filter=>"mail=$name_email" );
      I got the same error but with "code" instead of "count:
      Can't call method "code" without a package or object reference at c:\PROGRA~1\APACHE~1\apache\cgi-bin\PROCES~2.PL line 126

      Thanks,
      but it didn't fix it.
      chrisj

        Hrm. Three more thoughts. First, try printing out $search right after you call $ldap->search; that should tell you what type of object Perl thinks it is. Net::LDAP's search method is supposed to return a Net::LDAP::Search object. You might also want to make sure you have use Net::LDAP::Search at the top of your script; I'm not sure if Perl handles this automatically. Finally, make sure you check for an error after the bind method, since if that's failing it could explain why your other calls are failing.