- Create the subs that will be on the code.
sub change {
my($a) = @_;
chdir "$a" or die "ERROR: $!";
}
sub
is a subroutine/function declaration. This code can function by typing eitherchange("test")
or&change("test")
.my
is the word of a special variable designed for only the block of code (a block is{}
). Similar in math class, a variable is a placeholder that holds bytes.chdir
is another word for "change directory". A substitute issystem "cd $a"
, in this case.or
are logical operators withtrue
andfalse
as the "answers".die
is a command that prints the message to STDERR (another name for "Standard Error"). $! and $@ represent why the command failed, and can be added todie
, as well as others.
- Remember, all commands end with a semicolon.
- Create a message that will say to either register or log in, then add code to accept input.
print "Do you want to register or log-in? "; chomp($a=<STDIN>);
print
is a word meaning to print data.chomp
is a function to chop of the newline that the nasty<STDIN>
leaves after input. You could also use thechop
command.
- Add an "if" command.
if($a =~ /register/i) {
# Stuff goes here...
}
if
is aboolean
condition block withtrue
orfalse.
=~
means to compare to a regular expression operator. If you use UNIX/LINUX or something similar, the grep command should be similar./register/i
is the text that the regular expression should compare to. The/
means to start, the wordregister
is the code the operator will compare to, and the second/
means to end. The letteri
means to use case-insensitivity, as case-sensitivity is commonly used.#
is to signify a comment. The rest of the line is commented, the text will be ignored by the compiler.{
to}
represents a block. These will be executed when called.
- Create some more input.
print "What is your name: "; chomp($name=<STDIN>);
print "What is your password: "; chomp($password=<STDIN>);
- Before proceeding on, make sure you have two folders named "name" and "password".
- Write the name and password. Remember to add this between the two curly braces!
#* change "name";
open NAME, ">$name.nme";
print NAME "$name";
close NAME;
change "..";
change "password";
open PASSWORD ">$name.pswrd";
print PASSWORD "$password";
close PASSWORD;
change "..";
print "Complete.\n";
change
is the function we wrote when we first wrote the program. The function would change directory to the name we specified.open NAME, ">$name.nme"
or open PASSWORD, ">$name.pswrd" will "open the file" on the second parameter (>$name.nme or something close), and give a "name" to it, known as filehandle. The name is the first parameter (like NAME or PASSWORD). The > sign in the files name represents "types". < and nothing are reading, > is writing (overwrites anything) and >> is appending (writes additional stuff).print NAME
orprint PASSWORD
is a command. In this case, we use the print function to print data to a filehandle, which then will be processed and (with the > and >>) added.change ".."
is using thechange
we added to go back a directory. The command for going back is "..".\n
represents a newline.
- Add the log-in block.
else {
print "Name: "; chomp($a=<STDIN>);
print "Password: "; chomp($b=<STDIN>);
change "name";
open NAME "$a.nme";
$c=<NAME>;
close NAME;
change "..";
change "password";
open PASSWORD "$a.pswrd";
$d=<PASSWORD>;
close PASSWORD;
change "..";
if($a =~ /$c/ and $b =~ /$d/) {
print "Log-in successful!\n";
sleep(2);
print "Hello, $a!\n";
}
else
is if theif
test failed. The compiler will run the block.$c=<NAME>
or$d=<PASSWORD>
means that the variable's data will be assigned to the filehandle specified.sleep(2)
means that the compiler should wait for the number specified. 2 means to wait for two "blinks" on the prompt.
The code altogether
sub change {
my($a) = @_;
chdir "$a" or die "ERROR: $!";
}
print "Do you want to register or log-in? "; chomp($a=<STDIN>);
if($a =~ /register/i) {
print "What is your name: "; chomp($name=<STDIN>);
print "What is your password: "; chomp($password=<STDIN>);
change "name";
open NAME, ">$name.nme";
print NAME "$name";
close NAME;
change "..";
change "password";
open PASSWORD ">$name.pswrd";
print PASSWORD "$password";
close PASSWORD;
change "..";
print "Complete.\n";
}
else {
print "Name: "; chomp($a=<STDIN>);
print "Password: "; chomp($b=<STDIN>);
change "name";
open NAME "$a.nme";
$c=<NAME>;
close NAME;
change "..";
change "password";
open PASSWORD "$a.pswrd";
$d=<PASSWORD>;
PASSWORD;
"..";
if($a =~ /$c/ and $b =~ /$d/) {
print "Log-in successful!\n";
2);
"Hello, $a!\n";
-
- Well done! You completed a program!
- This code can be easily hacked. Try writing the code on hex if your adventurous.
Source : www.wikihow.com
Amalan Ringan Agar Doanya Terkabul
10 years ago
0 comments:
Post a Comment