- Create the subs that will be on the code.
sub change {my($a) = @_;chdir "$a" or die "ERROR: $!";}subis a subroutine/function declaration. This code can function by typing eitherchange("test")or&change("test").myis 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.chdiris another word for "change directory". A substitute issystem "cd $a", in this case.orare logical operators withtrueandfalseas the "answers".dieis 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>);printis a word meaning to print data.chompis a function to chop of the newline that the nasty<STDIN>leaves after input. You could also use thechopcommand.
- Add an "if" command.
if($a =~ /register/i) {# Stuff goes here...}ifis abooleancondition block withtrueorfalse.=~means to compare to a regular expression operator. If you use UNIX/LINUX or something similar, the grep command should be similar./register/iis the text that the regular expression should compare to. The/means to start, the wordregisteris the code the operator will compare to, and the second/means to end. The letterimeans 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";changeis 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 NAMEorprint PASSWORDis 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 thechangewe added to go back a directory. The command for going back is "..".\nrepresents 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";}elseis if theiftest 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
11 years ago





1:09 PM
Ivan Ananda Harsono
Posted in:
0 comments:
Post a Comment