Reference to hash and array

V

Vito Corleone

Hi,
I downloaded a module that need me to input the value in very confusing
way. For example:
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", "192.168.1.1:5500" ],
'debug' => 0 } );

You put the list of your servers IP and port number into servers. I put
my servers list in config file. It looks like this:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500';

And then when I load the module, I do:
my @servers = split(" ", SERVERS);
$m = Module::->new(
{ 'servers' => \@servers,
'debug' => 0 } );

So far so good. But you can also load the module this way.
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", ["192.168.1.1:5500", 3] ],
'debug' => 0 } );

Besides IP and port number, it also takes the value of the server (ie:
3). I want to make my config file looks like:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500,3';

But I don't know how can I pass these values to the module. Or is there
any better way to keep these values in config file? Please help, and
thanks in advance.

--vc
 
A

Anno Siegel

Vito Corleone said:
Hi,
I downloaded a module that need me to input the value in very confusing
way. For example:
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", "192.168.1.1:5500" ],
'debug' => 0 } );

You put the list of your servers IP and port number into servers. I put
my servers list in config file. It looks like this:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500';

And then when I load the module, I do:
my @servers = split(" ", SERVERS);
$m = Module::->new(
{ 'servers' => \@servers,
'debug' => 0 } );

So far so good. But you can also load the module this way.
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", ["192.168.1.1:5500", 3] ],
'debug' => 0 } );

Besides IP and port number, it also takes the value of the server (ie:
3). I want to make my config file looks like:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500,3';

But I don't know how can I pass these values to the module. Or is there
any better way to keep these values in config file? Please help, and
thanks in advance.

Untested:

my @servers = map /,/ ? [ split /,/] : $_, split ' ', SERVERS;

Anno
 
K

KKramsch

In said:
Hi,
I downloaded a module that need me to input the value in very confusing
way. For example:
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", "192.168.1.1:5500" ],
'debug' => 0 } );
You put the list of your servers IP and port number into servers. I put
my servers list in config file. It looks like this:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500';
And then when I load the module, I do:
my @servers = split(" ", SERVERS);
$m = Module::->new(
{ 'servers' => \@servers,
'debug' => 0 } );
So far so good. But you can also load the module this way.
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", ["192.168.1.1:5500", 3] ],
'debug' => 0 } );
Besides IP and port number, it also takes the value of the server (ie:
3). I want to make my config file looks like:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500,3';
But I don't know how can I pass these values to the module. Or is there
any better way to keep these values in config file? Please help, and
thanks in advance.

What Anno posted is right, I'm sure, but the code below may be
easier to follow.

# untested
my @servers;
for my $info (split ' ', SERVERS) {
# $info is either of the form ip:port" or "ip:port,num"
my ($ip_port, $num) = split /,/, $info;
if ($num) {
# $info is of the form "ip:port,num"
push @servers, [$ip_port, $num];
}
else {
# info is of the form "ip:port"
push @servers, $info;
}
}

my $m = Module::->new( { 'servers' => \@servers,
'debug' => 0 } );

__END__

Makes sense?

Karl
 
V

Vito Corleone

Ok, I got it now. Thanks Karl :)

In said:
Hi,
I downloaded a module that need me to input the value in very confusing
way. For example:
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", "192.168.1.1:5500" ],
'debug' => 0 } );
You put the list of your servers IP and port number into servers. I put
my servers list in config file. It looks like this:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500';
And then when I load the module, I do:
my @servers = split(" ", SERVERS);
$m = Module::->new(
{ 'servers' => \@servers,
'debug' => 0 } );
So far so good. But you can also load the module this way.
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", ["192.168.1.1:5500", 3] ],
'debug' => 0 } );
Besides IP and port number, it also takes the value of the server (ie:
3). I want to make my config file looks like:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500,3';
But I don't know how can I pass these values to the module. Or is there
any better way to keep these values in config file? Please help, and
thanks in advance.

What Anno posted is right, I'm sure, but the code below may be
easier to follow.

# untested
my @servers;
for my $info (split ' ', SERVERS) {
# $info is either of the form ip:port" or "ip:port,num"
my ($ip_port, $num) = split /,/, $info;
if ($num) {
# $info is of the form "ip:port,num"
push @servers, [$ip_port, $num];
}
else {
# info is of the form "ip:port"
push @servers, $info;
}
}

my $m = Module::->new( { 'servers' => \@servers,
'debug' => 0 } );

__END__

Makes sense?

Karl
 
K

KKramsch

In said:
Ok, I got it now. Thanks Karl :)

Actually, I spotted a bug. See below.
On Thu, 2 Dec 2004 16:44:11 +0000 (UTC)
In said:
Hi,
I downloaded a module that need me to input the value in very confusing
way. For example:
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", "192.168.1.1:5500" ],
'debug' => 0 } );
You put the list of your servers IP and port number into servers. I put
my servers list in config file. It looks like this:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500';
And then when I load the module, I do:
my @servers = split(" ", SERVERS);
$m = Module::->new(
{ 'servers' => \@servers,
'debug' => 0 } );
So far so good. But you can also load the module this way.
$m = Module::->new(
{ 'servers' => [ "192.168.1.0:5500", ["192.168.1.1:5500", 3] ],
'debug' => 0 } );
Besides IP and port number, it also takes the value of the server (ie:
3). I want to make my config file looks like:
use constant SERVERS => '192.168.1.0:5500 192.168.1.1:5500,3';
But I don't know how can I pass these values to the module. Or is there
any better way to keep these values in config file? Please help, and
thanks in advance.

What Anno posted is right, I'm sure, but the code below may be
easier to follow.

# untested
my @servers;
for my $info (split ' ', SERVERS) {
# $info is either of the form ip:port" or "ip:port,num"
my ($ip_port, $num) = split /,/, $info;
if ($num) {
^^^^^^^^^

That last line should be

if (defined $num) {

Otherwise you'd get incorrect results if $num happened to be 0
(assuming that such value makes sense).

Karl
# $info is of the form "ip:port,num"
push @servers, [$ip_port, $num];
}
else {
# info is of the form "ip:port"
push @servers, $info;
}
}

my $m = Module::->new( { 'servers' => \@servers,
'debug' => 0 } );

__END__

Makes sense?

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top