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

Normally this would be a simple operation for me as I would take something like this:
my @hosts = ('ldap0','ldap','ldap2'); foreach my $host (@hosts) { my $ldap_conn = new Mozilla::LDAP::Conn($host);
and proceed from here. I'm faced with one of the ldap servers listening on a different port. So my code needs to look something like this:
my @hosts = ('ldap0:390','ldap:389','ldap:389');
Unfortunately the perldap method new Mozilla::LDAP:Conn does not recognize a value like 'host:port'. The colon confuses it. How can I split() the host/port values in @hosts, and feed them into perldap thusly:
my $ldap_conn = new Mozilla::LDAP::Conn($host,$port);
Thank you.

Replies are listed 'Best First'.
Re: looping an array through perldap
by srawls (Friar) on Jun 06, 2001 at 00:40 UTC
    my @hosts = ('ldap0:80','ldap:3','ldap2:23'); foreach my $host (@hosts) { my $ldap_conn = new Mozilla::LDAP::Conn(split':',$host) }
    Look into split for more detail

    The 15 year old, freshman programmer,
    Stephen Rawls
Re: looping an array through perldap
by lestrrat (Deacon) on Jun 06, 2001 at 00:41 UTC

    I know nothing about Mozilla::LDAP, but assuming that it accepts ( $host, $port ) for the Conn->new() subroutine, can't you just

    my $ldap_conn = new Mozilla::LDAP::Conn( split( /:/, $host ) );

    Or am I missing something...

Re: looping an array through perldap
by protos (Initiate) on Jun 06, 2001 at 01:19 UTC
    Works like a charm:
    #!/usr/bin/perl5 -w use strict; use Mozilla::LDAP::Conn; use Time::HiRes qw(gettimeofday tv_interval); # measure elapsed time from open to close of each ldap connection my $t0 = [gettimeofday]; my @hosts = ('ldap0:390','ldap:389','ldap2:389'); foreach my $host (@hosts) { my $conn = new Mozilla::LDAP::Conn(split( /:/,$host)); die "connection failed" unless $conn; my $responsetime = sprintf("%d",tv_interval($t0)*1000); print "$responsetime "; } print "\\n"; print "\n";
    Thanks gang!

    protos

Re: looping an array through perldap
by the_slycer (Chaplain) on Jun 06, 2001 at 00:42 UTC
    foreach (@hosts){ my ($host,$port)=split /:/; etc.. }
    See split for more info.