Skip to content

Instantly share code, notes, and snippets.

@bigbosst
Created September 30, 2013 23:36
Show Gist options
  • Save bigbosst/6771875 to your computer and use it in GitHub Desktop.
Save bigbosst/6771875 to your computer and use it in GitHub Desktop.
> Hello,
>
> I need some help in understanding how to iterate thru array of hashes. I am
> basically pushing a hash into array and then passing the array to another
> function to iterate thru the values. I could do it parallely but I wanted to
> check the size of the array and make sure it is within the defined limits.
>
> Q: After passing the array, how can I iterate thru the entries to perform an
> LDAP-ADD. I am getting error adding. Any help or optimization for this?
>
> Does someone have a better way of doing this? I am performing a one-way sync
> from AD to LDAP with custom attributes and also making sure the additiona nd
> deletion list is within a specified limit.
>
> code:
> $user{'cn'} = $cn;
> $user{'givenName'}=$givenName;
> $user{'sn'} = $sn;
> $user{'description'} = $description;
> $user{'mail'} = $mail;
> $user{'uid'} = $sAMAccountName;
> $user{'objectClass'} = <at> objectClass;
> $user{'userPassword'} = &get_password;
> $user{'gidNumber'} = $gidNumber;
> $user{'loginShell'} = $loginShell;
> $user{'HomeDirectory'} = "/home/$sAMAccountName";
> $user{'gecos'} = $cn;
> $user{'uidNumber'} = $uidNumber;
> push <at> add_user,%user;
you cannot push hashes, perl will flatten it and push its contents.
You need to push a reference to the hash
push <at> add_user, \%user;
>
> Routine-2
> foreach $item ( <at> add_user )
> {
> $entry = Net::LDAP::Entry->new;
> $dn = "uid=".$item{'uid'}.",ou=People,".$ldap_base;
as $item is a reference to a hash, you have to access it as one
$item->{'uid'}
> print "DN:$dn \n"; # Test to see if DN prints properly
>
> my $mesg_ld = $ldap_ld->add($dn,attr => [ %item ] );
Graham.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment