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

Humble and grateful, your insightful observation to chomp has revived this perl program to a higher level. The resultant output remains divided by a dash line still; and Data::Dumper and print OUT, these lines are not working as buildAD.ldif only gets ,,bless'' coding structure.

foreach $dn ($dn->entries) { push @attr, $dn->dump; } #$Data::Dumper::Purity = 1; #$Data::Dumper::Deepcopy = 1; my $ev = Data::Dumper->Dump($dn, qw(dn)); print OUT $ev; }

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

    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 ); }

      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

        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.