运行时出现Perl错误

时间:2015-09-22 20:20:05

标签: perl

我一直致力于管理用户和密码列表的perl程序。它包含以下特定功能:添加用户,修改用户,生成用户列表以及删除用户。

然而,我遇到了几个错误,我希望你们能帮助我修复。

以下是错误

C:\Users\mte>perl C:\Users\mte\Desktop\org11.pl
 Useless use of lc in void context at C:\Users\mte\Desktop/org22.pm line 25.
 Useless use of private variable in void context at          
 C:\Users\mte\Desktop\org11.pl
 line 16.
 Useless use of private variable in void context at     
 C:\Users\mte\Desktop\org11.pl
 line 18.
 Useless use of a variable in void context at C:\Users\mte\Desktop\org11.pl    
 line
 72.
 Name "main::userNameStorage" used only once: possible typo at    
 C:\Users\mte\Desktop\org11.pl line 72.
 Name "main::nameofile" used only once: possible typo at    
 C:\Users\mte\Desktop\org11.pl line 23.
 what is the name of the filename:
 matthew
 Use of uninitialized value $nameofile in scalar chomp at    
 C:\Users\mte\Desktop\org11.pl line 23, <> line 1.
 Unsuccessful stat on filename containing newline at    
 C:\Users\mte\Desktop\org11.pl line 25, <> line 1.
 readline() on closed filehandle $filehandle at   
 C:\Users\mte\Desktop\org11.pl >line 38.

 User Accounts
 -------------
 i = Insert new user account
 m = modify existing user account
 r = Remove existing user account
 g = Generate list of accounts
 q = Quit

 Enter Choice:i
 Undefined subroutine &main::insertUser called at   
 C:\Users\mte\Desktop\org11.pl line 85, <> line 2.

org22.pl 的源代码

#!/bin/perl
use lib "C:\\Users\\mte\\Desktop";

use org22;
#use strict;
use warnings;

$filename;
$filehandle;
$nameoffile;

%useroptionsfunction = (
    'i' => \&insertUser,
    'm' => \&modifyUser,
    'r' => \&removeUser,
    'g' => \&generateList
);

$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 = ();

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";

$userNameStorage;

#insert new user account interface

print( "Enter Choice:" );
$input = <>;
chomp( $input );

if ( $input ne 'q' ) {

    if ( $input eq "i" ) {
        $command          = $useroptionsfunction{$input};
        %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 );

    }

}

if ( $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;

org22.pm

的源代码
package org;
use strict;
use Exporter;
use warnings;

my $userNameStorage;
my $insertUserName;
my $newpassword;
my %temporaryhash;
my $userName;

use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = ( "insertUser", "modifyUser", "removeUser", "generateList" );

sub insertUser {

    my $grabparameter = shift;
    my %temporaryhash = %$grabparameter;

    print "Enter Username:";
    my $insertUserName = <>;
    chomp( $insertUserName );

    lc( $insertUserName );
    $insertUserName =~ s/[^a-zA-Z0-9]//g;

    if ( exists( $temporaryhash{$insertUserName} ) ) {

        print "user already exists";
        return %temporaryhash;

        # referenceing whoel hash use percent, when refereing part of the
        # hash use $
    }

    $userNameStorage = $insertUserName;

    #$userNameStorage = ($input);

    print "Enter Password:";
    my $insertUserPassword = <>;
    chomp( $insertUserPassword );

    $userNameStorage = ( $insertUserPassword );
    chomp( $insertUserPassword );

    $insertUserPassword =~ s/\'//g;
    $temporaryhash{$insertUserName} = $insertUserPassword;

    # if the user name doesnt exist, get a password for it, then load key
    # into hash, where key is the user name and the value is the password

    return %temporaryhash;

}

sub modifyUser {

    my $grabparameter = shift;
    my %temporaryhash = %$grabparameter;

    print "Username to modify:";
    my $modifyUsername = <>;
    chomp( $modifyUsername );

    my $modifyUserName =~ s/[^a-zA-Z0-9]//g;
    if ( exists( $temporaryhash{$modifyUsername} ) )

      # checking if username exists, and if it does, then prompt for
      # password

    {

        print "password:, " . "\n";
        my $oldpassword = <>;
        chomp( $oldpassword );

        if ( $oldpassword eq $temporaryhash{$modifyUserName} )

        {
            print "new password: ";

            $newpassword = <>;
            chomp( $newpassword );
            $newpassword =~ s/\'//g;

            return %temporaryhash;

        }

        print "Invalid password";
        return %temporaryhash;

    }

    print "user does not exist";

    return %temporaryhash;

}

sub removeUser {

    my $grabparameter = shift;
    my %temporaryhash = %$grabparameter;

    print "User to remove:";
    my $removeuser = <>;
    chomp( $removeuser );

    if ( exists( $temporaryhash{$removeuser} ) ) {
        delete( $temporaryhash{$removeuser} );
        return %temporaryhash;
    }
    print "user does not exist";

    return %temporaryhash;

}

sub generateList {

    my $grabparameter = shift;
    my %temporaryhash = %$grabparameter;

    for my $userName ( keys %temporaryhash ) {

        print "----------------------------", "\n";
        print "List of User Accounts",        "\n";
        print "\n";
        print "$userName: $temporaryhash{$userName}";

        print $userName. ":" . $temporaryhash{$userName} . "\n";

        #print user name and passowrd, then colon, then it will print the associated
        value with the username

    }

    return %temporaryhash;
}

1;

0 个答案:

没有答案