문제

I have a perl script (abc.pl) and 2 config files (one var.pl and one config.txt)

Now

in var.pl

$admin_userid = "admin";
$guest_userid = "guest";

in config.txt - this has the value of user - can be either admin or guest

user=admin/guest

in abc.pl

require var.pl

$get_user = admin or guest (get this value from config.txt)
**$myfinal_userid = ??**

I want the value of myfinal_user_id as admin if user in config.txt is admin and guest if it is guest.

i.e. based on $get_user's value I want the value of userid - ${$get_user}."_userid" eg: if config.txt has user=admin, the $get_user = admin and I want $myfinal_userid = $admin_userid. similarly for guest. This has to be dynamic.

Finally what I want is, know the user from config.txt and based on it, get the userid from var.pl and store that in myfinal_userid.

Let me know how can I achieve this in perl?

도움이 되었습니까?

해결책

Use a hash to store the id's:

my %id = ( admin => 'admin',
           guest => 'guest',
         );

my $get_user = 'admin';  # Read this from the config.
my $final_id = $id{$get_user};

my $other_user = 'guest';
my $another_final_id = $id{$other_user};

print $final_id, "\n", $another_final_id, "\n";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top