Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.
mad noob perl skills
Hm..misleading title, i just cracked open the perl book these past nights, and was wondering if anyone had any pointers/better ways to code something, something i used wrong, etc:
format login =
=========================================
LOGIN V. 1.2!!!
=========================================
.
$~ = "login";
write;
print("Username: \n");
chop($username = <STDIN>);
print("Password: \n");
chop($password = <STDIN>);
print("I.D.: \n");
chop($id = <STDIN>);
if ($username !~ /<>{}\[\]`'"/ && $password !~ /<>{}\`[\]'"/ && $id =~ /[0-9]/) {
if ($username eq "nights" && $password eq "shadow" && $id eq "666") {
if (-e "outfile.txt") {
yes: $ask = print "\nWhat would you like to do to the file? (+r/+w/+a)\n";
chop($answer = <STDIN>);
if ($answer =~ /\+[Ww]/) {
open (file1, ">outfile.txt");
print "\nWhat would you like to overwrite the file with?\n";
$in1 = <STDIN>;
print file1 ("$in1");
print "\nSuccessfully written!";
$yesno = <STDIN>;
if ($yesno =~ /[Yy]+es/) {
goto yes;
}
} elsif ($answer =~ /\+[Aa]/) {
open (file1, ">>outfile.txt");
print "\nWhat would you like to add to the file?\n";
$in1 = <STDIN>;
print file1 ("$in1");
print "\nSuccessfully added!";
$yesno = <STDIN>;
if ($yesno =~ /[Yy]+es/) {
goto yes;
}
} elsif ($answer =~ /\+[Rr]/) {
open (file1, "outfile.txt");
@read1 = <file1>;
print "\n", (@read1);
print "\nRun another command? (yes/no)\n";
$yesno = <STDIN>;
if ($yesno =~ /[Yy]+es/) {
goto yes;
}
} else {
print "Invalid Choice!";
}
} else {
die "File is invalid!";
}
} else {
print "Invalid Login!";
}
} else {
print "Invalid character(s)!";
}```
Also, is there a better way to not care about capitalization? I used:
```markup$answer =~ /\+[Ww]/```
but was wondering if there's a better way.
And...another also, is there any good books on perl, i've been using "Sam's teach yourself perl"
If you want to do case insensite matching, use the 'i' modifer:
$answer =~ /\+[w]/i
I'll post a full code review along with tips and the like tommorow, as I'm going to go eat dinner now. Feel free to hit me up on AIM or via email (or IRC) for help. I'm always glad to see people starting to code Perl. :)
Here's your code with my comments:
#!C:\perl
# You should always use lexical variables, and warnings.
# use strict;
# use warnings;
format login =
=========================================
LOGIN V. 1.2!!!
=========================================
.
$~ = "login";
write;
# That's cumbersome. Why not:
# print qq(=========================================
# LOGIN V. 1.2!!!
# =========================================\n);
# and then you don't have to fumble around with $~.
print("Username: \n");
# You don't need parens here.
# print "Username: ";
# Also, notice that that doesn't print a \n, so they
# can type it on the same line.
chop($username = <STDIN>);
# since we're getting a \n, use chomp()
# chomp(my $username = <STDIN>);
print("Password: \n");
# Same as the above.
chop($password = <STDIN>);
print("I.D.: \n");
chop($id = <STDIN>);
if ($username !~ /<>{}\[\]`'"/ && $password !~ /<>{}\`[\]'"/ && $id =~ /[0-9]/)
{
if ($username eq "nights" && $password eq "shadow" && $id eq "666") {
# Regex this?
if (-e "outfile.txt") {
yes: $ask = print "\nWhat would you like to do to the file? (+r/+w/+a)\n";
chop($answer = <STDIN>);
if ($answer =~ /\+[Ww]/) {
open (file1, ">outfile.txt");
# open(FILE, '>', "outfile.txt") or die "open(): error: Can't create outfile.txt#.\n";
# File handels (not sockets), should always be in all caps, and don't need to
# start with a '$'.
print "\nWhat would you like to overwrite the file with?\n";
$in1 = <STDIN>;
# Good job, you realized that you needed to keep the newline, so you
# didn't use chomp() or chop().
print file1 ("$in1");
# print FILE $in1;
# You don't need to quote scalars, arays, or hashes.
print "\nSuccessfully written!";
$yesno = <STDIN>;
if ($yesno =~ /[Yy]+es/) {
goto yes;
# Never, ever ever use goto. Make a subroutine.
}
} elsif ($answer =~ /\+[Aa]/) {
open (file1, ">>outfile.txt");
# open(FILE, '>>', "outfile.txt") || die "open(): error: Can't open() outfile.txt.\n";
# The reason I call open() like this is to avoid exploitation due to variable
# interpolation, and in some circumstances, it's psssible to change the open
# type (read, write, or append) based on what the first character of the file
# is.
print "\nWhat would you like to add to the file?\n";
$in1 = <STDIN>;
print file1 ("$in1");
# Same deal as above.
# print FILE $in1;
print "\nSuccessfully added!";
$yesno = <STDIN>;
if ($yesno =~ /[Yy]+es/) {
goto yes;
}
} elsif ($answer =~ /\+[Rr]/) {
open (file1, "outfile.txt");
# You should always specficially say that you're reading.
# open(FILE, '<', "outfile.txt") or die "open(): error: Can't read outfile.txt"\n";
@read1 = <file1>;
print "\n", (@read1); # Interesting. There are better ways to do this, but
# I won't nitpick.
# Also, you're not closing your file handels.
# close(FILE);
print "\nRun another command? (yes/no)\n";
$yesno = <STDIN>;
if ($yesno =~ /[Yy]+es/) {
goto yes;
}
} else {
print "Invalid Choice!";
}
} else {
die "File is invalid!";
}
} else {
print "Invalid Login!";
}
} else {
print "Invalid character(s)!";
}
Here's my version and it's output:
[n3w7yp3@localhost tmp]$ perl nights_shadow2.pl
=========================================
LOGIN V. 1.2!!!
=========================================
Username: nights
Password: shadow
ID: 666
Can't find outfile.txt
[n3w7yp3@localhost tmp]$ touch outfile.txt
[n3w7yp3@localhost tmp]$ perl nights_shadow2.pl
=========================================
LOGIN V. 1.2!!!
=========================================
Username: nights
Password: shadow
ID: 666
What would you like to do to the file?
(r)ead, (w)rite, (a)ppend, or (q)uit? r
What would you like to do to the file?
(r)ead, (w)rite, (a)ppend, or (q)uit? w
Line to write: blah blah blah text
What would you like to do to the file?
(r)ead, (w)rite, (a)ppend, or (q)uit? r
blah blah blah text
What would you like to do to the file?
(r)ead, (w)rite, (a)ppend, or (q)uit? a
Line to append: test test test
What would you like to do to the file?
(r)ead, (w)rite, (a)ppend, or (q)uit? r
blah blah blah text
test test test
What would you like to do to the file?
(r)ead, (w)rite, (a)ppend, or (q)uit? q
[n3w7yp3@localhost tmp]$
And the actual code:
#!/usr/bin/perl
use strict;
use warnings;
print qq(
=========================================
LOGIN V. 1.2!!!
=========================================
);
print "Username: ";
chomp(my $username = <STDIN>);
print "Password: ";
chomp(my $password = <STDIN>);
print "ID: ";
chomp(my $id = <STDIN>);
if($username =~ /^nights$/ and $password =~ /^shadow$/ and $id == 666)
{
die "Can't find outfile.txt\n" unless -e 'outfile.txt';
while(1)
{
print "What would you like to do to the file?\n";
print "(r)ead, (w)rite, (a)ppend, or (q)uit? ";
chomp(my $answer = <STDIN>);
last if $answer =~ /^q$/i;
if($answer =~ /^r$/i) # Note how it's case insensitive.
{
open(FILE, '<', "outfile.txt") or die "open(): error: Can't read from outfile.txt\n";
while(defined(my $line = <FILE>))
{ # only read data into an array when you have to make
# batch modifications to it frist.
print $line;
}
close FILE;
}
elsif($answer =~ /^w$/i)
{
open(FILE, '>', "outfile.txt") or die "open(): error: Couldn't open outfile.txt for writing.\n";
print "Line to write: ";
my $line = <STDIN>;
print FILE $line;
close FILE;
}
elsif($answer =~ /^a$/i)
{
open(FILE, '>>', "outfile.txt") or die "open(): error: Couldn't open outfile.txt.\n";
print "Line to append: ";
my $line = <STDIN>;
print FILE $line;
close FILE;
}
else
{
print "Invalid selection.\n";
}
}
}
# EOF
HTH