Hello,
I'm new on this list and perl and hope you can help me out with a little
problem.
I have a little problem with a hash / key reference.
I have two hashes, where 1 hash stores a configuration and the other one
stores the description for some keys / values of %h1
my %config = {
'ip' => 'value1',
'netm' => 'value2',
'bcast' => 'value3',
}
my %configdesc = {
'ip' => "this is value1 !",
'netm' => "this is value1 !",
}
than in my code i ask the user for the values I have a key in %h2.
sub ask_config {
foreach (keys %configdesc) {
if ($config{$_}) {
printf gettext "Please enter %s", $configdesc{$_};
print "\n";
# print default value for array and scalars
if (ref($config{$_}) eq "ARRAY") { printf
gettext "(default: %s) : ", join(' ', @{$config{$_}});
} else {
printf gettext "(default: %s) : ", join(' ', $config{$_});
}
my $input = <STDIN>; chomp($input);
# array handling
if (ref($config{$_}) eq "ARRAY") {
my $key = $_;
if ($input ne '') {
my @tmp = split(/ /, $input);
$config{$key} = @tmp;
}
} else {
$config{$_} = $input if ($input ne '');
}
}
}
}
So now my problem is, that i have to ask the user in a specific order.
(ip before netmask, a.s.o.)
I thought i simply can change things like that: (i tested both ways,
1_ip and 2_netm)
my %config = {
'ip' => 'value1',
'netm' => 'value2',
'bcast' => 'value3',
'1_ip' => \$config{'ip'},
'2_netm' => sub { return $config{'ip'} }
'2_netm' => \&get_hostname,
}
my %configdesc = {
'1_ip' => "this is value1 !",
'2_netm' => "this is value1 !",
}
with:
sub get_hostname {
return ${$config{'hostname'}};
}
both require to disable subs or refs in strict what is not the best way,
i know.
Some1 have an idea to fix this problem or maybe do the whole thing some
better?
I dont wanna rewrite my whole code to change the config{ip} to
config{1_ip}.
Best regards and thanks for your efforts.
Erwin
_______________________________________________
Perl-Unix-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs