Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!usr/bin/perl -w
- print("Hello, what is your name?\n");
- $name = <STDIN>;
- chomp($name);
- print("What is the password?\n");
- $password = <STDIN>;
- chomp($password);
- if(valid_user($name, $password) && ($name =~ /^foo\b/i))
- {
- print("Welcome, Foo - is there anything you want me to do?\n");
- print("Your options are: r - read | w - write | c - clear\n");
- get_option();
- # At the moment, the commented bit doesn't work (the subs are below)
- } elsif(valid_user($name, $password)){
- print("Welcome, $name\n");
- } else {
- print("You are not registered on this system. Would you like to create a new user?\n");
- print("Y - yes/ N - no\n");
- $input = <STDIN>;
- chomp($input);
- if($input =~ /^Y\b/i)
- {
- new_user();
- }
- }
- sub valid_user
- {
- my($user,$pword) = @_;
- open(my $file, "<", "wordslist.txt");
- $done = 0;
- while(<$file>)
- {
- my($handle,$passw) = split(' ');
- if(($user eq $handle) && ($pword eq $passw))
- {
- $done = 1;
- last;
- }
- }
- close($file);
- return($done);
- }
- sub new_user
- {
- print("What is your desired username?\n");
- $newuser = <STDIN>;
- chomp($newuser);
- if(original_user($newuser))
- {
- create_user($newuser);
- } else {
- print("Your username is already in use.\n");
- }
- }
- sub original_user
- {
- my $searchterm = $newuser;
- $original = 1;
- open(my $file, "<", "wordslist.txt");
- while(<$file>)
- {
- my($row1, $row2) = split(' ');
- if(($searchterm eq $row1) || ($searchterm eq $row2))
- {
- $original = 0;
- last;
- }
- }
- close($file);
- return($original);
- }
- sub create_user
- {
- my $user = $newuser;
- print("What password do you want to use?\n");
- my $same = 0;
- while(!$same)
- {
- my $newpass = <STDIN>;
- chomp($newpass);
- print("Please type your password again for verification.\n");
- my $temp = <STDIN>;
- chomp($temp);
- if($temp eq $newpass)
- {
- open(my $file, ">>", "wordslist.txt");
- print $file("$user $newpass\n");
- close($file);
- $same = 1;
- } else {
- print("Sorry, your passwords didn't match. Please type your password again:\n");
- }
- }
- }
- sub get_option
- {
- my $option = <STDIN>;
- if($option =~ /^r\b/i){
- read_file();
- } elsif($option =~ /^w\b/i){
- write_to_file();
- } else {
- clear_file();
- }
- }
- # This produces the error: Use of uninitialized value $line in print at Login.pl line 147, <$file> line 1.
- sub read_file
- {
- open(my $file, "<", "wordslist.txt");
- while(<$file>)
- {
- my $line = <$file>;
- print STDOUT $line;
- }
- close($file);
- get_option();
- }
- # This works.
- sub write_to_file
- {
- open(my $file, ">>", "wordslist.txt");
- $do = 1;
- while($do == 1)
- {
- print STDOUT "What do you want to write?\n";
- $write = <STDIN>;
- print $file $write;
- close($file);
- print STDOUT "Do you want to write again? Y - yes / N - no\n";
- $do = <STDIN>;
- chomp($do);
- if($do =~ /^Y/i){
- $do = 1;
- } else {
- $do = 0;
- }
- }
- get_option();
- }
- # This sub works out of the 'admin options':
- sub clear_file
- {
- open(my $file, ">", "wordslist.txt");
- while(<$file>)
- {
- print $file " ";
- }
- close $file;
- get_option();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement