我正在处理一项任务,如果他们成功登录,我必须让用户登录(检查他们的用户名/密码对文件),然后我需要打印他们的薪水数据,该数据存储在另一个名为 IN-accounting.data 的文件中。我设法让用户登录部分工作,但我无法打印“薪水”部分,因为它现在将打印会计文件中的每个人我可以使用一些帮助来弄清楚如何只打印数据用户登录有什么建议吗?这些文件和代码中的所有 ssn、电话号码、地址和人员都是假的,它们只是为完成任务而提供的数据
11-6-12 更新以反映建议的更改,它现在以我最初想要的方式运行
#!/usr/bin/perl
use warnings;
use strict;
use Digest::MD5 'md5_hex';
open (PSWD, '<', 'password.passwd');
#getting username and password
#converting username to lowercase if username is entered in CAPS
print "Please enter your username: ";
chomp(my $userN = <STDIN>);
my $username = lc($userN);
############################################
print "Please enter your password: ";
chomp(my $password = <STDIN>);
my $passwd=md5_hex($password);
###############################################
my $matchCount = 0;#used later to make sure username and password match file
#reading password.passwd and assigning values
while (my $lines = <PSWD>){
my($user,$pswd,$userID,$groupID,$info,$home,$shell) = split ':', $lines;
#checking username entered vs that in the passwd file
if ($username eq $user){
print "Checking username... MATCH\n";
$username=$info;
#keeps track if username matches or not
$matchCount+=1;
#checking password entered vs that in the passwd file
if ($passwd eq $pswd){
print "Checking password... MATCH\n";
my ($first,$last)=split(" ", $info);
accounting($first,$last);
}
else{
print "Password does not match!\n";
}
last;
}
}
# if matchcount did not change, username did not match killing the program
if ($matchCount == 0){
die ("\"$username\" does not match any users in our database!\n");
}
sub accounting{
my $first_name=shift;
my $last_name=shift;
open(my $fh, '<', 'IN-accounting.data') or die "cannot open accounting file $!";
while (my $lines = <$fh>){
chomp $lines;
my @fields = split(/\|/, $lines);
push @data2, \@fields;
my($Lname,$Fname,$ssn,$address)=($fields[0],$fields[1],$fields[2],$fields[3]);
my($city,$state,$zip,$payDate)=($fields[4],$fields[5],$fields[6],$fields[7]);
my($hours,$rate,$taxes,$deductions,$notes)= ($fields[8],$fields[9],$fields[10],$fields[11],$fields[12]);
next if $Lname ne $last_name and $Fname ne $first_name;
my ($Gpay)= eval($hours)*eval($rate);#gross pay
my ($Tpay)=$Gpay-($taxes+$deductions);#total pay
my $Essn=substr($ssn,+-4);#edited ssn
print "$Fname $Lname\n";
print "$address\n";
print "$city $state $zip\n";
print "SSN: xxx-xx-$Essn\n";
print"\n";
print "Pay Date: $payDate";
print"\n";
print"You had $hours hours at \$$rate/hour\n";
print"Gross Pay: $Gpay\n";
print"Taxes:\$-$taxes\n";
print"Deductions:\$-$deductions\n";
print"Total Pay: $Tpay\n";
print"\n";
print"Notes:$notes\n\n";
}
print"press enter to quit: ";
my $quit=<>;
if ($quit){ exit;}
}
密码.passwd
amon9640:4cb9c8a8048fd02294477fcb1a41191a:500:25:亚历山大周一:/首页/工资:/斌/工资iart1373:4cb9c8a8048fd02294477fcb1a41191a:501:25:伊尼戈Arterbury:/首页/工资:/斌/工资wher0210:4cb9c8a8048fd02294477fcb1a41191a:502:25:沃德尔赫尔曼:/home/payroll:/bin/payroll
会计档案
Monday|Alexander|815-19-9640|4662 Dewy Subdivision|Owltown|Oregon|97434-8480|1/18/1998|19|21.68|60.28|2.24|工资单正在审核帐户,报告即将到期。
Arterbury|Inigo|037-30-1373|987 Rocky Island Byway|Christmas City|New Mexico|88023-3889|4/1/1993|9|7.02|17.75|12.71|审核完成。发现不足。
Herman|Wardell|114-29-0210|5555 Cinder Forest Wynd|White Eyes Town|Washington|98707-5628|10/0/2003|37|3.07|41.90|20.89|审核完成。发现不足。