Perl LDAP NET::LDAP search queries, trying to store them as a variable

## Im trying to pull values from the ldap server.  The user is already authenticated as itself, but I want to pull out values for their account and store them as variables.  Obviously the syntax below is incorrect.  At this point in the code,  the ldap->bind is already bound, and i want to run a query with that bound information

I know how to do an ldapsearch command, but not familiar enough with the Net::LDAP to know how to parse it within perl..   For the values, I want to strip out  or Filter just the relevant info so that I get  these results.  I sort of want to awk/grep for specific return values.

$user = "John Doe"
$email = ""
## Get some variables in place from the ldap server so we can continue on with relevant user info
$email = "$data{username}\@domain\.com";   ##<-----   want to change this also to an ldap query
$name  = ($ldap->search ("uid=$data{username},ou=group," , cn));   #<---this section, how to i just pull out the username only
print "<p style=\"margin:10pt;\">$name </p>\n";    <----------this just prints out the data value for $name, but on the webpage is just shows up as    	Net::LDAP::Search=HASH(xxxxxxx).   

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kim RyanIT ConsultantCommented:
That's right, search return a hash of all the name/value pairs for the match. YOu need to unpack the hash like so,

foreach $entry ($name->entries)

Also see

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robstaroxAuthor Commented:
So how can i print out the values returned from the ldap search on the webpage in that section of code?....   need help withing my example code.
Kim RyanIT ConsultantCommented:
The link I pointed you to (
) includes several detailed examples by the author of this module on how to use it. The examples are very similar to the type of problem you are trying to solve, and should be fairly easy to adapt with a bit of trial and error.

I prefer to point people in the right direction rather than write every line of code for you. It means you get a better understanding of the problem. Can't see why this should be deleted.
robstaroxAuthor Commented:
## Get some variables in place$ldap = Net::LDAP->new ( 'ldapserver' );
$ldap->bind("uid=$data{username},", password => "$data{password}") or die ;
$mesg = $ldap->search ( base => "" , filter => "uid=$data{username}" , attrs => [ 'sn', 'mail' ] );
   my $entry = $mesg->entry($index);    my $email = $entry->get_value("mail");    my $name = $entry->get_value("sn");

Thanks!!   it took a while but i figured it out....

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.