Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

use a variable in ldapsearch filter

by Cornichon (Initiate)
on Mar 27, 2020 at 23:50 UTC ( #11114743=perlquestion: print w/replies, xml ) Need Help??

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

Hello everyone,

I'm writing a script that receives as a parameter the name of a user and performs a search in LDAP but I can't find a way to use a Perl variable in the LDAP search filter.

The code used for the search is this one:

$value = $ldap->search( base => 'dc=xxdom,dc=xxdc', scope => 'sub', filter => '(sn="xx")', );

Let's say the variable $name contains the name I need to do the search.

I tried this: filter => '(sn = $name)'.

It's not working, does anyone have a solution for me?

Replies are listed 'Best First'.
Re: use a variable in ldapsearch filter
by kcott (Bishop) on Mar 28, 2020 at 01:47 UTC

    G'day Cornichon,

    Welcome to the Monastery.

    "It's not working, ..."

    I see a problem has been pointed out.

    You'll often encounter the same problem when writing one-liners: the code needs to be delimited by single- or double-quotes (depending on the platform) which can be a problem if the code itself needs to use similar quotes.

    "... does anyone have a solution for me?"

    The solution is to use alternative quotes. Have a look in perlop. You'll find general information under "Quote and Quote-like Operators"; and more specific details under "Quote-Like Operators".

    [Yes, the names of those sections could be improved.]

    Here's a couple of examples.

    First, using alternative quotes to replicate what your code is currently doing:

    $ perl -E 'my $name = q{Ken}; say q{(sn = $name)}' (sn = $name)

    Next, using alternative quotes to achieve what you want your code to be doing:

    $ perl -E 'my $name = q{Ken}; say qq{(sn = $name)}' (sn = Ken)

    — Ken

Re: use a variable in ldapsearch filter
by AnomalousMonk (Bishop) on Mar 28, 2020 at 01:17 UTC

    Are you aware that single-quoted strings do not interpolate?

    c:\@Work\Perl\monks>perl -wMstrict -le "my $name = 'foobleooble'; print '(sn = $name)'; " (sn = $name)

    Give a man a fish:  <%-{-{-{-<

Re: use a variable in ldapsearch filter
by Cornichon (Initiate) on Apr 09, 2020 at 16:49 UTC

    Thank you for your answers, unfortunately after many tests, the LDAP filter does not use the value contained in the variable.

    I put you the code that I currently use to see if you have another solution :

    package cnmodule; sub function1 { use strict; use Net::LDAP; my ($local) = @_; my $valueA; my $ldap = Net::LDAP->new('',version => 3) or die "Can t connect to LDAP ($@)"; $valueA = $ldap->bind ("cn=usercheck,cn=users,dc=domain,dc=network", + password => 'password'); $valueA->code && die $valueA->error; $valueA= $ldap->search( base => 'dc=domain,dc=network', scope => 'sub', filter => qq{(sn=q{$local})}, ); $ldap->unbind(); $valueA->code && die $valueA->error; foreach my $entry ($valueA->all_entries) { my $valueB = $entry->get_value('CN'); } return $valueB; } $variableA='test'; $variableB=function1($variableA); print $variableB; 1;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11114743]
Approved by kcott
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2020-06-03 14:05 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (24 votes). Check out past polls.