And you're complaining about the code I gave? I can't wait for the day you create a user name, password, database name or server name that's a subset of a user name, password, database name or server name (which doesn't seem unlikely).
The direct equivalent of:
database=...
user=...
PASSWD=`cat password_file | grep $database | grep $user | cut -d: -f2`
is
my $database = '...';
my $user = '...';
my $PASSWD = '';
open(my $fh, '<', 'password_file') or die("open: $!\n");
while (<$fh>) {
chomp;
$PASSWD = (split /:/)[2] if /$database/ && /$user/;
}
Not bad. Replacing 4 entire programs with 5 statements. (Although cat is totally useless in your code.)
If you want an equivalent drop-in replacement, you can use
PASSWD=`perl -e"chomp; print((split /:/)[2]) if /$database/ && /$user/
+" < password_file`
If you want a better drop-in replacement, you can use
PASSWD=`perl -F: -lane'print $F[1] if $F[2] eq $ARGV[0] && $F[0] eq $A
+RGV[1]' $database $user < password_file`
|