Wednesday, November 2, 2011

Perl Basics 1



1) To find all installed modules for your perl in your host :

root@ubuntu:~# perl -MCPAN -e 'CPAN::Shell->r'
---------------------------------------------------------------
List::Util                     1.21      1.23  GBARR/Scalar-List-Utils-1.23.tar.gz
Locale::Constants              2.07      3.16  SBECK/Locale-Codes-3.16.tar.gz
Locale::Country                2.07      3.18  SBECK/Locale-Codes-3.18.tar.gz
Mail::Address                  2.06      2.08  MARKOV/MailTools-2.08.tar.gz
PerlIO::scalar                 0.07      0.12  RJBS/perl-5.15.2.tar.bz2
Pod::Man                       2.22      2.25  RRA/podlators-2.4.0.tar.gz
Socket                         1.82      1.94  FLORA/perl-5.13.11.tar.gz
mod_perl2                  2.000004  2.000005  PHRED/mod_perl-2.0.5.tar.gz
233 installed modules have no parsable version number
(use 'o conf show_unparsable_versions 1' to show them)
-------------------------------------------------------------------------

Installing Perl Modules :

root@ubuntu:~# perl -MCPAN -e shell
Terminal does not support AddHistory.

cpan shell -- CPAN exploration and modules installation (v1.9402)
Enter 'h' for help.

cpan[1]> install File::Compare
CPAN: Storable loaded ok (v2.20)
Going to read '/root/.cpan/Metadata'
  Database was generated on Wed, 09 Nov 2011 04:30:34 GMT
File::Compare is up to date (1.1006).





2) Use of perl module strict :
use strict;
'use strict' turns on the 'strict' pragma which will make you declare your variables with the my keyword.

3) Use of my in variables?
Variables that you use in a subroutine should be made private to that subroutine with the my operator.
This avoids accidentally overwriting similarly-named variables in the main program.
If you already included use strict at the top of your program, perl will check that all variables are introduced with my.

4) Variable Declaration

my $scalar;  # $scalar is private
my @array;   # now @array is private
my %hash;    # %hash is private

5) Loops in Perl


A) For Loop :
=============

for (starting assignment; test condition; increment)
{
 code to repeat
}

Well, that's great, but it doesn't tell us much about what happens. Instead, let's use a real example. Let's say we wanted to print the word "cool" ten times in a row. We could use a for loop like this:

#!/usr/bin/perl
for ($count=1; $count<11; $count++)
{
 print "cool\n";
}




B) While Loop :
================

Unlike the for loop, the while loop does not have a self-contained way to alter the initial expression.



#!/usr/bin/perl
$i=1;                  /* initial expression set to 1 */
while ($i < 10) {     /* Next comes the start of the while loop, and the expression in the parenthesis is evaluated: */
print "$i\n";         /* Execute the code block inside the while loop.  */
$i++;                  /* Execute the increment value inside codeblock */
}





C) Foreach loop:
=================

The foreach loop is one that is often used with some sort of array.

foreach variable_name (array_name)
{
 code to repeat
}

#!/usr/bin/perl
@names=("a","b",'c","d");
print "Print Names in array \n";

foreach $i (@names)
{
print "$i\n";
}

No comments:

Post a Comment