我似乎得到了一些我无法弄清楚的编译错误。任何帮助将不胜感激。创建程序的基本功能是为了管理用户列表。
我得到的错误是:
C:\Users\mte>perl C:\Users\mte\Desktop\org11.pl
Unrecognized escape \m passed through at C:\Users\mte\Desktop\org11.pl line 2.
Unrecognized escape \D passed through at C:\Users\mte\Desktop\org11.pl line 2.
Unquoted string "pm" may clash with future reserved word at C:\Users\mte\Desktop\org11.pl line 3.
syntax error at C:\Users\mte\Desktop\org11.pl line 3, near "use org22."
syntax error at C:\Users\mte\Desktop\org11.pl line 119, near "$usernames1 ("
syntax error at C:\Users\mte\Desktop\org11.pl line 126, near "}"
Execution of C:\Users\mte\Desktop\org11.pl aborted due to compilation errors.
这是代码。
use warnings;
use lib "C:\Users\mte\Desktop";
$nameoffile; #name of file
$filename; #the full path of the file
print "what is the name of the filename:", "\n";
$nameoffile = <>;
chomp($nameofile);
if (-e $nameoffile)
{
open ($filehandle, "<", $nameoffile);
}
else
{
open ($filehandle, ">", $nameoffile);
}
# load user accoutns from filefield;
%useraccountshash = ();
%useroptionsfunction = ('i' =>\&insert_user, 'm'=>\&modify_user,
'r'=>\&remove_user, 'g'=>\&generate_list);
while ($loadlines=<$filehandle>) # load all data into hash if the file already exists
{
# this array temporarily holds the user data while we prepare it to go to hash
@myarray = split(/:/, $loadlines); # split the line at the colon, will put username into index 0 in the array, and password in index 1
$username=$myarray[0];
$password=$myarray[1];
chomp($password);
$username=~ s/[^a-zA-Z0-9]//g;
$username=lc($username);
$password=~ s/\'//g;
# now we are putting in the user name and password into the hash , array to va, variiables, variables to hash
$useraccounthash{$username} = $password;
}
#user account interface
print "\t","\n","User Accounts","\n";
print "-------------","\n";
print "i = Insert new user account","\n";
print "m = modify existing user account","\n";
print "r = Remove existing user account","\n";
print "g = Generate list of accounts","\n";
print "q = Quit","\n","\n";
@userAccounts = ();
$userNameStorage;
#insert new user account interface
print("Enter Choice:");
while ($input != 'q')
{
while($input = <>) {
chomp $input;
if ($input eq "i"){
$command=$useroptionsfunction{i};
%useraccountshash=$command->(\%useraccountshash);
}
#modify username and password interface
elsif ($input eq "m"){
$command=$useroptionsfunction{m};
%useraccountshash=$command->(\%useraccountshash);
}
#remove the user interface
elsif ($input eq "r"){
$command=$useroptionsfunction{r};
%useraccountshash=$command->(\%useraccountshash);
}
#generate list of accounts
elsif ($input eq "g"){
$command=$useroptionsfunction{g};
%useraccountshash=$command->(\%useraccountshash);
}
elsif ($input eq "q"){
print "Quitting Program...";
}
}
}
close ($filehandle);
print "save changes? type, y or n";
$userinput=<>;
chomp($userinput);
if ($userinput eq 'y')
{
open ($filehandle, ">", $filename)
for $usernames1 (keys %useraccounthash)
{ # used to iterate through the hash and pull out usernames
print $filehandle "$usernames1:$useraccountshash{$usernames1}\n"; #goes
through keys and writes the key and value to file
}
close ($filehandle);
}
1;
答案 0 :(得分:4)
第一个问题是这一行。
use lib "C:\Users\mte\Desktop";
在Perl中,\
是一个转义字符。它还用于\n
(换行符)等特殊字符。 Perl将\U
和\m
以及\D
视为特殊字符。虽然有\U
,但它不了解其余部分。
为避免这种情况,您需要转义转义字符。
use lib "C:\\Users\\mte\\Desktop";
syntax error at C:\Users\mte\Desktop\org11.pl line 119, near "$usernames1 ("
这是由于前一行中缺少分号导致Perl认为open
和for
循环都是一个语句。 Perl并不擅长检测缺失的分号;如果语法错误令人费解,请检查上一行。
其他错误消息指的是您未发布的代码。