What's New!

Chat with
Hackers

How to Defend
Your Computer 

The Guides
to (mostly) 
Harmless Hacking

Happy Hacker 
Digests (old stuff) 

Hacker Links 

Hacker
Wargames 

Meet the 
Happy Hacksters 

Help for 
Beginners 

Hacker 
Bookstore 

Humor 

It Sucks 
to Be Me!

How to Commit
Computer Crime (not)! 

What Is a 
Hacker, Anyhow? 

Have a 
Great Life! 

News from the 
Hacker War Front

Carolyn's most
popular book,
in 4th edition now!

For advanced
hacker studies,
read Carolyn's
Google Groups
Subscribe to Happy Hacker
Email:
Visit this group

About the free unices inquiry.. whatever you first use you'll likely be
hooked on so make the choice wisely. I'd recommend redhat for newbies: the
new 6.1 installation is a breeze (it's in X for cryin' out loud).
Slackware is a hardcore linux that's good for servers. It's intel only.
FreeBSD is an intel and alpha unix that had a freakin' *fast* tcp stack.
It's a great server, but hard to install. OpenBSD is the most secure and
least buggy of them because there are constantly people pouring over their
code looking for security holes and general software bugs. They've had
things fixed before they even knew they were fixing security bugs. That's
very cross platform: you can burn a cd and run it on your dreamcast and
when the cablemodem comes out you can make it a full-blown yet unhackable
server (if you really want to burn a new iso for each update). I run it on
an old sparcstation of mine.

Silvakow <silvakow@earthling.net>

-----------------------------------------------------------------------

Editor: Thanks a lot everyone!

***********************************************************************
*** Perl 101, Lesson 2
***********************************************************************

Last week, we left off with:

if ($planetname eq "Earth")
{
print("Hello World!\n");
}
else
{
if ($planetname eq "Mars")
{
print("Hello, Mars. Have you seen our spacecraft?\n");
}
else
{
print("Hello $planetname\n");
}
}

and the problem was what was wrong with this code. Before I get to that,
someone wrote in to ask why I didn't use the elsif statement. Answer:
2:30am. You're right, the best way to have written this would be

if ($planetname eq "Earth")
{
print("Hello World!\n");
}
elsif ($planetname eq "Mars")
{
print("Hello, Mars. Have you seen our spacecraft?\n");
}
else
{
print("Hello $planetname\n");
}

Note that the if, elsif, and else statements are all followed by a block of
statements, which is denoted by a set of statements in curly bracket {}.
Anyway, on to the problem. The error would've been that if the user type
"earth" instead of "Earth", the program would not have responded correctly
(well, it would have been correctly, but not what was intended :) ). Since
Unix is case sensitive, the only acceptable string would be Earth.
Therefore, we need to use a regular expression. I don't want to go into
detail here on regular expressions, if you use Unix, you should be half
decent with writing and reading re's, especially to use a tool like grep. By
replacing

eq "Earth"

with

=~ /Earth/i

our problem is solved. All regular expressions are enclosed in slashes, and
the i following the concluding slash means case (I)nsensitive. The
difference in eq and =~ is that eq is an exact comparison, and =~ just does
a compare. Ok, if you thought that was obfuscated, it was, deliberatly. I'm
not sure _exactly_ what =~ does. Moving along...

Next statement. The unless statement is used exactly like an if, except it
means negative.
so

unless $planetname eq "Earth"

is the same as

if $planetname ne "Earth"

The ne means not equal, and is the opposite (logically) of the eq operator.
Like if, unless is followed by a block of statements in curly brackets.

Arrays
======

All scalar variables begin with $. There is another type of variable we're
going to use now, called an array variable. Array's, used similar to those
in C, are defined by @name = (element0, element1, element2, etc); . So to
make an array of the planet names, you could type:

@planetarray = ("Mercury", "Venus", "Earth", "Mars", "Jupitar", "Saturn",
"Uranus", "Neptune", "Pluto");

Quick note on whitespace. Like C, whitespace (newlines, tabs, spaces) is
usually irrelevant. You could've written the previous as

@planetarray=("Mercury","Venus","Earth","Mars","Jupitar","Saturn","Uranus","
Neptune","Pluto");

or even

@planetarray =
( "Mercury" ,
"Venus", "Earth", "Mars",
"Jupitar", "Saturn","Uranus", "Neptune", "Pluto");

It's a matter of personal style (or in the last case, a lack of style).
Similar to arrays in C, the first elements is refered to be the index number
0 (not 1), so the element mercury would be defined as $planetarray[0], and
neptune would be $planetarray[7]. But wait a minute... didn't I say an array
began with a @? The answer is that an array begins with @, but since
planetarray[index] refers to a scalar (b/c scalars are numbers or strings),
the reference to the individual element is a $. Quick shortcut: if your
array is going to consist of only single word strings (like our
planetarray), instead of writing all the quotes, you can write

@planetarray = qw(Mercury Venus Earth Mars Jupitar Saturn Uranus Neptune
Pluto);

I'm not sure what qw stands for (QaWote? Quote Words? QED?), but it takes
each elements, sticks some quotes around it, and makes an array. Use it, qw
is your friend.

We could use this to modify our program such as follows:

print "What your planet's name? ";
$planetname = <STDIN>;
chomp ($planetname);

$index=0;
if ($planetname eq $planetarray[$index])
{
if ($index == 3) #This is Mars, the 4th planet with an index of 3
{
print("Hello, Mars. Have you seen our spacecraft?\n");
}
else #it's not Mars, but still in the solar system
{
print("Hello, Neighbor from ", $planetarray[$index], ".\n");
}
}
else #not in the solar system
{
print("Hi. Where exactly is ", $planetname, " located?\n"");
}

Note that the # can be used to begin a single line comment. I use a new
thing in the print function here... remember, print can take in any (series
of) scalar variable(s), so you can give it either: a) a string/number or b)
a variable that evaluates to a string/number. Since $planetname is a scalar
variable, the statement

print($planetname)

is perfectly legitamate. If you'll ask why I used $planetname once and
$planetarray[$index] once, it was just to show that they are identical.
(BTW: this is not strictly necessary, perl can expand a variable inside a
string, but until we get to doublequote vs. singlequote, this way is much
simpler.)

Ok... our last topic for today... the final kind of variable: hashes. A hash
is a two column array, in which the second column, the "data", is referenced
by the first column, the "key". What does this mean? Well, let's try an
example.

%planethash=(
"Mercury", "Hi Merc. Are you related to Merck Chemical? Haha!",
"Venus", "Ever met John Gray?",
"Earth", "Third Rock, eh?"
"Mars", "Ever met John Gray?",
"Jupitar", "Do you really jump?",
"Saturn", "Sega!",
"Uranus", "I think you know the joke...",
"Neptune", "Nothing interesting to say about you. Sorry, pal."
"Pluto", "Have you sued Walt for copyright infringement?");

Ok, this hash contains the names of the planets as keys, with a not-so-witty
comment as the data. By typing $planethash("Mercury"), the computer will
return "Hi Merc. Are you related to Merck Chemical? Haha!". Therefore, an
array can be viewed a hash with the keys being 0 to length-1. So our
@planetarray could have been written:

%planetarrayashash=(
0, "Mercury",
1, "Venus",
2, "Earth",
3, "Mars",
<rest snipped out>
8, "Pluto");

Well, that about does it for today. Next time we'll continue with some more
perl concepts, probabaly disk I/O, and then I think I'll go back and discuss
all these concepts, beginning with what _exactly_ a scalar variable is, in
depth. Happy Perl-ing!
-----------------------------------------------------------------------

This is a list devoted to *legal* hacking! If anyone plans to use any
information in this Digest or at our Web site to commit crime, go away! We
like to put computer criminals behind bars where they belong!

 

 © 2013 Happy Hacker All rights reserved.