1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

A Basic Introduction To Perl

Discussion in 'PHP & Perl' started by WinBoot, Oct 21, 2013.

  1. WinBoot

    WinBoot Registered Member

    Joined:
    Aug 25, 2010
    Messages:
    61
    Likes Received:
    14
    A Basic Introduction To Perl

    Content table:
    Code:
    1. How should we start using Perl?
    2. Printing
    2.1 How to print text?
    2.2 How to print variables?
    3. Mathematics
    4. Statements
    4.1 If, else
    4.2 For-loop
    5. Input
    6. Example
    7. End
    NOTE: Do not copy this tutorial unless you have my permission (by mailing me, on msn or just pm)

    I wrote this to share a little of my Perl knowledge (not that i have so many of it ;)). I guess it might be educational for some interested people around here. If this introduction contains any mistakes/anything, please report.

    Note: I didn't test anything in this tutorial, it's general knowledge.

    1. How should we start using Perl?

    First of all you will have to install Perl on your server, now that you have done this, we can really start with this tutorial.

    Well as you may know, the *.pl extension is the one you will need to use for sure. Another thing that is very important is the Perl path identifying. How do we do this? Simple, the first line of our *.pl file should look similar to this:

    Code:
    #!/usr/bin/perl
    As you should have guessed by now, the /usr/bin/perl is the corresponding Perl path on your server. You should write this line whenever your creating a new Perl file.

    2. Printing

    2.1 How to print text?

    Offcourse there are several ways, but i preferr the printf command. It's easy and clean, and in a small example it may look like this:

    Code:
    printf "Hello world.";
    This will output "Hello world." in your browser. When you are willing to print out several lines, then you should use /n which stands for new line. Some other wildcards are:

    Code:
    .     Match any character
    \w    Match "word" character (alphanumeric plus "_")
    \W    Match non-word character
    \s    Match whitespace character
    \S    Match non-whitespace character
    \d    Match digit character
    \D    Match non-digit character
    \t    Match tab
    \n    Match newline
    \r    Match return
    \f    Match formfeed
    \a    Match alarm (bell, beep, etc)
    \e    Match escape
    \021    Match octal char ( in this case 21 octal)
    \xf0    Match hex char ( in this case f0 hexidecimal)
    I have taken these from http://www.troubleshooters.com/codecorn/littperl/perlreg.htm since i don't know all of them. To use them you put them in your printing line, like this:

    Code:
    printf "Hello world.\n";
    printf "My nick is CrazyLord.";
    2.2 How to print variables?

    Like any other programming/scripting language, Perl uses data types to store values and information in. The main difference between Perl and all other programming/scripting languages is that Perl auto-defines the data type.

    These are some examples of variable definitions:

    Code:
    $integervalue = 1;
    $floatvalue = 0.1;
    $charactervalue = "mycharacter";
    $booleanvalue = true;
    As you can see you don't need to define the data type before the actual variable ($<string>). It auto-detects it. Also, you can see that the $charactervalue is defined using double quotes around the actual input. You should always do this for characters. There are also cases where you should use single quotes, but since this is a beginners guide i will not give any further explenation on this.

    Now how would we actually use these variables in our printf function? Let's look at an example using the previously shown variables:

    Code:
    printf "This is an integer value: $integervalue";
    printf "This is a float value: $floatvalue";
    printf "This is a character value: $charactervalue";
    printf "This is a boolean value: $booleanvalue";
    We just use the variables definition and put it in the output function (printf) between the double quotes. Our ouput would look like this when we would execute our *.pl file:

    Code:
    This is an integer value: 1
    This is a float value: 0.1
    This is a character value: mycharacter
    This is a boolean value: true
    It simply shows the information stored in our variables, it's just that simple.

    Now knowing all this information, we wouldn't really be able to make a real program written in Perl. So what would we need next?
    Simple, let's have a look at the next title.

    3. Mathematics

    Let's take in our minds that we would want to create a calculator that takes two values, multiplies them and shows the output on our screen.
    We allready know how we would store both values and how we would output it. But what about the multiplying? Let's first have a look at since we can use:

    Code:
    +    Add two variables
    -    Substract two variables
    /    Divide two variables
    *    Multiply two variables
    These are basic well-known operations which we can use to let's say multiply two variables, which could look like this:

    Code:
    $result = 3 * 3;
    This would give $result the value of 9, we can basically do the exact same to multiply two identified variables:

    Code:
    $var_1 = 3;
    $var_2 = 4;
    $result = $var_1 * $var_2;
    This would multiply the two variables ($var_1 and $var_2) and put the result in $result.
    Related to this, we might aswell use operations which make our code smaller but make it do the exact same, possible operations like this are:

    Code:
    +=    Add the variable behind the operation to the one in front of it
    -=    Substract the variable behind the operation from the one in front of it
    /=    Divide the variable in front of the operation by the one behind it
    *=    Multiply the variable in front of the operation by the one behind it
    Using these operations, our code could possible look like this:

    Code:
    $add_value = 4;
    $basic_value = 3;
    $basic_value *= $add_value;
    This would make $basic_value contain 12 since it multiplies it's own value (3) by $add_value which contains 4. Now let's have a look at statements which will bring our knowledge up to the next level.

    4. Statements

    I'm not going to rewrite everying twice, just for another language. (Since i have allready written a similar tutorial, but for C/C++) So i guess i'll just Copy/Paste the statements in here. It should be the exact same.

    4.1 If, else

    First of all you should know that the we use # to comment out code in Perl, not like any other programming/scripting languages who mostly use /* */ and //.

    In the next topic i will show you how to write your first program based on these two statements.

    Other then the else-statement, the if-statement can stand-alone and can only be used in one way.
    The else-statement can also be used in a combination of both statements. These are the two possibilities:

    Code:
    if ( ... ) {
    }
    else {
    }
    Code:
    if ( ... ) {
    }
    else if ( ... ) {
    }
    else {
    }
    When you use the else-statement the two actions will never be executed at the same time.
    As you may have guessed, the code which should get executed when a certain action
    returns true has to be placed between the two brackets.

    So now we know where to put the action but not yet what we can do where the points are situated. This is an
    example of how you would use it with a boolean.

    Code:
    $test = false;
    if (test) {
        # test is true
    }
    else {
        # test is false
    }
    (Without the spaces) The above code is one way to check if a boolean is true or false. But to
    compare all data types there are built in signs you can use, here i give you a very smal list:

    Code:
    ==    equal to
    !=    not equal to
    >    bigger
    <    smaller
    >=    bigger or equal
    <=    smaller or equal
    4.2. For-loop

    You can use this loop when you want a certain action to get executed several times, or use it to loop
    trough several variables. This is an example of how you use it.

    Code:
    for ($i = 0;$i <= 10;$i++) {
    }
    As you can see, there are three main parts in the loop. The first one "$i=0" defines the variable
    that will change it's value. The second part "$i<=10" is where you define how far it should loop, here it
    goes from 0 to 10 since we defined the variable as 0. The last one "i++" is the actual counting up, you
    can also use "$i--" or any other action you would like.

    5. Input

    To receive input in Perl we use the command <STDIN>, let's just point this out using an example:

    Code:
    printf "Insert your value: \n";
    $myvariable = <STDIN>;
    printf "Your value is $myvariable";
    Whenever you will execute your script, it will display the text "Insert your value:" and it will "pause". Then you will be able to type in anything you want. And after you will have pressed ENTER it will have stored your inserted value into the value $myvariable.

    After that it will display "Your value is <value you have typed>". So this is basically how you get your info.

    Also, to make sure that something has actually been typed and, you can use something similar to this:

    Code:
    printf "Insert your value: \n";
    $myvariable = <STDIN>;
    if (!$myvariable) {
        printf "Input corrupt \n";
        exit();
    }
    else {
        printf "Your value is $myvariable";
    }
    This will check if a value has been entered, if their hasn't it will display the corrupt message and exit the program. If their has been an input, then it will display your value.

    6. Example

    To finish my tutorial i will show you how you would be able to do a simple multiplying calculator using input:

    Code:
    #!/usr/bin/perl
    
    printf "Insert value 1: \n";
    
    $value1 = <STDIN>;
    if (!$value1) {
        printf "Input corrupt \n";
        exit();
    }
    
    printf "Insert value 2: \n";
    
    $value2 = <STDIN>;
    if (!$value2) {
        printf "Input corrupt \n";
        exit();
    }
    
    $output = $value1 * $value2;
    
    printf "Value 1: $value1 \n";
    printf "Value 2: $value2 \n";
    printf "Multiplied result: $output";
    7. End

    I hope this tutorial has been as educational for you as i have put time in it and enjoyed writing. For any further questions, please reply.

    NOTE: Do not copy this tutorial unless you have my permission (by mailing me, on msn or just pm)