Results 1 to 3 of 3

Thread: Short Introduction to Perl Share/Save - My123World.Com!

  1. #1

    Short Introduction to Perl

    Perl, Python, Ruby are quite famous scripting languages amongst hackers apart from main mainstream C/C++. I got some good tutorials and ebooks from Guru gOOgle on Perl I m gonna post a short tutorial on Perl here.You should have perl installed in your system. Its present in some Linux version.

    Lets start with the basics, printing some text. Open up any text editor:

    #!usr/bin/perl

    print "Hello World!"; #prints hello world to the screen

    you can save the file as "all files" with .pl extension. like "hello.pl"

    Lets disect this one line at a time:

    Code:
    #!usr/bin/perl

    The first line of every PERL script is a commented line directed toward the PERL interpreter. That way is the commonly used way for Linux users, but what if you're running Windows? Like so:

    Code:
    #!D:\Perl\bin\perl

    Make sure to see if you have the right directory set to perl.pl (WILL VARY!!)
    Now The # makes it a commented line.
    Code:
    print "Hello World!"; #prints hello world to the screen

    The print keyword sends output to the screen, in this case, Hello World! will be sent to the screen as output. Do not forget the semi-colon. There you see the # again which means its a comment and won't show up when you run it. Comments should always be used and will help keep code easy and readable. Both single or double quotes can be used in perl.

    Lets move on for printing variables. To declare a string variable, you do the following:
    Code:
    #!usr/bin/perl
    $myVariable = "Hello World!";
    my $myVar = "Hello World!";
    print "$myVariable";

    String variables always have an $ in the beginning following a variable name, but you can also put 'my' in front, which makes it easier in bigger size perl code, than followed by an = sign which means "set to" and then the string in quotations, dont forget the semi-colon and remember variables are case-sensitive!! Now in Perl, there is no int, char, double, long, float. It is all done with the $. Example:
    Code:
    #!usr/bin/perl

    #This defines many different variables
    $stringvariable = "This is a string!";
    $numbervariable = 5;
    $mathvariable = "1 + 1";
    #This is called Scalar Variables

    print "$stringvariable\n";
    print "$numbervariable\n";
    print "$mathvariable";

    Scalar variables are simple variables containing only one element--a string, a number, or a reference. Strings may contain any symbol, letter, or number. Numbers may contain exponents, integers, or decimal values. Very simple and dead to the point.

    Arrays are defined using the @ followed by the array name and then the "set to", or =, then your elements defined enclosed in parenthesis and in quotations and seperated by comma's:
    Code:
    #!usr/bin/perl
    @myArray = (" Zero", " One", " Two", " Three", " Four", " Five");
    print "@myArray";
    This will output:
    Zero One Two Three Four Five

    Now, you're probably wondering why we are printing our variables with double quotations around them, and you're probably wondering how you would print out character such as $, @, %, ', " you would do this with a backslash(\) character:
    Code:
    #!usr/bin/perl
    $mystring = "This is mystring!";
    print "\$mystring\n";
    print "$mystring";

    The first print will print the text $mystring and not the variable $mystring because we have used the backslash character. This is called escape characters. the second one will print the value in $mystring because there is no escape characters. You're probably wondering why I keep using \n and this should come easy for alot you. \n is a formatting character, there's a whole bunch of them. \n begins a newline where ever you put it at.

    Formatted Characters using backslash:
    \L Transform all letters to lowercase
    \l Transform the next letter to lowercase
    \U Transform all letters to uppercase
    \u Transform the next letter to uppercase
    \n Begin on a new line
    \r Applys a carriage return
    \t Applys a tab to the string
    \f Applys a formfedd to the string
    \b Backspace
    \a Bell
    \e Escapes the next character
    \0nn Creates Octal formatted numbers
    \xnn Creates Hexideciamal formatted numbers
    \cX Control characters, x may be any character
    \Q Do not match the pattern
    \E Ends \U, \L, or \Q functions

    example:
    Code:
    #!usr/bin/perl

    $newline = "\n";
    $allUpperCase = "\U";

    print "Hello, Here comes a newline! $newline";
    #change the word cat to all uppercase using our variable
    print "$allUpperCase cat";

    Let's go back to arrays for a little bit, and use this example:
    Code:
    #!usr/bin/perl

    @myArray = ("Zero","One","Two","Three");

    Every value in your array is assigned to an element, the elements always start at 0 and so forth. Which means, the value in @myArray, "Zero" is the element 0. "One" is element 1, and so on.

    Array values can get hard to read if you have alot of them, with all those quotations and commas, to simplify it you can use qw();
    Code:
    #!usr/bin/perl

    @easierArray = qw(Zero One Two Three Four Five Six Seven Eight);
    --------------------------------------------------------------------------------
    So this was quick introduction to Perl. Hope you all will like it For further study of this language , please download the documentation from the official Perl site. Its very helpful . Thanks
    The three great essentials to achieve anything worth while are: Hard work, Stick-to-itiveness, and Common sense. - Thomas A. Edison
    __________________________________________________ _____________________

  2. #2
    Garage Member D4rk357's Avatar
    Join Date
    Jul 2010
    Location
    localhost@mumbai
    Posts
    153
    Blog Entries
    1
    nicce guide .. would help starters and newbies in perl like me a lot
    Spirit was turned 2 ashes ,soul endured so much pain..
    now the darker time evanescence ,the fallen shall rise again.

  3. #3
    me also a newbie bro..these languages r quite interesting
    The three great essentials to achieve anything worth while are: Hard work, Stick-to-itiveness, and Common sense. - Thomas A. Edison
    __________________________________________________ _____________________

Posting Permissions

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