[置顶] 注册新用户(perl)

#!/usr/bin/perl
use strict;
use warnings;
use Term::ReadPassword;  #隐藏密码

=pod 注册用户 passwd file ./switch 是存放用户名密码的数据文件 数据文件的格式:用户名 密码 =cut

print "\n\nWelcome to Jackm's word\n\n";
my (%user_info,$passwd,$re_passwd);
open(WL,'>>','./switch'); #写入新的用户和密码的信息 
sub check_info {
   open(DL,'<','./switch');#读取用户数据文件用于检索新用户是否存在于数据库中
   while(<DL>)
   {
      my ($user,$pass)  = split /\s+/;
      $user_info{$user} = $pass;
   }
}
LINE:while(1)
{
  &check_info();
  print "Please enter your new name: ";
  chomp(my $new_user = <STDIN>); 
  if($new_user =~ /^\d+/ or $new_user =~ /^\s+/)
  {
     print "The user name cannot be in digital or blank characters as the beginning\n";
     redo;
  }

  unless(exists $user_info{$new_user}) #在现有的用户数据库不存在该用户,以用来作为新用户名
  {
     print "The '$new_user' can be used as a new user name\n";
  RE:while(1)
      {
         $passwd = read_password('passwd: ');
         $re_passwd = read_password('passwd_again: ');
         if( "$passwd" eq "$re_passwd")
         {
            print WL "$new_user $passwd\n";
            print "Your's name is: $new_user\n";
            print "Your's passwd is: $passwd\n";
            last LINE;
          }else
          {
             print "Two input password is not the same,try again\n";
             redo RE;
          }
      }
  }
  else
  {
     print "The $new_user is exists,try again\n";
     &check_info(); #刷新数据文件
  }

}

你可能感兴趣的:(perl,注册用户)