Monday, June 30, 2014

PERL BASICS

1.What is Perl?

     Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more.
     The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). Its major features are that it's easy to use, supports both procedural and object-oriented (OO) programming, has powerful built-in support for text processing, and has one of the world's most impressive collections of third-party modules.

2.Running Perl programs

To run a Perl program from the Unix command line:

                 perl file_name.pl

 First line of the script contains:

                       #!/usr/bin/perl (-w -s )
                             or
                        use warning;
                        use strict;

    w ->  It is used for the warning. This pops out  the message if program
              contains any error or warning.
    s -> This is used to caught the potential problem in the program.


3.Basic syntax

A Perl script or program consists of one or more statements. These statements are simply written in the script in a straightforward fashion. There is no need to have a main() function or anything of that kind.

Perl statements end in a semi-colon:

                      print " Message " ;

print -> Like "C" print is a inbuilt function, which displays the Message  on the 
              GUI (terminal).

Example:  print "Hello World";

Numbers don't need quotes around them:

                Print 25;

4.Perl variable types

 Perl has three main variable types: 1.scalars  2.arrays  3.hashes.

a] Scalars:
 
A scalar represents a single value:

               $name = "John";
            $number=25;

Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required.

Scalar values can be used in various ways:
 
                    print $name;
                 print "My Name is $name\n";
                 print "Square of the $number is" , $number*$number "\n";

There are a number of "magic" scalars with names that look like punctuation or line noise. These special variables are used for all kinds of purposes.The only one you need to know about for now is "$_ " which is the "default variable". It's used as the default argument to a number of functions in Perl, and it's set implicitly by certain looping constructs.
         
                  print;                         # Prints content of $_ by default.


b] Arrays:

An array represents a list of values:

                  @friends = ("John" , "Jimmy", "Lucy") ;
               @numbers=(10,20,30);
               @mixed=("john",20,1.58);

Arrays are zero-indexed. Here's how you get at elements in an array:

                print $friends[0];              #  prints John
                print $numbers[1];           #  prints 20
                print $mixed [2];              # prints 1.58

The special variable $#array tells you the index of the last element of an array:

                print $numbers[$#numbers];   # Prints the last element of the
                                                                            array i.e 30.

You might be tempted to use $#array + 1 to tell you how many items there are in an array. Don't bother. As it happens, using @array where Perl expects to find a scalar value ("in scalar context") will give you the number of elements in the array:

                                      if (@friends <5) 
                                              { ......}

The elements we're getting from the array start with a $ because we're getting just a single value out of the array; you ask for a scalar, you get a scalar.
To get multiple values from an array:
             
                @friends [0,1]                       # Gives "John" and "Jimmy"
             @friends [0..2]                     # Gives "John","Jimmy" and "Lucy"
             @friends [1..$#friends]       # Gives all except 1st one

This is called an "array slice".
You can do various useful things to lists:

                        @sorted = sort @friends;
                    @rev      = reverse @numbers;

There are a couple of special arrays too, such as @ARGV (the command line arguments to your script) and @_ (the arguments passed to a subroutine).

No comments:

Post a Comment