in reply to Re^2: Perl Builds an LDIF
in thread Perl Builds an LDIF

Grateful, I return with good news. I have removed the dashes, unwrapped the search result and sent to a file (although I had to hardcode the filename. My next plea is to discover why Net::LDAP::LDIF only writes the last result to FILE, I wish all results to write to FILE. Here's the updated code.

while (<PERSON>){ chomp; $dn = $ldap->search( #return only the employeeID DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => ['1.1'] ); Net::LDAP::LDIF->new( "buildAD.ldif", "w", wrap=>40 )->write( $dn->ent +ries ); }

Replies are listed 'Best First'.
Re^4: Perl Builds an LDIF
by rfransix (Novice) on Jun 15, 2010 at 19:34 UTC

    Happy, I return with more good news. I have been able to send all search results to a file. However, I wish to iteratively search on PERSON values, writing the result to FILE, as well as the print OUT statements, to build an LDIF. This is not happening. Instead, the print OUT statements are clobbering FILE, overwriting the search results. Here's the code, thanks.

    while (<PERSON>){ chomp; $dn = $ldap->search( #return only the employeeID DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => ['1.1'] ); my $result = Net::LDAP::LDIF->new( "buildAD.ldif", "a", wrap=>40 ); while( my $entry = $dn->pop_entry() ) { $result->write_entry($entry); } print OUT "changetype: modify"; print OUT "replace: manager"; print OUT "manager: dn","\n"; } $dn = $ldap->unbind; #session ends
      Your are building new "$result" objects for each PERSON.

      Try moving the "new LDIF" to outside of the loop:

      my $ldif_OUT = Net::LDAP::LDIF->new( "buildAD.ldif", "a", wrap=>40 ); while (<PERSON>){ chomp; $dn = $ldap->search( #return only the employeeID DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => ['1.1'] ); next unless $dn; # Make sure something is returned.. while( my $entry = $dn->pop_entry() ) { $ldif_OUT->write_entry($entry); } print OUT "changetype: modify"; print OUT "replace: manager"; print OUT "manager: dn","\n"; } $dn = $ldap->unbind; #session ends
      (Minimalistic error-checking added..)

           Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

        Humbled, I return for wisdom. The while statements are erratic, my wish is to have a 1to1 relationship between the while statements and the input files, yet the 2nd while <MGR> will pull results for 50 lines before the first while <PERSON> runs again. I wish to read iteratively <PERSON> one line at a time, then print @lines, then read iteratively <MGR>, one line at a time, resulting in a four lines written to OUT or buildAD.ldif.

        Also, perl throws an error on the second $_ use; how else may it be defined as I wish to place the next line from <MGR> file into the filter?

        Error reads: Use of uninitialized value $_ in pattern match (m//) at build4.pl line 48, <MGR> line 120 (#1)(W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.

        I am also puzzled why print OUT (@lines) does not print the contents of LDIFA after each line of <PERSON> is processed (instead it is printing erratically to OUT). And, why OUT fails in Net::LDAP::LDIF->new( "OUT", "a", wrap=>40 );, I am forced to hardcode "buildAD.ldif" instead of OUT.

        my $ldap = Net::LDAP->new("$HOST", port=>389) or die "$@"; my $dn = $ldap->bind("$ADMIN", password=>"$PWD"); my $dnm = $ldap->bind("$ADMIN", password=>"$PWD"); my @attr = "1.1"; my $result = Net::LDAP::LDIF->new( "buildAD.ldif", "a", wrap=>40 ); my @lines = (<LDIFA>); while (<PERSON>) { chomp; $dn = $ldap->search( #return only the employeeID DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => [@attr] ); next unless $dn; # Make sure something is returned while( my $entry = $dn->pop_entry() ) { $result->write_entry($entry); if ( $result != /dn:/ ) { print OUT (@lines); } while (<MGR>) { chomp; $dnm = $ldap->search( #return only the manager DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => [@attr] ); next unless $dnm; # Make sure something is returned while( my $entry = $dnm->pop_entry() ) { $result->write_entry($entry); } } } } $dn = $ldap->unbind; #session ends $dnm = $ldap->unbind; #session ends close OUT2 or die "Cannot close in-memory file: $!"; close OUT or die "Cannot close in-memory file: $!"; close MGR or die "Cannot close in-memory file: $!"; close PERSON or die "Cannot close in-memory file: $!"; close LDIFA or die "Cannot close in-memory file: $!";

      Grateful. I return with no good news. $result continues to clobber FILE, or possible print OUT clobbers FILE; either way, the desired output in an iterative format is not resolved. We pray for continued wisdom.

        I have reverted to the previous code, as I am getting no where with this exploration. This code gives me the DN from ou3 file to the OUT file, but once I hit the $entry = $dn->pop_entry(): line, it breaks again. If I do not use the pop_entry() method, each result sends the following to OUT (Net::LDAP::Search=HASH(0x1c72f1c)). Is there any other way around this problem? Thanks again.

        #! perl use strict; use warnings; use diagnostics; use Net::LDAP; use Net::LDAP::Entry; use Net::LDAP::Search; use Net::LDAP::LDIF; open PERSON, "<", "ou3" or die "Cannot open 'ou3': $!"; open MGR, "<", "ou8" or die "Cannot open 'ou8': $!"; open OUT, ">", "buildAD.ldif" or die "Cannot open 'buildAD.ldif': $!"; my $HOST = "11"; my $ADMIN = "cn=d"; my $PWD = "0"; my $BASEDN = "DC=corp"; my $ldap = Net::LDAP->new("$HOST", port=>389) or die "$@"; my $dn = $ldap->bind("$ADMIN", password=>"$PWD"); my @attr = "1.1"; my $result = Net::LDAP::LDIF->new( "buildAD.ldif", "a", wrap=>40 ); my $entry = <>; while (<PERSON>){ chomp; $dn = $ldap->search( #return only the employeeID DN base => "$BASEDN", filter => "(&(objectClass=user)(employeeID=$_))", scope => "sub", attrs => [@attr], ); next unless $dn; #make sure something is returned $entry = $dn->pop_entry(); $result->write_entry($entry); print OUT "changetype: modify"; print OUT "replace: manager"; } $dn = $ldap->unbind; #session ends close OUT or die "Cannot close in-memory file: $!"; close MGR or die "Cannot close in-memory file: $!"; close PERSON or die "Cannot close in-memory file: $!";