0

I have been working endlessly on perl program that manages a user and password list. It has contains the specific functions of , adding a user, modifying a user, generating a user list, and deleting a user.

However, I am running into several errors, I am hoping you guys might be able to help me fix.

Here are the errors

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.

Source Code for 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;

source code for 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;
5
  • Giving errors is good, but show us your code, this will be better for anyone Commented Sep 22, 2015 at 20:24
  • i added my quote beneath the errors? @GillesQuenot Commented Sep 22, 2015 at 20:26
  • Yes, now it's OK since your editing Commented Sep 22, 2015 at 20:29
  • 2
    Don't comment 'use strict' Commented Sep 22, 2015 at 20:30
  • 2
    Is this homework? It's terrible code. It's laid out very badly so that it's impossible to see the structure (I've reformatted it so that I could read it a little better) and you've written far too much before starting to test it. You should write just two or three lines of code at a time, then test that and add some more code only when it works properly. Disabling use strict is a very bad move. It would have saved you from many misspellings such as useraccountshash / useraccounthash. There's there's really far too much wrong with it to be able tl help you. Try again with this advice Commented Sep 22, 2015 at 21:08

1 Answer 1

2

Your script does not have a sub named insertUser because use org22; imports from the package org22, which doesn't exist, much less exports anything. You want to import from package org. Solve this by giving the file and the package the same name.

use org22;
org22.pm
package org22;

or

use org;
org.pm
package org;

Actually, since all-lowercase module names are reserved for pragmas, why don't you rename it to something more conventional.

use Org22;
Org22.pm
package Org22;

or

use Org;
Org.pm
package Org;

The warnings indicate a number of other problems, but I only have time to address the main one. Start by adding use strict; and fixing the errors it finds. Then, address the warnings. Adding use diagnostics; will try to explain the warnings to you.

Sign up to request clarification or add additional context in comments.

2 Comments

It should also be called Org22
Yeah, all-lowercase module names are reserved for pragmas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.