View difference between Paste ID: KrQQ1vvN and d3JqJgb5
SHOW: | | - or go back to the newest paste.
1
#!usr/bin/perl -w
2
3
print("Hello, what is your name?\n");
4
$name = <STDIN>;
5
chomp($name);
6
7
print("What is the password?\n");
8
$password = <STDIN>;
9
chomp($password);
10
11-
if(valid_user($name, $password) && ($name =~ /^lucy\b/i))
11+
if(valid_user($name, $password) && ($name =~ /^foo\b/i))
12
{
13
	print("Welcome, Foo - is there anything you want me to do?\n");
14
	print("Your options are: r - read | w - write | c - clear\n");
15
16
	get_option();
17
18
#	At the moment, the commented bit doesn't work (the subs are below)
19
20
} elsif(valid_user($name, $password)){
21
	print("Welcome, $name\n");
22
} else {
23
	print("You are not registered on this system. Would you like to create a new user?\n");
24
	print("Y - yes/ N - no\n");
25
	$input = <STDIN>;
26
	chomp($input);
27
28
	if($input =~ /^Y\b/i)
29
	{
30
		new_user();
31
	}
32
}
33
34
sub valid_user
35
{
36
	my($user,$pword) = @_;
37
38
	open(my $file, "<", "wordslist.txt");
39
	$done = 0;
40
41
	while(<$file>)
42
	{
43
	my($handle,$passw) = split(' ');
44
45
		if(($user eq $handle) && ($pword eq $passw))
46
		{
47
48
			$done = 1;
49
			last;
50
		
51
		}
52
	}
53
54
	close($file);
55
	return($done);
56
}
57
58
sub new_user
59
{
60
	print("What is your desired username?\n");
61
	$newuser = <STDIN>;
62
	chomp($newuser);
63
	
64
	if(original_user($newuser))
65
	{
66
		create_user($newuser);
67
	} else {
68
		print("Your username is already in use.\n");
69
	}
70
71
}
72
73
sub original_user
74
{
75
	my $searchterm = $newuser;
76
	$original = 1;
77
78
	open(my $file, "<", "wordslist.txt");
79
80
81
	while(<$file>)
82
	{
83
		my($row1, $row2) = split(' ');
84
85
		if(($searchterm eq $row1) || ($searchterm eq $row2))
86
		{
87
			$original = 0;
88
			last;
89
		}
90
	}
91
	close($file);
92
	return($original);
93
}
94
95
sub create_user
96
{
97
	my $user = $newuser;
98
	print("What password do you want to use?\n");
99
100
	my $same = 0;
101
102
	while(!$same)
103
	{
104
		my $newpass = <STDIN>;
105
		chomp($newpass);
106
107
		print("Please type your password again for verification.\n");
108
109
		my $temp = <STDIN>;
110
		chomp($temp);
111
112
		if($temp eq $newpass)
113
		{
114
			open(my $file, ">>", "wordslist.txt");
115
116
			print $file("$user $newpass\n");
117
			
118
			close($file);
119
			$same = 1;
120
		} else {
121
			print("Sorry, your passwords didn't match. Please type your password again:\n");
122
		}
123
124
		
125
	}
126
}
127
128
sub get_option
129
{
130
	my $option = <STDIN>;
131
	if($option =~ /^r\b/i){
132
		read_file();
133
	} elsif($option =~ /^w\b/i){
134
		write_to_file();
135
	} else {
136
		clear_file();
137
	}
138
}
139
140
#	This produces the error: Use of uninitialized value $line in print at Login.pl line 147, <$file> line 1.
141
sub read_file
142
{
143
	open(my $file, "<", "wordslist.txt");
144
	while(<$file>)
145
	{
146
		my $line = <$file>;
147
		print STDOUT $line;
148
	}
149
	close($file);
150
	get_option();
151
}
152
153
#	This works.
154
sub write_to_file
155
{
156
	open(my $file, ">>", "wordslist.txt");
157
158
	$do = 1;
159
160
	while($do == 1)
161
	{	
162
163
		print STDOUT "What do you want to write?\n";
164
165
		$write = <STDIN>;
166
167
		print $file $write;
168
		
169
		close($file);
170
		
171
		print STDOUT "Do you want to write again? Y - yes / N - no\n";
172
173
		$do = <STDIN>;
174
		chomp($do);
175
		if($do =~ /^Y/i){
176
			$do = 1;
177
		} else {
178
			$do = 0;
179
		}
180
	}
181
	get_option();
182
}
183
184
185
#	This sub works out of the 'admin options':
186
sub clear_file
187
{
188
	open(my $file, ">", "wordslist.txt");
189
	
190
	while(<$file>)
191
	{
192
		print $file " ";
193
	}
194
	close $file;
195
	get_option();
196
}