in reply to Re: Interpolating variables from a file
in thread Interpolating variables from a file

1 and 3. What it's actually being used for is this - In the course of my work, I've got to login to a bunch of different servers under a bunch of different ids - sometimes my own, sometimes an anonymous id. So what this application does is provide a menu of servers to login into - which saves me from having to type a lot or set up/remember a bunch of aliases. After selecting a number, the application calls (with exec()), "rlogin <server_name> -l <user_id>". Here's what the code looks like with the implementation of tye's suggestion (I had to made one minor change in the regex - '@\$' instead of '@$'. With the original one, I was getting '${$user_value}' instead of '{user_value}').
####################################################### # Setup variables and packages ####################################################### my $user = $ENV{USER}; my %servernames = (); my $i = 1; ####################################################### # MAIN ####################################################### open(CONFIG, "<lgn.config"); chomp(my @lines = <CONFIG>); close CONFIG; foreach my $line(@lines) { # Add the -l between "server user_name" pattern in $line $line =~ s/\s/ \-l /; # Interpolate the value for "$" values $line =~ s/([@\$]\w[\w\[\]{}']+)/'"'.$1.'"'/gee; $servernames{"$i"} = $line; $i++ } print "***Log in****\n"; print "Select the number of a server and user name combination\n"; foreach my $key(sort{ $a <=> $b }(keys %servernames)) { print "\($key\) $servernames{$key}\n"; } print ": "; chomp(my $ans = <STDIN>); if (($ans eq "") || ($ans =~ m/\D+/g)) { die "Please specify a number\n"; } if (defined($servernames{$ans})) { my $rlogin = "rlogin $servernames{$ans}"; print qq(Executing "$rlogin"\n); exec("$rlogin"); } else { die "Please specify a valid number\n"; }
The config file (lgn.config) would look like:
server1 foo server2 $user server2 foo server3 $user
etc. Thanks to everyone for their help and suggestions....