Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Perl LDAP script error "Can't call method "dn" on an undefined value"

by anakin30 (Acolyte)
on Apr 03, 2013 at 04:53 UTC ( #1026775=perlquestion: print w/replies, xml ) Need Help??
anakin30 has asked for the wisdom of the Perl Monks concerning the following question:

i received below error when i run my code

The script generate result if the input file contains less then 10 users, however the error trigger if the input file contain more than 10 users

i appreciate if anybody can advice, how to rectify the error

Can't call method "dn" on an undefined value at line 45, <TEST> line 991.

i have marked the error , line number with the comment # in the below code for your reference

$TEST = 'C:\Perl\project\export\20130402\test.txt'; #$TEST = 'C:\Perl\project\export\20130402\users.txt'; $outputfile = 'C:\Perl\project\export\20130402\result.csv'; open (FH, ">$outputfile") or die "$!"; print FH "uid,SN,GIVENNAME,uniSALUTATION,Displayname\n"; my $adHost = ""; my $adUsr = "uid=myscript,ou=users,"; my $adPass = "Myscript2012"; my $query = "(objectclass=uniperson)"; my @attrs = qw(uid SN GIVENNAME uniSALUTATION Displayname); my $ldap = Net::LDAP->new($adHost) or die $!; my $mesg = $ldap->bind($adUsr, password =>$adPass); my $page = Net::LDAP::Control::Paged->new( size => 1000 ) or die $!; open(TEST) or die("Could not open log file."); foreach $line (<TEST>) { chomp($line); #print "$line \n";#} #while (1) { my @args = ( base => 'ou=users,' , scope => 'sub', filter => +$line, attrs => \@attrs, control => [ $page ],); $mesg = $ldap->search ( @args ) or die $!; #while (my $entry = $mesg->shift_entry() ) { #uid,SN,GIVENNAME,uniSALUTATION,Displayname my $entry = $mesg->shift_entry(); my $entrydn = $entry->dn(); #line 45 the error triggered here my $uid = $entry->get_value ('uid'); my $SN = $entry->get_value ('SN'); my $GIVENNAME = $entry->get_value ('GIVENNAME'); my $uniSALUTATION = $entry->get_value ('uniSALUTATION'); my $Displayname = $entry->get_value ('Displayname'); print FH "$uid,$SN,$GIVENNAME,$uniSALUTATION,$Displayname\n"; }

please advice me how to get rid of the error above. I need to export 3000 users based on userID from input file with the selected attributes from LDAP using this script"

  • Comment on Perl LDAP script error "Can't call method "dn" on an undefined value"
  • Download Code

Replies are listed 'Best First'.
Re: Perl LDAP script error "Can't call method "dn" on an undefined value"
by Loops (Curate) on Apr 03, 2013 at 05:41 UTC
    Hi there,

    Likely what is happening is that "$mesg" is not receiving valid data back from $ldap->search. You may just have a single bad entry in your test.txt file.

    At a minimum you will want to add a line that tests the value of $entry, like so:

    my $entry = $mesg->shift_entry(); next unless $entry;

    In order to skip over any bad entries in your input text file. Of course you should probably do proper error reporting as well.

    To debug this further you can use Data::Dumper and show the return results of the search function. If my guess is correct you will see 'errorMessage => "Bad filter"' in the returned hash:

    use Data::Dumper; ... $mesg = $ldap->search ( @args ) or die $!; print Dumper $mesg;

      Hi Loops,

      Thanks for sharing with me .. i appreciate that a lot.

Re: Perl LDAP script error "Can't call method "dn" on an undefined value"
by Loops (Curate) on Apr 03, 2013 at 07:14 UTC
    In case it helps to see a working example, here's a stripped down version of your code configured to work with my universities public ldap server:
    use strict; use warnings; use Net::LDAP; use Net::LDAP::Util qw(ldap_explode_dn); my $ldap = Net::LDAP->new("") or die $!; my $bind = $ldap->bind(); my @attr = (qw(sn givenName cn mail)); my @args = ( base => 'ou=people,o=SFU,c=CA', scope => 'sub', filter => '(objectclass=*)', attrs => \@attr); $,=','; my $mesg = $ldap->search ( @args ) or die $!; while (my $entry = $mesg->shift_entry()) { my $UID = ldap_explode_dn($entry->dn())->[0]->{UID}; print $UID, (map {$entry->get_value($_)} @attr), "\n"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1026775]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-06-24 17:18 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.