Recently I've asked two questions:
Traverse LDAP Tree and
Populating hash keys. The first one wasn't actually answered, and I decided to apply second way. It's too bad for me, but I can't understand how to implement the following task using techniques described in
Populating hash keys.
What I have is a number of arrays containing LDAP dn`s split by ',' and reversed, as well as corresponding entries:
qw(p=root cp=1)
qw(p=root cp=1 n=1)
qw(p=root cp=1 n=1 n=1)
qw(p=root cp=1 n=1 n=1 n=1)
qw(p=root cp=1 n=1 n=1 n=1 n=1)
qw(p=root cp=1 n=1 n=1 n=1 n=2)
qw(p=root cp=1 n=1 n=1 n=1 n=3)
qw(p=root cp=1 n=1 n=1 n=2)
qw(p=root cp=1 n=1 n=1 n=2 n=1)
qw(p=root cp=1 n=1 n=1 n=2 n=2)
qw(p=root cp=1 n=1 n=1 n=2 n=3)
Each
What I need is to create the following hash:
$href->{p=root}{cp=1} = 'entry1'
$href->{p=root}{cp=1}{n=1) = 'entry11'
#.. etc
Then I will be able to recursively walk trough it.
My code:
use strict;
use Net::LDAP;
use Text::ParseWords;
use Data::Dumper;
my $host = 'somehost';
my $port = 400;
my $user = 'p=xxx';
my $pass = 'secret';
my $base = 'cp=1, p=root';
my $ldap = Net::LDAP->new($host, port => $port) or die "$@\n";
$ldap->bind($user, password => $pass);
my $mesg = $ldap->search (
base => "$base",
filter => "(objectClass=*)",
);
$mesg->code && die $mesg->error . "\n";
my %entries;
foreach my $entry ($mesg->all_entries) {
my $dn = $entry->dn;
$dn =~ s/\s+//g;
$entry->dn($dn);
my @path = reverse parse_line(",", 1, $dn);
$entries{join ',', @path} = $entry; # this is not what I want exa
+ctly
}
$ldap->unbind;
foreach my $dn (sort keys %entries) {
print "$dn\n";
}
exit;