Perl Tutorial

Discussion in 'Web Programming' started by J++, Sep 14, 2007.

Perl Tutorial
  1. Unread #1 - Sep 14, 2007 at 4:48 AM
  2. J++
    Referrals:
    0

    J++ Guest

    Perl Tutorial

    Hello im Jay or J++. This is a quick tutorial i wrote which explains the basics of perl. Enjoy.

    !Where there appears to be gaps, there should be $input. For some reason this doesn't show up!

    Hello and welcome to my tutorial which will show you the basics of perl programming. Okay to start you will need to download a few things...

    ActivePerl which you can download from: http://downloads.activestate.com/ActivePerl/Windows/5.8/ActivePerl-5.8.8.820-MSWin32-x86-274739.zip

    Crimson Editor which i use as my text editor: http://robotics.snu.ac.kr/pds/CrimsonEditor/cedt370r.exe

    Okay now we can begin... Perl is a easy programming language to pick up and can be very useful. Once you have installed perl open your crimson editor and click on the Macros button on the top menu and click Begin Recording... Call it perl opening line and then type #!usr/bin/perl and then stop the recording. Then go back into the macros menu and click conf. user macros. Click on the macro you made and change the hotkey to Ctrl + Q or something you'll remember. Click apply and ok and try out your hotkey. If it works then we can proceed, if not then read over the instructions and try again. Now time for me to explain the reason for this hotkey. In perl you use this line in every program you write, it always goes at the start of each program to tell it where to find the perl interpreter to run the program. Instead of trying to remember it you can now just use that hotkey. Now for some basic things you should know...

    Comments - Anything with # before it (besides #!usr/bin/perl) is a comment and doesn't count as part of the program.

    Variables - If something has a $ in front of it, it is most of the time a variable. Variables are things that are likely to change and can do so easily.

    Operators - Here are some operators that are important in perl:

    == This checks if the interger you put in front and behind the equal signs are the same. eg. 12=3*4 would return true

    = This operator assigns a value to a variable. eg. $a=5 would make $a now equal 5.

    ++ This adds one interger to the variable or number you put in

    -- This subtracts one interger to the variable or number you put in

    eq This checks if the variable or text you put on either side of the equal signs are the same.

    ne This checks if the variable or text returns false or isnt equal on either sides.

    || This is an 'or' operator. eg. if(this=that || this=these)

    && This is an 'and' operator. eg. if(this=that && these=those)

    Now lets get onto how to make the all famous 'hello world' program.
    Printing text through the interpreter is very easy in perl, here is what the code looks like:

    #!usr/bin/perl
    print "Hello World";

    Simple, isnt it? You might have noticed the semi-colon at the end of the text, it is required at the end of each line of code, unless you have a different character at the end such as a '{' or similar. Try putting that code into your crimson editor and hit save. Call it test or something and make sure you choose perl file from the drop down menu or alternately just save it as .pl
    Now find where you saved it and open it up to see the anticipated interpreter which will have the words 'Hello World' printed. Yay!

    Its time to move onto the 'if' and 'then' function. This basically is when you say, "if this happens then do this, otherwise if this happens do this". Lets say we want to gain input from the user, so the person who runs the program has to type something which the program will then respond depending on their input. Lets start on the program, put in your starting line, Ctrl + Q or #!usr/bin/perl if you didn't get your macro working. now put we are going to put in the code that will tell the program to get the user input and save it into the variable $input. To do this we use the operator that will assign the user input into the variable which is one equal sign (=) so we will use this code:
    Code:
    
    $input=<STDIN>; 
    
    
    The STDIN is just the code used for keyboard input so now our code has made the variable $input equal whatever the user types in. There is one problem with this, it will also record when the user hits enter. The good news is, this is easily fixable. By adding 'chomp' in front of it, you can cut off the 'enter' input, so your new code should look like this:
    Code:
    chomp($input=<STDIN>); 
    
    Now its time to get this working with the if and then function. Here is what your code should look like.
    Code:
    #!usr/bin/perl 
    
    chomp($input=<STDIN>); 
    
    Now we can add the if statement, its simple, type:
    Code:
    if($input eq "good") { 
    print "thats good"; 
    } 
    
    This tells the program if the user types in "good" then the program will respond with, "thats good". The 'then' part of this statement is the curly bracket '{'. It is starts the function of the then part and the '}' ends the function. Now to add the elsif command. This will say now say, if this happens then this, otherwise if this happens then this.
    Code:
    elsif($input eq "bad") { 
    print "thats not good"; 
    } 
    
    Now the final part of the if statement, the 'else' statement. This is if the user input doesn't equal "good" or "bad". So type this in after the elsif:
    Code:
    else { 
    print "I dont care!"; 
    } 
    
    Now we put it all together and we have this:
    Code:
    #!usr/bin/perl 
    
    chomp($input=<STDIN>); 
    
    if($input eq "good") { 
    print "thats good"; 
    } 
    elsif($input eq "bad") { 
    print "thats not good"; 
    } 
    else { 
    print "I dont care!"; 
    } 
    

    Now we have a working script, but we are missing one thing, some text before you chomp and after your first line, type:
    Code:
    print "How are you today?"; 
    
    Now save and run your program and this should happen:

    How are you today? - If you type good then this...
    good
    thats good - If you type bad then...
    bad
    thats not good - If you type anything else then...
    weeeeeeee
    I dont care!

    Yay! You have made a fully functional, if and then responding program. I hope this tutorial has helped you.

    Tutorial made by Jay


    !Where there appears to be gaps, there should be $input. For some reason this doesn't show up!
     
< Free Website Hosting | PHP - [Tutorial] #2 - Webforms and PHP >

Users viewing this thread
1 guest


 
 
Adblock breaks this site