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

hello perl mongers,

i think this is more of a hashref or HoH question, I'm seeing a data structure similar to HoH in the data dumper output, but am having problems accessing key value pairs returned to the $hashref = Net::LDAP::LDIF->new function. specifically i'm having issues accessing the dn even though it's listed as a attribute in the ldif file. ldif data file example:
# extended LDIF # # LDAPv3 # base <ou=help,dc=perl,dc=monks> with scope subtree # filter: (|(Id1=*)(Id2=*)) # requesting: dn FriendlyId CreatedOn # # userid, help, perlmonks.com dn: uid=userid,ou=help,dc=perl,dc=monks FriendlyId: Pizza IdCreatedOn: 27-02-2099 00:00:00 data dumper displays the dn's in its output via data dumper: <code> $VAR1 = bless( { 'attrs' => { 'friendlyid' => [ 'Pizza' ], 'idcreatedon' => [ '27-02-2099 00:00 +:00' ] }, 'changes' => [], 'asn' => { 'objectName' => 'uid=userid,ou=help,dc=per +l,dc=monks',
What I'm interested in is the objectname above which happens to be the dn and holds the information i need to formulate my report, along with the other data in the object. my problem is that i can't access the dn object within each entry, but can access the other attributes available in the ldif. I see them in data dumper out put and can access them via standard methods. i.e. entry->get_value("attribute-name"), but this doesn't work for the dn...
#i.e. these work: print $entry->get_value("IdCreatedOn")."\n"; print $entry->get_value("FriendlyId")."\n"; #but can't get the dn: print $entry->get_value("dn")."\n"; print $entry->get_value("objectName")."\n";
above won't pull back the object name which has the dn i need to access and itterate through along with the other elements of the hash.
#!/usr/bin/perl use Net::LDAP::LDIF; use Net::LDAP::Entry; use Data::Dumper; use strict; use warnings; my $ldif = Net::LDAP::LDIF->new( "../packages/ldapsearch2.ldif", "r") +or die "file not exits\n"; while( not $ldif->eof ( ) ) { my $entry = $ldif->read_entry ( ); #this doesn't work print "HERE is the DN!!!"."\n"; # 'asn' => { # 'objectName' print "anything?" . "\n". $ldif->{'asn'}{'objectName'}. "\n"; # but this shows the dn's in the output, but how do I get them +to print? print Dumper $entry; } #the following doesn't work.. my @arrayOfDNs = keys %$ldif; print "here are all the dn's:"."\n"; print @arrayOfDNs;
looking for some jedi ldif reporting experts, I know you're out there!! ...and probably can solve this very quickly, THANK YOU!.
Really could use the help here, working to automate this ldif export perl to csv daily processing and i know i can always count on the monks to point me in the right direction, thanks in advance. this returns the keys, just need to get the subkeys
foreach my $key (keys %{$ldif}) { print "\n this is the key:" . $key . "\n"; #need to dereference the subkeys... not working yet below. #for my $subkey ( keys %{$ldif{$key}} ) { # print "Subkey=$$ldif{$key}{$subkey} "; #} print "\n" . "\n"; }
- 3dbc

Replies are listed 'Best First'.
Re: can't access dn within ldif file
by NetWallah (Canon) on Dec 27, 2017 at 03:04 UTC
    You are accessing:
    $ldif->{'asn'}{'objectName'} # Does not exist
    instead of
    $entry->{'asn'}{'objectName'} # Dumper shows this exists

                    We're living in a golden age. All you need is gold. -- D.W. Robertson.

      That worked, THANK YOU!!!

      what an ID10T error!

      at least someone on here can understand me because everyone sitting next to me or around me has no F'ing idea what I'm talking about!!!

      Happy Holidays PERL MONKS!!

      More ramblings...

      print "\n\n" . "this is the nycbidcreatedon: " . $entry->{'attrs'} +{'idcreatedon'} . "\n\n"; print "\n\n" . "this is the nycbfriendlyid: " . $entry->{'attrs'}{ +'friendlyid'} . "\n\n"; print "\n\n" . "this is the DN!! " . $entry->{'asn'}{'objectName'} + . "\n\n"; # Dumper shows this exists

      above $entry's don't work for idcreatedon or friendlyid within the while( not $ldif->eof ( ) ) { loop, I'll try within the foreach my $key (keys %{$ldif}) { loop instead. guess that ldif read entry is a special way to hop through the HoH.
      But I can get those other elements in there using:
      while( not $ldif->eof ( ) ) { #loop print $entry->get_value("IdCreatedOn")."\n"; print $entry->get_value("FriendlyId")."\n";
      strange.
      - 3dbc
        Try:
        $entry->{'attrs'}{'idcreatedon'}[0] $entry->{'attrs'}{'friendlyid'} [0]
        If your version of "get_value" fails, try this (ALL untested):
        print $entry->get_value("idcreatedon")."\n"; print $entry->get_value("friendlyid")."\n"; # The hash index is case-sensitive, and "get_value" documentation dema +nds 'exact match'
        UPDATE: Removed attempt to do $entry->{attrs}->get_value, because 'get_value" already references the has index attrs first.

                        We're living in a golden age. All you need is gold. -- D.W. Robertson.

Re: can't access dn within ldif file
by poj (Abbot) on Dec 26, 2017 at 22:03 UTC