Results 1 to 7 of 7

Thread: Some Perlie Perl

  1. #1

    Post Some Perlie Perl

    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…

    print "Enter your name:" ;

    $name=<> ;

    print "Enter your age:" ;

    $age=<> ;

    print "$name";

    print "$age";

    Output will be:

    Enter your name:Ankit

    Enter your age:14

    Ankit

    14

    Now what happened here? Why did Perl print Ankit and 14 in different lines? There was no newline i.e. "\n" character in this program. Now what actaully happened lies in the fact that when the user is given the Input prompt that is when the user is needed to enter some input, Perl keeps on accepting input from the user as long as the user provides the ending operator( The ending operatort is Carriage Return or Enter). When the ending operator is provided by the user then Perl stops taking input and assigns the data input by the user to the variable specified including the Carriage Return.

    This means that in the above program the value of the scalar $name is Ankit followed by carriage return which is equivalent to "Ankit\n".

    So when we print the scalar $name then the value of $name is printed followed by carriage return or "\n".

    To avoid this problem we use chop( ) and chomp( ).

    The basic differnece between these two functions will become clearer with the below example:

    $var1="Ankit";

    chop($var1);

    print $var1;

    The Output will be:

    Anki

    In the below example:

    $var1="Ankit";

    chomp($var1);

    print $var1;

    The Output will be:

    Ankit

    Now the differnece between chop( ) and chomp( ) is that chop( ) will remove the last character of the string irrespective of the fact, what it is. While chomp( ) will remove the last charcter if and only if the last character is "/n".

    This means that

    $var1="Ankit\n";

    chomp($var1);

    print $var1;

    and

    $var1="Ankit\n";

    chomp($var1);

    print $var1;

    will have the same effect as the last character here is "\n". So our problem of printing both the name and age of the user on the same line can be solved by the following snippet of code:

    print "Enter your name:" ;

    $name=<> ;

    chomp($name);

    print "Enter your age:" ;

    $age=<> ;

    chomp($age);

    print "$name";

    print "$age";

    Output now would be:

    Enter your name:Ankit

    Enter your age:14

    Ankit14

    Courtesy : Fadia, Ankit

  2. #2
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    Don't forget to quote the source .

    http://hackingtruths.box.sk/perl1.htm
    OpenBSD - The proactively secure operating system.

  3. #3
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    smirc's right, don't just repost tutorials without quoting the source, if you're going to reproduce a tutorial then give us the link and we'll follow it from there. Anyone can copy/paste, but taking the credit for someone else's work is just plain out of order.
    Paul Waring - Web site design and development.

  4. #4
    Senior Member
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    734
    Good tutorial Ankit Fadia
    Don't neg him though, it's only one of his first posts, so he's still coming to grips with the whole system.
    I hate to see newbies getting negged of AntiOnline just because they wanted to add something helpful but ``forgot`` to quote the source.
    iz me> Only original tutorials are allowed in this forum...

  5. #5
    Junior Member
    Join Date
    Nov 2001
    Posts
    14

    Question

    Whatis path statement?Where does it belong?Downloaded www.activestate.com,file installed OK,but will not open. Any suggestions would help,am new to this,trying to learn.Thank You

  6. #6
    Senior Member
    Join Date
    Jun 2002
    Posts
    148
    to help lodebare, the path is a enviroment variable, basicaly, if you try to execute a program such as the perl interpriter just by typeing the program name and entering itinto a command line or the dialog box Start> Run , dos will first try to find the program in the current directory and if it does not find it there, it will look in each directory spesified in the path. You can invoke the perl interpriter from the command line in DOS. But first set your path as mentioned in the above tutorial, an easy way to do this is Start> Programs> Assorceries> System tools> system information, in the tools menu, configure system, then select the autoexec tab, then add a new line with your path,. As explaind above.

    You can write your perl scripts in a text editor such as notepad, when saveing them, choose save as, and in quotes "yourprog.pl" this way it gets the pl extension which, in activeperl from activestate, automaticaly pl extensions are associated with the perl interpriter. In dos type perl yourprog.pl and this will instruct the perl interpriter to execute the script named yourprog.pl

    Does that help clerify lodebare?
    In snatches, they learn something of the wisdom
    which is of good, and more of the mere knowledge which is of evil. But must I know what must not come, for I shale become those of knowledgedome. Peace~

  7. #7
    I didn't really read the whole tut, just kinda skimmed it but, I don't think I ever saw the #!/usr/bin/perl -w path to the interpreter. . .isn't that an importact part of a script?? Heh heh.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •