-1

I seem to be getting some compiling errors i cannot figure out. any help would be greatly appreciated. the basic function of the program was created to manage a list of users.

the errors i am getting are:

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.

Here is the code.

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;
Schwern
  • 153,029
  • 25
  • 195
  • 336
matters1776
  • 11
  • 1
  • 6
  • 3
    I see you've found warnings. You're going to want to [turn on `strict`](http://stackoverflow.com/questions/8023959/why-use-strict-and-warnings), it will save you hours of frustrating debugging time. You'll also want to indent your code. Most [good editors](http://stackoverflow.com/questions/12528241/best-ide-for-perl-5) will do this for you when you hit `tab` at the start of a line. You can also use [perltidy](https://metacpan.org/pod/distribution/Perl-Tidy/bin/perltidy) to get you started. Finally, [autodie](https://metacpan.org/pod/autodie) will find file errors for you. – Schwern Sep 21 '15 at 19:15

1 Answers1

4

The first problem is this line.

use lib "C:\Users\mte\Desktop";

In Perl, \ is an escape character. It's also used for special characters like \n (newline). Perl reads \U and \m and \D as special characters. While there is a \U, it does not understand the rest.

To avoid this, you need to escape the escape character.

use lib "C:\\Users\\mte\\Desktop";

syntax error at C:\Users\mte\Desktop\org11.pl line 119, near "$usernames1 ("

This is caused by a missing semi-colon on the previous line causing Perl to think the open and for loop are all one statement. Perl isn't good at detecting missing semi-colons; if a syntax error is puzzling, check the previous line.


The rest of your error messages are referring to code which you did not post.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • 2
    It understands `\U` fine, it just disagrees with the questioner's understanding. :) – Jim Davis Sep 21 '15 at 19:27
  • Why would you suggest "escaping the escape characters" when the OP has a simple literal string with no interpolation? Single quotes, ala `use lib 'C:\Users\mte\Desktop'` is a better practice to encourage, in my opinion. Did you perhaps suggest the escapes in this case for instructional reason? – type_outcast Sep 21 '15 at 22:53
  • 1
    @type_outcast I always forget that \ doesn't interpolate in single quotes (except \'). I tend to just spam `"` around. Let me retrofit an instructional reason... eventually they're going to want to interpolate a Windows path. – Schwern Sep 21 '15 at 23:08
  • Also, I'm pretty sure perl will happily handle windows paths with /'s instead of \'s, if you're so inclined. – elcaro Sep 22 '15 at 00:37
  • @Joshua Something like a simple filename is *probably* safe. Perl is feeding the paths to the Windows API which *almost always* accepts /. Windows programs will also probably work. See [this answer](http://superuser.com/a/176395/22550) for more detail. – Schwern Sep 22 '15 at 00:57