The Perl Weekly Journal By Ankit Fadia
____________________________________________________________________
The Perl Weekly Journal---By
Ankit Fadia
____________________________________________________________________
###########
#
###
######### ## #
############
###
##### ###
##### ### ##
##### ####
#####
####### ###
### #### ###
##### ##### #######
######### ###
# #### ####
#### ##### #######
########### ###
### ####
#########
####### ####
#### ###
#######
###########
##### #####
##### ### ##
#######
############# ###
## ###### #########
### #### #########
###### ####
# #### ######## #####
## ###### ### ###
###### ####
########## #### ####
######### ### #####
##### ####
########## ### ###
####### #### ######
#############
########## ## ##
##### #### ####
###########
######## ## ##
### ### ##
#######
# # #
#
########## ##
## ## ##
### ###
## ## ###
###
### #
### ### ##### ####
Black Sun Research Facility
###
## ## ###### ####
http://blacksun.box.sk
###
## ## ####### ####
ASCII By : cyRu5
# ###
### ### #### #######
### #### ####
#### ### #####
########### ###########
## ###
######### #######
# #
You cannot become a good hacker
unless you have some programming knowledge. Not only for Hacking, Perl
is very much useful for developing security related and also normal but
useful programs.In the Perl Weekly Journal I will be starting from the
basics of Perl and then will move on to some cool advanced stuff.I am assuming
that you do not have any previous programming experience, although a sound
background in C, Basic or JavaScript will help you tremendously.
Perl : The Basics
Perl was born in 1987 and was
developed by Larry Wall by fusing the Unix utility awk with a system administartion
tool he had developed. Perl's development has been done on the lines of
including all the useful and important aspects of other programming languages
and remove the not so useful aspects. Now Perl is an interpreted language,that
means that the Perl code is run as it is and it is not complied like other
languages.When you first run a Perl program, it is first compiled into
a bytecode, which is then converted into machine instructions.
Now first of all, before you
can start writing your own Perl programs, you need ActivePerl the Perl
Interpreter.You can download ActivePerl for Win32 from:
http://www.activestate.com/
Follow the links for the latest
build and download it.It is around a 5MB download. After installing ActivePerl
ensure that the file perl.exe is in your path statement.Although ActivePerl
Build 509 sets the path automatically during setup, just make sure that
your path statement contains reference to the file perl.exe by typing "set"
at the command prompt(without quotes), now look for the "PATH" environment
variable and make sure that it contains the line "c:\perl\bin" in that
statement.Normally it would contain this line, but if it doesn' then open
the file c:\autoexec.bat in Notepad and add the following line:
PATH=%PATH%;.;c:\perl\bin
Now save the file and reboot
or update the environment for that session by running the file autoexec.bat
by going to DOS and typing autoexec.bat
Note:Nt users will just have
to update the current system environment by going to
Control Panel > System
Now let's start by the obligatory
Hello World Program.Now to write Perl programs you do not need any special
Perl Text Editor, NotePad would do just fine. So launch Notpad and type
the following:
Print "Hello World\n"; #This
prints Hello World on the Screen
Now save the file by the name
"first.pl". You can replace first by any name of your choice but just remember
that the file should have a .pl extension. Now go the DOS Prompt and then
to the folder in which you had saved the above file and type:
C:\myfiles>first.pl
Note: Replace filename with
the name of the file that you chose while saving.
If the above program does not
work that is you get an error, then check your path statement or try to
write perl filename.pl instead of just filename.pl
Now lets analyse the program,the
word print calls the print function which takes the text from within the
quotes and displays it on the screen.The "\n" symbolises a new line or
the carriage return. Almost all lines in Perl end with a semicolon.
Scalars
Now let's make the above program
a bit more complex by introducing a scalar.
$scalarvar= 'Hello World\n'
; #the Variable $scalarvar has the value Hello World\n
print "$scalarvar" ; #Prints
value of Variable $scalarvar
Now scalars are declared by
the $ sign followed by the Variable name. The first line feeds the text
with the quotes into the scalar whose name is scalarvar.We know the scalarvar
is a scalar because it is preceeded by the $ sign.
Now you must be wondering why
I have used single quotes in the first line and double in the second. Now
the reason behind this is the fact that Perl performs variable interpolation
within double quotes this means that it replaces the variable name with
the value of the variable. This will become more understandable with the
following examples,
$scalarvar= 'Hello\n' ; # Variable
$scalarvar has the value Hello\n
print '$scalarvar' ; # But
as we use single quotes there is no variable interpolation and
function print prints $scalarvar
on the screen.
Output will be:
$scalarvar
The following is an example
of Variable Interpolation:
$scalarvar= 'Hello' ;
print "$scalarvar" ; # In this
case Variable Interpolation takes place the the Print function
is fed the value of the variable
$scalarvar.
Output will be
Hello
By now the difference between
single quotes and double quotes would have become quite clear.
Interacting with User by
getting Input
The Diamond Operator i.e <
> is the Perl equivalent of the C function scanf and
the C++ function cin. It basically grabs input from the user to make a
program interactive.It will become more clear after the following example:
print 'Enter your Name:' ;
$username= <>
; #The User will enter a text which will be fed into
the scalar
print 'Hi $username' ;
Output will be:
Enter your Name: Ankit
Hi Ankit
This program will print the
text Enter your Name: on the screen and will wait for user input.
The text entered by the user
will be fed into the scalar $username. Then the program will print Hi followed
by the text entered by the User.
chomp( ) and chop( )
Now sometimes you need to manipulate
strings and do this there are many functions available which can be used.
So when do you need to use chop( ) and chomp( ) Consider the following
situation厖You need to write a program to print the name and age of the
user which would be input by the User itself. Now consider the following
code