Manuel Vacelet wrote:
Hello,

My question is probably more related to my own understanding of perl
than Net::LDAP module but it causes some headaches to me :)

The following code just works fine:
1 use strict;
2 use warnings;
3 use Net::LDAP;
4 my @servers =
['ldap://ldap5.example.com','ldap://ldap-fallback-eu.example.com','ldap://ldap.example.com','ldap://ldap2.example.com'];
5 my $ldap = Net::LDAP->new(@servers) or die "Unable to connect to
ldap server: [EMAIL PROTECTED]";

But, if I change [] by () in the servers array affectation L4, I get an error:
Unable to connect to ldap server: IO::Socket::INET: Bad hostname ''

I googled at bit but faild to find an explanation for the difference
between the list and the array definition. (The funniest part comes
when it works when the list contains an odd number of elements).

As it stands, line 4 is defining an array with one item, which is a ref to an array of URLs. Net::LDAP->new is documented as taking either a string or an array ref as the first argument, and then an optional number of key/value pairs of option parameters.

When you pass an array to a perl function, actually all the elements in the array get passed instead.

Hopefully you can see how an odd number of elements in @servers makes things "work".

My very problem is that the list of available ldap server comes from a
configuration variable (a scalar) I split for Net::LDAP->new.


Anyway, what you want is:

my $servers = [ 'ldap://foo', 'ldap://bar', 'ldap://baz' ];
my $ldap = Net::LDAP->new($servers) or die "blah blah"

$servers is now a scalar variable, "pointing" at (referring to) an array of URLs.

I'd recommend playing around a bit in the perl debugger, setting some variables and then printing them out using 'x'.

perl -d -e 1
<test stuff here>

Cheers,

Chris

Reply via email to