SlideShare a Scribd company logo
Perl Sopan Shewale,  [email_address]
Hello World – First Program $ cat HelloWorld.pl #!/usr/bin/perl print "Welcome to the world of Perl Programming !\n"; $ chmod +x HelloWorld.pl $ ./HelloWorld.pl Welcome to the world of Perl Programming ! $ perl HelloWorld.pl Welcome to the world of Perl Programming ! Make it executable Execute it Another Way to Execute
Another Simple Example – Taking input from user  [sopan@ps3724 tutorial]$ cat InteractiveHello.pl #!/usr/bin/perl my $user; print &quot;Please write your name:&quot;; $user = <STDIN>; print &quot;Welcome to the world of perl, You are : $user\n&quot;;
Simple example – way to execute external command [sopan@ps3724 tutorial]$ cat df.pl #!/usr/bin/perl print &quot;Method1:\n&quot;; my $out1 = system(&quot;df -k&quot;); print &quot;The output of df -k command is :\n $out1\n&quot;; Method1: Filesystem  1K-blocks  Used Available Use% Mounted on /dev/hda8  19346964  10931664  7432528  60% / /dev/hda1  101089  14826  81044  16% /boot /dev/hda3  15116868  12328832  2020132  86% /home none  252056  0  252056  0% /dev/shm /dev/hda5  8064272  5384612  2270008  71% /usr /dev/hda6  3020140  307088  2559636  11% /var /dev/hda2  30233928  8830792  19867324  31% /usr/local The output of df -k command is : 0
Simple example – way to execute external command print &quot;Method2:\n&quot;; my $out2 = `df -k`; print &quot;The output of df -k command is :\n $out2\n&quot;; Method2: The output of df -k command is : Filesystem  1K-blocks  Used Available Use% Mounted on /dev/hda8  19346964  10931664  7432528  60% / /dev/hda1  101089  14826  81044  16% /boot /dev/hda3  15116868  12328832  2020132  86% /home none  252056  0  252056  0% /dev/shm /dev/hda5  8064272  5384612  2270008  71% /usr /dev/hda6  3020140  307088  2559636  11% /var /dev/hda2  30233928  8830792  19867324  31% /usr/local [sopan@ps3724 tutorial]$
An introduction to Simple Stuff Perl Variables – How perl treats integers, floating numbers or strings? Give simple examples. Play with variables – assign, modify, print the values. Explain about “my” syntax. Arithmetic Operations  (+, -, * etc) on int/float and strings.  Introduction to loops (if-else, while, for) Comparison operators – (>, <, ==, eq, nq etc) Introduction to special variable $_ How to use command line argument in the script –though arrays will be introduced later.
Exercise  Find the maximum from the given set of integers. Take two numbers from user – print the addition of these numbers Greatest Common Divisor – Implement following algorithm gcd (a, b) { if (b < a ) {  swap (a, b);  } While (b) {  r = b % a; a = b; b = r; } return a; }
Directory and File Operations File Operations – Read the file, Write the file, Append the file. Introduction to $! Introduction to die  Introduction to chomp  Unlink Exercise– Write the program to monitor given set of hosts in the network.
Arrays and Functions Arrays, split, join functions, pop, push, shift, unshift functions. Exercises  Example 1. Counting number of lines from a given file. Example 2. Counting number of words from a file (… what is word?) Example 3. Various operations on /etc/password – e.g. Which userid do not have user-gecos?
Exercise – long assignement Example-Data We would like to provide the following services: Q1. Given Year, won/lost category, print team name. Q2. Given Year, Print all entries for that year.  Q3. For Won/Lost – print year and teams.  Q4. Print all entries sorted by Won/Lost  or By Year.  1975:Won:West Indies 1975:Lost:Australia 1979:Won:West Indies 1979:Lost:England 1983:Won:India 1983:Lost:West Indies 1987:Won:Australia 1987:Lost:England 1992:Won:Pakistan 1992:Lost:England 1996:Won:Srilanka 1996:Lost:Australia
grep and map functions List Filtering with grep  my @result = grep EXPR @input_list; my $count = grep EXPR @input_list; my @input_numbers = (1, 4, 50, 6 14); my @result = grep $_>10, @input_numbers; Transforming Lists with Map  my @input_list = (1, 4, 10, 56, 100); my @result = map  $_ + 100,  @input_list; my @next_result = map ($_, 5*$_), @input_list; my %hash_result =map ($_, $_*$_), @input_list;
Exercise Q. 1 Write a program that takes a list of filenames on the command line and uses grep to select the ones whose size in bytes is less than 1000. Use map to transform the strings in this list, putting four space characters in front of each and a newline character after. Print the resulting list.
Regular Expression Q.  What is a regular expression?  Ans.  A regular expression is simply a string that describes a pattern. Q.  So what is pattern? Ans.  Let us understand by example:  ls *.txt,  dir *.*  are examples of patterns.  author:sopan_shewale  can be example of search  pattern in for Search on content management site or for search engine.  In Perl, the patterns described by regular expressions are used to search strings,  extract desired parts of strings, and to do search and replace operations. Regular Expression is often abbrevated as regexp, regex.
Regular expression (Cont…) Let us look at the example   if (&quot;Hello  World &quot; =~ /World/) {  print  &quot;It matches\n&quot;;  }  else  {  print  &quot;It doesn't match\n&quot;; }  The sense of  =~  is reversed by  !~  operator.
Regular Expression (Cont…) The literal string in the regexp can be replaced by a variable:  $greeting = &quot;World&quot;;  if (&quot;Hello World&quot; =~ /$greeting/) {  print  &quot;It matches\n&quot;;  } else  {  print  &quot;It doesn't match\n&quot;; }  Regexp’s are case sensitive
Regular Expression (Cont…) If a regexp matches in more than one place in the string, perl will always  match at the earliest possible point in the string:  &quot;Hello World&quot; =~ /o/; # matches 'o' in 'Hello'  &quot;That hat is red&quot; =~ /hat/; # matches 'hat' in 'That'  &quot;2+2=4&quot; =~ /2+2/; # doesn't match, + is a metacharacter  &quot;2+2=4&quot; =~ /2\+2/; # matches, \+ is treated like an ordinary +  Some characters, called  metacharacters , are reserved for use in regexp notation. List is :  {}[]()^$.|*+?\
Where  in the string the regexp should try to match – Use  ^ and $ .  The anchor ^ : match at the beginning of the string  The  anchor $ : match at the end of the string,  Regular Expression (Cont…) %vi  simple_grep  #!/usr/bin/perl  $regexp =  shift ;  while (<>) {  print  if /$regexp/; } &quot;housekeeper&quot; =~ /keeper/; # matches  &quot;housekeeper&quot; =~ /^keeper/; # doesn't match  &quot;housekeeper&quot; =~ /keeper$/; # matches  &quot;housekeeper\n&quot; =~ /keeper$/; # matches  &quot;keeper&quot; =~ /^keep$/; # doesn't match &quot;keeper&quot; =~ /^keeper$/; # matches &quot;&quot; =~ /^$/; # ^$ matches an empty string  Grep Kind of example
Using character classes A character class allows a set of possible characters, rather than just a single character, to match at a particular point in a regexp.  $x = 'bcr';  /[\\$x]at/; # matches '\at', 'bat, 'cat', or 'rat'  /item[0-9]/; # matches 'item0' or ... or 'item9'  /[0-9bx-z]aa/; # matches '0aa', ..., '9aa', /cat/; # matches 'cat'  /[bcr]at/; # matches 'bat, 'cat', or 'rat'  /item[0123456789]/; # matches 'item0' or ... or 'item9‘ &quot;abc&quot; =~ /[cab]/; # matches 'a'  In the last statement, even though 'c' is the first character in the class, 'a‘ matches because the first character position in the string is the earliest point at which the regexp can match.  Regular Expression (Cont…)
\d is a digit and represents [0-9] \s is a whitespace character and represents [\ \t\r\n\f] \w is a word character (alphanumeric or _) and represents [0-9a-zA-Z_] \D is a negated \d; it represents any character but a digit [^0-9] \S is a negated \s; it represents any non-whitespace character [^\s] \W is a negated \w; it represents any non-word character [^\w] The period '.' matches any character but &quot;\n&quot; Regular Expression (Cont…)
Matching this or that This is accomplished by using the  alternation  metacharacter |  &quot;cats and dogs&quot; =~ /cat|dog|bird/;  # matches &quot;cat&quot;  &quot;cats and dogs&quot; =~ /dog|cat|bird/;  # matches &quot;cat&quot; Regular Expression (Cont…)
Grouping things and hierarchical matching sometime we want alternatives for just part of a regexp  The grouping  metacharacters () solve this problem /(a|b)b/; # matches 'ab' or 'bb‘ /(ac|b)b/; # matches 'acb' or 'bb'  /(^a|b)c/; # matches 'ac' at start of string or 'bc' anywhere  /(a|[bc])d/; # matches 'ad', 'bd', or 'cd'  /house(cat|)/; # matches either 'housecat' or 'house‘ /house(cat(s|)|)/; # matches either 'housecats' or 'housecat' or  # 'house'. Note groups can be nested.  /(19|20|)\d\d/; # match years 19xx, 20xx, or the Y2K problem, xx  &quot;20&quot; =~ /(19|20|)\d\d/; # matches the null alternative '()\d\d', # because '20\d\d' can't match  Regular Expression (Cont…)
The process of trying one alternative, seeing if it matches, and moving on to the next alternative if it doesn't, is called backtracking .  To be concrete, here is a step-by-step analysis of what perl does when it tries to match the regexp   &quot;abcde&quot; =~ /(abd|abc)(df|d|de)/;  Regular Expression (Cont…)
[1].  Start with the first letter in the string 'a'. [2].  Try the first alternative in the first group 'abd'. [3].  Match 'a' followed by 'b'. So far so good. [4].  'd' in the regexp doesn't match 'c' in the string – a dead end. So backtrack  two characters and pick the second alternative in the first group 'abc'. [5].  Match 'a' followed by 'b' followed by 'c'. We are on a roll and have satisfied the first group. Set $1 to 'abc'. [6].  Move on to the second group and pick the first alternative 'df'. [7].  Match the 'd'. [8].  'f' in the regexp doesn't match 'e' in the string, so a dead end. Backtrack  one character and pick the second alternative in the second group 'd'. [9].  'd' matches. The second grouping is satisfied, so set $2 to 'd'. [10].  We are at the end of the regexp, so we are done! We have matched 'abcd' out of the string &quot;abcde&quot;. Regular Expression (Cont…)
Extracting Matches The grouping metacharacters () allow the extraction of the parts of the string that  matched. The extracted stuff is put into the special variables $1, $2, etc # extract hours, minutes, seconds  if ($time =~ /(\d\d):(\d\d):(\d\d)/) {  # match hh:mm:ss format  $hours = $1;  $minutes = $2;  $seconds = $3;  }  Regular Expression (Cont…)
Matching Repetitions metacharacters ?, * , + , and {} play a vital role in matchings. a? = match 'a' 1 or 0 times a* = match 'a' 0 or more times, i.e., any number of times a+ = match 'a' 1 or more times, i.e., at least once a{n,m} = match at least n times, but not more than  m  times. a{n,} = match at least n or more times a{n} = match exactly n times Regular Expression (Cont…)
A few Principles Principle 0:  Taken as a whole, any regexp will be matched at the earliest possible position in the string. Principle 1:  In an alternation a|b|c... , the leftmost alternative that allows a match for the whole regexp will be the one used. Principle 2:  The maximal matching quantifiers ?, * , + and {n,m} will in general match as much of the string as possible while still allowing the whole regexp to match. Regular Expression (Cont…)
Principle 3:  If there are two or more elements in a regexp, the leftmost greedy quantifier, if any, will match as much of the string as possible while still allowing the whole regexp to match. The next leftmost greedy quantifier, if any, will try to match as much of the string remaining  available  to it as possible, while still allowing the whole regexp to match. And so on, until all the regexp elements are satisfied. A few Principles Regular Expression (Cont…)
Principle 0 overrides the others - the regexp will  be matched as early as possible, with the other  principles determining how the regexp matches  at that earliest character position.  $x = &quot;The programming republic of Perl&quot;;  $x =~ /^(.+)(e|r)(.*)$/;  # matches,  # $1 = 'The programming republic of Pe'  # $2 = 'r'  # $3 = 'l'  Regular Expression (Cont…)
Exercise  Problem 1: Let us match both integers and floating point numbers from text, ignore other parts. Problem 2. Match Email Addresses or collect all email addresses from given text topic.  Problem 3. $string = “/some/path/in/directory/html”; print only last part – last level directory name.
Regular Expression – Match the positive Integer You might start with expression like  /[0-9]+/  which is for  &quot;one or more digits&quot;.  Then you might do simplification – to  /\d+/  - but still it is wrong, why?  Expression like  &quot;abc123de“  fails - Opps So you are moving to add anchors like  /^\d+$/ . This this is not correct – the new line char troubles us- the expression can match  &quot;123\n&quot;  as well as  &quot;123“  - oops!  Now the modern Perl versions provide the  \z  anchor, - The best answer to our problem could be  /^\d+\z/ . But still issues - Although deprecated, the  $*  variable controls the matching of  ^  and  $  to permit internal newline matches as well as end-of-string matches. The string  &quot;foobar\n123&quot;  will also match our new regular expression - Oops again.  Let us go to the final answer – The best answer is  /\A\d+\z/ , which says, &quot;beginning of string&quot; followed by &quot;one or more digits&quot; followed by &quot;end of string&quot;. The answer is: /\A\d+\z\/ - finally     Question:  Determine whether a string contains a positive integer. Discussion:
Hashes Defination: Hashes are like arrays but scalar indexes.  Example Defination:  my %fruit;  $fruit{&quot;red&quot;} = &quot;apple&quot;; $fruit{&quot;orange} = &quot;orange&quot;;  $fruit{&quot;purple&quot;} = &quot;grape&quot;;  %fruit = (&quot;red&quot;, &quot;apple&quot;, &quot;orange&quot;, &quot;orange&quot;, &quot;purple&quot;, &quot;grape&quot;);  %fruit = (          &quot;red&quot; => &quot;apple&quot;,          &quot;orange&quot; => &quot;orange&quot;,          &quot;purple&quot; => &quot;grape&quot;,  );
Functions on Hashes: delete Keys values   each Functions on Hashes #vi fruit.pl my %fruit = (          &quot;red&quot; => &quot;apple&quot;,          &quot;orange&quot; => &quot;orange&quot;,          &quot;purple&quot; => &quot;grape&quot;,  ); for (keys %fruit) { print “$_ : $fruit{$_} \n”; } Why use Hash?  Write a program which takes the input the city/capital name and returns the country name by Using Arrays and Hashes or  any other method. So you know why use Hashes.
References # Create some variables  $a = &quot;mama mia&quot;;  @array = (10, 20);  %hash = (&quot;laurel&quot; => &quot;hardy&quot;, &quot;nick&quot; => &quot;nora&quot;); # Now create references to them  $ra = \$a;  # $ra now &quot;refers&quot; to (points to) $a  $rarray = \@array;  $rhash = \%hash;  #You can create references to constant scalars in a similar fashion: $ra = \10;  $rs = \&quot;hello world&quot;;
References Dereferencing:  Means getting at the value that a reference point to. #Look nicely: @array = (10, 20); $ra = \@array; is similar to $ra =[10, 20] # Notice the square braces. %hash = (&quot;laurel&quot; => &quot;hardy&quot;, &quot;nick&quot; => &quot;nora&quot;);  $rhash = \%hash is similar to $rhash = { “laurel”=>”hardy”, “nick” => “nora” }; $ra = \$a; # First take a reference to $a  $$ra += 2; # instead of $a += 2;  print $$ra; # instead of print $a  $rarray = \@array; push (@array , &quot;a&quot;, 1, 2); # Using the array as a whole  push (@$rarray, &quot;a&quot;, 1, 2); # Indirectly using the ref. to the array  $rhash = \%hash; print $hash{&quot;key1&quot;}; # Ordinary hash lookup print  $$rhash{&quot;key1&quot;}; # hash replaced by $rhash
Shortcuts with the Arrow Notation $rarray = \@array;  print $rarray->[1] ; # The &quot;visually clean&quot; way  $rhash = \%hash;  print $rhash->{&quot;k1&quot;}; #instead of ........  print $$rhash{&quot;k1&quot;}; # or  print ${$rhash}{&quot;k1&quot;};  References
Subroutines Q.  Why Subroutines exists in Programming?  Ans.  Do I need to Answer? Defining subroutine: #Defining subroutine: sub mysubroutinename { #### code to do that } How to execute subroutine? How to pass the arguments to subroutines? How to use those arguments passed to the subroutines? Return Values?
Modules Perl Allows you to partition your code into one more reusable module. Modules is code reuse We will see: Define Modules using the package keyword Load pre-defined modules using “use” and “require” keywords. Access package specific variables and subroutines using “::” notation. Load Functions at Run Time.
 
Remember that modules are not intended to be standalone applications  we'll never actually be running our module  by itself . Instead, we'll be using our module to provide added functionality to a larger script  Let us look at the example: Filename is  SayHello.pm Modules (Cont…)  Package SayHello; sub hello_package { return “Hello, Welcome to the world of Modules”; } 1;
Use of “use” keyword This is simplest way to pull the module into your script Searches all the paths in the array @INC until it finds a file Use “require” command:  What’s the difference then? When you load a module using use, the module is loaded and executed at  compile time , even before the remaining portion of the script is compiled.  require pulls in the code of the module at run-time; and if the file cannot be found generates a run time error Note on variables declared with “my”  you will not be able to access it from outside the module  If you must access a module variable from outside the package you are declaring it in, then you can declare it using the  our  function   use and require – what is that?
 
For the decent example – may be if you are thinking of contributing to cpan.org- Have a look at  http://en.wikipedia.org/wiki/Perl_module bless :  This operator takes the reference and converts it into object. Some Useful work module –  Employee.pm
 
CGI  Check my other PPT  Explore the application available at   http://perl-md5-login.sourceforge.net/   MD5 Algorithm for Encryption  The module is used by Yahoo etc
Example: Count Click Script #!/usr/bin/perl ######################### ## Author: Sopan Shewale ## Company: Persistent Systems Pvt Ltd ## This script is created for giving demo on click count. ## The Script is support to display increse/decrease  ##click's, handles back button of browser, does not  ##handle reload stuff. ## also it's based on sessions. ######################## use strict; use warnings; use CGI; use CGI::Session; use CGI::Cookie; my $q = new CGI(); my $sessionid = $q->cookie(&quot;CGISESSID&quot;) || undef; my $session = new CGI::Session(undef, $sessionid, {Directory=>'/tmp'}); $sessionid = $session->id(); my $cookie = new CGI::Cookie(-name=>'CGISESSID',  -value=>$sessionid, -path=>&quot;/&quot;); print $q->header('text/html', -cookie=>$cookie); print $q->start_html(&quot;Welcome to Click Count Demo&quot;); print &quot;<h1>Welcome to Click Count Demo</h1>&quot;; my $count = $session->param('count');  ## count-is click count variable if(!defined($count)) { $session->param('count', 0);  $count=0;}  ### if session is first time created, set count=0 $session->param('count', $count); $count = $session->param('count'); #print &quot;<h1>The Click Count is: $count \n&quot;; ## Form stuff print $q->startform(-method=>'POST'); print $q->submit( -name=>&quot;Increase&quot;, -value=>'Increase1'); print $q->submit( -name=>&quot;Decrease&quot;, -value=>'Decrease1'); print $q->endform();
Example: Count Click Script (Cont…) ## Which button is being pressed my $which_button = $q->param('Increase'); if(defined ($which_button)) { print &quot;Increase pressed&quot;; $count = increase_count($count);  ## Increase the count since increase button is clicked $session->param('count', $count); }else { $which_button=$q->param('Decrease'); if(defined($which_button)){ print &quot;Decrease pressed&quot;; $count = decrease_count($count);  ## Decrease the count since decrease button is clicked $session->param('count', $count); } else {print &quot;You have not pressed any button,  seems you are typing/re-typing the same URL&quot;; } } $count = $session->param('count'); print &quot;<h1>The Click Count is: $count \n&quot;; print $q->end_html(); ## increases the count by 1 sub increase_count { my $number = shift; $number = $number +1; return $number; } ## decreases the count by 1 sub decrease_count { my $number = shift; $number = $number -1; return $number; }
Using eval String Form: Expression Evaluation The Block Form: Exception Handling Possible to use eval for time-outs.  A few more…
Data::Dumper
TWiki
bin:  view, edit, search etc tools:  mailnotify template:  Templates (view.tmpl), also has skin related data lib:  modules and required libraries, plugin code data:  webs directories (e.g. Main,TWiki) and each directory inside contains the topics from that web. The topics are companied by there version history pub:  Attachments of the topics and commonly shared data
Basic request in TWiki from browser side, calls some script from bin Directory (like views) which Loads setlib.cfg from the same  directory. Which loads  LocalSite.cfg from lib directory.  The  LocalSite.cfg has site related  Configuration variables defined.  Then the relevant modules are loaded, topic are read, macros expanded, the content converted into html page and sent back to browser, request is completed. Now a days the LocalSite.cfg is written using Web-Interface, For example http://mytwikiste.com/bin/configure   TWiki (Cont…)
TWiki (Cont…) What is plugins?:   One can add plugins to TWiki to extend the functionality of TWiki without altering the core code. Plugin approach lets us do following: add virtually unlimited features while keeping the main TWiki code compact and efficient; heavily customize an installation and still do clean updates to new versions of TWiki; rapidly develop new TWiki functions in Perl using the Plugin API.
TWiki Community: The plugins are located at  http://twiki.org/cgi-bin/view/Plugins   Each plugin has couple of topics on the site for support, development, installation instruction. Let us look at example:  http://twiki.org/cgi-bin/view/Plugins/CommentPlugin  : Has comment Plugin, with installation instructions, syntax etc. http://twiki.org/cgi-bin/view/Plugins/CommentPluginDev : Used for support activity and comments from public.  http://twiki.org/cgi-bin/view/Plugins/CommentPluginAppraisal : Appraisal purpose, any one can appraise the plugin.  The plugin developer must read: TWiki::Func ( http://twiki.org/cgi-bin/view/TWiki/TWikiFuncModule ) code.  That’s the Plugin API. TWiki (Cont…)
General Instructions for creating Plugin The plugin name is in the format <name>Plugin. The code goes in <TWikiRootDirectory>/lib/TWiki/Plugings/ directory.  TWiki provides plugin called EmptyPlugin which can be used as Template to develop new plugin. The code of Empty Plugin is <TWikiRootDirectory>/lib/TWiki/Plugins/EmptyPlugin.pm Create a  <name>Plugin  topic in the TWiki web. The topic contains the settings, installation instructions and syntax of the plugin.  Enable Plugin by using configure e.g.  http://yourtwikiinstalled.com/bin/configure   TWiki (Cont…)
HelloPlugin  %HELLO{format=“Dear $first $last, $brWelcome to the world of TWiki” first=“Hari” last=“Sadu”}%  Dear Hari Sadu, Welcome to the world of TWiki This should get expanded to:
 
 
Database Connectivity   DBI Module General process used in database activity  Connect Prepare sql query  Execute  Finish  Disconnect  Best document available at  http://search.cpan.org/src/TIMB/DBI_AdvancedTalk_2004/index.htm
Exercise Q. 1: Given array, delete the duplicate entries – the order  should be reserved e.g. For input Array - (“Proxy”, “ Proxy”, “smtp”, “smtp”, “smtp”, “mail”, 25)  - output should be (“proxy”, “smtp”, “mail” 25);
References Online http://learn.perl.org/library/beginning_perl/  - Simon Cozens http://ebb.org/PickingUpPerl/ http:// gnosis.cx/publish/programming/regular_expressions.html http://perldoc.perl.org/perlretut.html Books Advanced Perl Programming – By Sriram Srininivasan Learning Perl Objects, References, & Modules – Randal L. Schwartz Intermediate Perl – Randal L. Schwartz  Perl for Systems Administration – David N. Blank Mastering Algorithms with Perl – Jon Orwant et all  People to Track Randal L. Schwartz Simon Cozens  Convey - ???
References (Cont…) Tools Web Application Development - Frameworks CGI::Application ( http:// cgiapp.erlbaum.net / )  Catalyst ( http:// www.catalystframework.org /  ) PageKit Web Application Framework ( http:// www.pagekit.org / )  Combust web site Framework ( http:// combust.develooper.com / )  Mason ( http://www.masonhq.com/ ) Embperl ( http:// perl.apache.org/embperl /  ) Template Tools – used in Web Application Development Template Toolkit  http://www.tt2.org/   HTML::Template  http://html- template.sourceforge.net /   Petal -  http://search.cpan.org/~bpostle/Petal-2.19/lib/Petal.pm Other TWiki ( http://twiki.org ) – Enterprise Collaboration Platform Sympa  - Mailing List Mhon-Arch – Mail Archiving Soltion Webmin – Unix System Administration Application
Thank You

More Related Content

What's hot (20)

PPT
Perl 101 - The Basics of Perl Programming
Utkarsh Sengar
 
PPTX
Bioinformatics p1-perl-introduction v2013
Prof. Wim Van Criekinge
 
ODP
Introduction to Perl
Dave Cross
 
ODP
Intermediate Perl
Dave Cross
 
ODP
Advanced Perl Techniques
Dave Cross
 
KEY
Introduction to Perl Best Practices
José Castro
 
PDF
Introduction to Perl and BioPerl
Bioinformatics and Computational Biosciences Branch
 
PPT
Perl tutorial
Manav Prasad
 
PDF
Improving Dev Assistant
Dave Cross
 
ODP
Introducing Modern Perl
Dave Cross
 
PDF
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
PPT
Perl Basics with Examples
Nithin Kumar Singani
 
PDF
DBIx::Class introduction - 2010
leo lapworth
 
PDF
Perl.Hacks.On.Vim
Lin Yo-An
 
PDF
Perl Programming - 02 Regular Expression
Danairat Thanabodithammachari
 
Perl 101 - The Basics of Perl Programming
Utkarsh Sengar
 
Bioinformatics p1-perl-introduction v2013
Prof. Wim Van Criekinge
 
Introduction to Perl
Dave Cross
 
Intermediate Perl
Dave Cross
 
Advanced Perl Techniques
Dave Cross
 
Introduction to Perl Best Practices
José Castro
 
Introduction to Perl and BioPerl
Bioinformatics and Computational Biosciences Branch
 
Perl tutorial
Manav Prasad
 
Improving Dev Assistant
Dave Cross
 
Introducing Modern Perl
Dave Cross
 
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
Perl Basics with Examples
Nithin Kumar Singani
 
DBIx::Class introduction - 2010
leo lapworth
 
Perl.Hacks.On.Vim
Lin Yo-An
 
Perl Programming - 02 Regular Expression
Danairat Thanabodithammachari
 

Similar to Perl Presentation (20)

ODP
Beginning Perl
Dave Cross
 
PPT
CGI With Object Oriented Perl
Bunty Ray
 
PPT
Cleancode
hendrikvb
 
PDF
Good Evils In Perl
Kang-min Liu
 
ODP
Why Python by Marilyn Davis, Marakana
Marko Gargenta
 
PPT
PERL Unit 6 regular expression
Binsent Ribera
 
ODP
Introduction to Modern Perl
Dave Cross
 
PPT
Dealing with Legacy Perl Code - Peter Scott
O'Reilly Media
 
PPT
The JavaScript Programming Language
Raghavan Mohan
 
PPT
The Java Script Programming Language
zone
 
PPT
Les origines de Javascript
Bernard Loire
 
PPT
Javascript by Yahoo
birbal
 
PPT
Javascript
guest03a6e6
 
PPTX
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
PPT
Javascript
vikram singh
 
PPT
Talk Unix Shell Script
Dr.Ravi
 
PDF
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Andrea Telatin
 
ODP
Perl Moderno
Tiago Peczenyj
 
PPT
Prototype js
mussawir20
 
Beginning Perl
Dave Cross
 
CGI With Object Oriented Perl
Bunty Ray
 
Cleancode
hendrikvb
 
Good Evils In Perl
Kang-min Liu
 
Why Python by Marilyn Davis, Marakana
Marko Gargenta
 
PERL Unit 6 regular expression
Binsent Ribera
 
Introduction to Modern Perl
Dave Cross
 
Dealing with Legacy Perl Code - Peter Scott
O'Reilly Media
 
The JavaScript Programming Language
Raghavan Mohan
 
The Java Script Programming Language
zone
 
Les origines de Javascript
Bernard Loire
 
Javascript by Yahoo
birbal
 
Javascript
guest03a6e6
 
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
Javascript
vikram singh
 
Talk Unix Shell Script
Dr.Ravi
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Andrea Telatin
 
Perl Moderno
Tiago Peczenyj
 
Prototype js
mussawir20
 
Ad

Recently uploaded (20)

PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Ad

Perl Presentation

  • 1. Perl Sopan Shewale, [email_address]
  • 2. Hello World – First Program $ cat HelloWorld.pl #!/usr/bin/perl print &quot;Welcome to the world of Perl Programming !\n&quot;; $ chmod +x HelloWorld.pl $ ./HelloWorld.pl Welcome to the world of Perl Programming ! $ perl HelloWorld.pl Welcome to the world of Perl Programming ! Make it executable Execute it Another Way to Execute
  • 3. Another Simple Example – Taking input from user [sopan@ps3724 tutorial]$ cat InteractiveHello.pl #!/usr/bin/perl my $user; print &quot;Please write your name:&quot;; $user = <STDIN>; print &quot;Welcome to the world of perl, You are : $user\n&quot;;
  • 4. Simple example – way to execute external command [sopan@ps3724 tutorial]$ cat df.pl #!/usr/bin/perl print &quot;Method1:\n&quot;; my $out1 = system(&quot;df -k&quot;); print &quot;The output of df -k command is :\n $out1\n&quot;; Method1: Filesystem 1K-blocks Used Available Use% Mounted on /dev/hda8 19346964 10931664 7432528 60% / /dev/hda1 101089 14826 81044 16% /boot /dev/hda3 15116868 12328832 2020132 86% /home none 252056 0 252056 0% /dev/shm /dev/hda5 8064272 5384612 2270008 71% /usr /dev/hda6 3020140 307088 2559636 11% /var /dev/hda2 30233928 8830792 19867324 31% /usr/local The output of df -k command is : 0
  • 5. Simple example – way to execute external command print &quot;Method2:\n&quot;; my $out2 = `df -k`; print &quot;The output of df -k command is :\n $out2\n&quot;; Method2: The output of df -k command is : Filesystem 1K-blocks Used Available Use% Mounted on /dev/hda8 19346964 10931664 7432528 60% / /dev/hda1 101089 14826 81044 16% /boot /dev/hda3 15116868 12328832 2020132 86% /home none 252056 0 252056 0% /dev/shm /dev/hda5 8064272 5384612 2270008 71% /usr /dev/hda6 3020140 307088 2559636 11% /var /dev/hda2 30233928 8830792 19867324 31% /usr/local [sopan@ps3724 tutorial]$
  • 6. An introduction to Simple Stuff Perl Variables – How perl treats integers, floating numbers or strings? Give simple examples. Play with variables – assign, modify, print the values. Explain about “my” syntax. Arithmetic Operations (+, -, * etc) on int/float and strings. Introduction to loops (if-else, while, for) Comparison operators – (>, <, ==, eq, nq etc) Introduction to special variable $_ How to use command line argument in the script –though arrays will be introduced later.
  • 7. Exercise Find the maximum from the given set of integers. Take two numbers from user – print the addition of these numbers Greatest Common Divisor – Implement following algorithm gcd (a, b) { if (b < a ) { swap (a, b); } While (b) { r = b % a; a = b; b = r; } return a; }
  • 8. Directory and File Operations File Operations – Read the file, Write the file, Append the file. Introduction to $! Introduction to die Introduction to chomp Unlink Exercise– Write the program to monitor given set of hosts in the network.
  • 9. Arrays and Functions Arrays, split, join functions, pop, push, shift, unshift functions. Exercises Example 1. Counting number of lines from a given file. Example 2. Counting number of words from a file (… what is word?) Example 3. Various operations on /etc/password – e.g. Which userid do not have user-gecos?
  • 10. Exercise – long assignement Example-Data We would like to provide the following services: Q1. Given Year, won/lost category, print team name. Q2. Given Year, Print all entries for that year. Q3. For Won/Lost – print year and teams. Q4. Print all entries sorted by Won/Lost  or By Year. 1975:Won:West Indies 1975:Lost:Australia 1979:Won:West Indies 1979:Lost:England 1983:Won:India 1983:Lost:West Indies 1987:Won:Australia 1987:Lost:England 1992:Won:Pakistan 1992:Lost:England 1996:Won:Srilanka 1996:Lost:Australia
  • 11. grep and map functions List Filtering with grep my @result = grep EXPR @input_list; my $count = grep EXPR @input_list; my @input_numbers = (1, 4, 50, 6 14); my @result = grep $_>10, @input_numbers; Transforming Lists with Map my @input_list = (1, 4, 10, 56, 100); my @result = map $_ + 100, @input_list; my @next_result = map ($_, 5*$_), @input_list; my %hash_result =map ($_, $_*$_), @input_list;
  • 12. Exercise Q. 1 Write a program that takes a list of filenames on the command line and uses grep to select the ones whose size in bytes is less than 1000. Use map to transform the strings in this list, putting four space characters in front of each and a newline character after. Print the resulting list.
  • 13. Regular Expression Q. What is a regular expression? Ans. A regular expression is simply a string that describes a pattern. Q. So what is pattern? Ans. Let us understand by example: ls *.txt, dir *.* are examples of patterns. author:sopan_shewale can be example of search pattern in for Search on content management site or for search engine. In Perl, the patterns described by regular expressions are used to search strings, extract desired parts of strings, and to do search and replace operations. Regular Expression is often abbrevated as regexp, regex.
  • 14. Regular expression (Cont…) Let us look at the example if (&quot;Hello World &quot; =~ /World/) { print &quot;It matches\n&quot;; } else { print &quot;It doesn't match\n&quot;; } The sense of =~ is reversed by !~ operator.
  • 15. Regular Expression (Cont…) The literal string in the regexp can be replaced by a variable: $greeting = &quot;World&quot;; if (&quot;Hello World&quot; =~ /$greeting/) { print &quot;It matches\n&quot;; } else { print &quot;It doesn't match\n&quot;; } Regexp’s are case sensitive
  • 16. Regular Expression (Cont…) If a regexp matches in more than one place in the string, perl will always match at the earliest possible point in the string: &quot;Hello World&quot; =~ /o/; # matches 'o' in 'Hello' &quot;That hat is red&quot; =~ /hat/; # matches 'hat' in 'That' &quot;2+2=4&quot; =~ /2+2/; # doesn't match, + is a metacharacter &quot;2+2=4&quot; =~ /2\+2/; # matches, \+ is treated like an ordinary + Some characters, called metacharacters , are reserved for use in regexp notation. List is : {}[]()^$.|*+?\
  • 17. Where in the string the regexp should try to match – Use ^ and $ . The anchor ^ : match at the beginning of the string The anchor $ : match at the end of the string, Regular Expression (Cont…) %vi simple_grep #!/usr/bin/perl $regexp = shift ; while (<>) { print if /$regexp/; } &quot;housekeeper&quot; =~ /keeper/; # matches &quot;housekeeper&quot; =~ /^keeper/; # doesn't match &quot;housekeeper&quot; =~ /keeper$/; # matches &quot;housekeeper\n&quot; =~ /keeper$/; # matches &quot;keeper&quot; =~ /^keep$/; # doesn't match &quot;keeper&quot; =~ /^keeper$/; # matches &quot;&quot; =~ /^$/; # ^$ matches an empty string Grep Kind of example
  • 18. Using character classes A character class allows a set of possible characters, rather than just a single character, to match at a particular point in a regexp. $x = 'bcr'; /[\\$x]at/; # matches '\at', 'bat, 'cat', or 'rat' /item[0-9]/; # matches 'item0' or ... or 'item9' /[0-9bx-z]aa/; # matches '0aa', ..., '9aa', /cat/; # matches 'cat' /[bcr]at/; # matches 'bat, 'cat', or 'rat' /item[0123456789]/; # matches 'item0' or ... or 'item9‘ &quot;abc&quot; =~ /[cab]/; # matches 'a' In the last statement, even though 'c' is the first character in the class, 'a‘ matches because the first character position in the string is the earliest point at which the regexp can match. Regular Expression (Cont…)
  • 19. \d is a digit and represents [0-9] \s is a whitespace character and represents [\ \t\r\n\f] \w is a word character (alphanumeric or _) and represents [0-9a-zA-Z_] \D is a negated \d; it represents any character but a digit [^0-9] \S is a negated \s; it represents any non-whitespace character [^\s] \W is a negated \w; it represents any non-word character [^\w] The period '.' matches any character but &quot;\n&quot; Regular Expression (Cont…)
  • 20. Matching this or that This is accomplished by using the alternation metacharacter | &quot;cats and dogs&quot; =~ /cat|dog|bird/; # matches &quot;cat&quot; &quot;cats and dogs&quot; =~ /dog|cat|bird/; # matches &quot;cat&quot; Regular Expression (Cont…)
  • 21. Grouping things and hierarchical matching sometime we want alternatives for just part of a regexp The grouping metacharacters () solve this problem /(a|b)b/; # matches 'ab' or 'bb‘ /(ac|b)b/; # matches 'acb' or 'bb' /(^a|b)c/; # matches 'ac' at start of string or 'bc' anywhere /(a|[bc])d/; # matches 'ad', 'bd', or 'cd' /house(cat|)/; # matches either 'housecat' or 'house‘ /house(cat(s|)|)/; # matches either 'housecats' or 'housecat' or # 'house'. Note groups can be nested. /(19|20|)\d\d/; # match years 19xx, 20xx, or the Y2K problem, xx &quot;20&quot; =~ /(19|20|)\d\d/; # matches the null alternative '()\d\d', # because '20\d\d' can't match Regular Expression (Cont…)
  • 22. The process of trying one alternative, seeing if it matches, and moving on to the next alternative if it doesn't, is called backtracking . To be concrete, here is a step-by-step analysis of what perl does when it tries to match the regexp &quot;abcde&quot; =~ /(abd|abc)(df|d|de)/; Regular Expression (Cont…)
  • 23. [1]. Start with the first letter in the string 'a'. [2]. Try the first alternative in the first group 'abd'. [3]. Match 'a' followed by 'b'. So far so good. [4]. 'd' in the regexp doesn't match 'c' in the string – a dead end. So backtrack two characters and pick the second alternative in the first group 'abc'. [5]. Match 'a' followed by 'b' followed by 'c'. We are on a roll and have satisfied the first group. Set $1 to 'abc'. [6]. Move on to the second group and pick the first alternative 'df'. [7]. Match the 'd'. [8]. 'f' in the regexp doesn't match 'e' in the string, so a dead end. Backtrack one character and pick the second alternative in the second group 'd'. [9]. 'd' matches. The second grouping is satisfied, so set $2 to 'd'. [10]. We are at the end of the regexp, so we are done! We have matched 'abcd' out of the string &quot;abcde&quot;. Regular Expression (Cont…)
  • 24. Extracting Matches The grouping metacharacters () allow the extraction of the parts of the string that matched. The extracted stuff is put into the special variables $1, $2, etc # extract hours, minutes, seconds if ($time =~ /(\d\d):(\d\d):(\d\d)/) { # match hh:mm:ss format $hours = $1; $minutes = $2; $seconds = $3; } Regular Expression (Cont…)
  • 25. Matching Repetitions metacharacters ?, * , + , and {} play a vital role in matchings. a? = match 'a' 1 or 0 times a* = match 'a' 0 or more times, i.e., any number of times a+ = match 'a' 1 or more times, i.e., at least once a{n,m} = match at least n times, but not more than m times. a{n,} = match at least n or more times a{n} = match exactly n times Regular Expression (Cont…)
  • 26. A few Principles Principle 0: Taken as a whole, any regexp will be matched at the earliest possible position in the string. Principle 1: In an alternation a|b|c... , the leftmost alternative that allows a match for the whole regexp will be the one used. Principle 2: The maximal matching quantifiers ?, * , + and {n,m} will in general match as much of the string as possible while still allowing the whole regexp to match. Regular Expression (Cont…)
  • 27. Principle 3: If there are two or more elements in a regexp, the leftmost greedy quantifier, if any, will match as much of the string as possible while still allowing the whole regexp to match. The next leftmost greedy quantifier, if any, will try to match as much of the string remaining available to it as possible, while still allowing the whole regexp to match. And so on, until all the regexp elements are satisfied. A few Principles Regular Expression (Cont…)
  • 28. Principle 0 overrides the others - the regexp will be matched as early as possible, with the other principles determining how the regexp matches at that earliest character position. $x = &quot;The programming republic of Perl&quot;; $x =~ /^(.+)(e|r)(.*)$/; # matches, # $1 = 'The programming republic of Pe' # $2 = 'r' # $3 = 'l' Regular Expression (Cont…)
  • 29. Exercise Problem 1: Let us match both integers and floating point numbers from text, ignore other parts. Problem 2. Match Email Addresses or collect all email addresses from given text topic. Problem 3. $string = “/some/path/in/directory/html”; print only last part – last level directory name.
  • 30. Regular Expression – Match the positive Integer You might start with expression like /[0-9]+/ which is for &quot;one or more digits&quot;. Then you might do simplification – to /\d+/ - but still it is wrong, why? Expression like &quot;abc123de“ fails - Opps So you are moving to add anchors like /^\d+$/ . This this is not correct – the new line char troubles us- the expression can match &quot;123\n&quot; as well as &quot;123“ - oops! Now the modern Perl versions provide the \z anchor, - The best answer to our problem could be /^\d+\z/ . But still issues - Although deprecated, the $* variable controls the matching of ^ and $ to permit internal newline matches as well as end-of-string matches. The string &quot;foobar\n123&quot; will also match our new regular expression - Oops again. Let us go to the final answer – The best answer is /\A\d+\z/ , which says, &quot;beginning of string&quot; followed by &quot;one or more digits&quot; followed by &quot;end of string&quot;. The answer is: /\A\d+\z\/ - finally  Question: Determine whether a string contains a positive integer. Discussion:
  • 31. Hashes Defination: Hashes are like arrays but scalar indexes. Example Defination: my %fruit; $fruit{&quot;red&quot;} = &quot;apple&quot;; $fruit{&quot;orange} = &quot;orange&quot;; $fruit{&quot;purple&quot;} = &quot;grape&quot;; %fruit = (&quot;red&quot;, &quot;apple&quot;, &quot;orange&quot;, &quot;orange&quot;, &quot;purple&quot;, &quot;grape&quot;); %fruit = (         &quot;red&quot; => &quot;apple&quot;,         &quot;orange&quot; => &quot;orange&quot;,         &quot;purple&quot; => &quot;grape&quot;, );
  • 32. Functions on Hashes: delete Keys values each Functions on Hashes #vi fruit.pl my %fruit = (         &quot;red&quot; => &quot;apple&quot;,         &quot;orange&quot; => &quot;orange&quot;,         &quot;purple&quot; => &quot;grape&quot;, ); for (keys %fruit) { print “$_ : $fruit{$_} \n”; } Why use Hash? Write a program which takes the input the city/capital name and returns the country name by Using Arrays and Hashes or any other method. So you know why use Hashes.
  • 33. References # Create some variables $a = &quot;mama mia&quot;; @array = (10, 20); %hash = (&quot;laurel&quot; => &quot;hardy&quot;, &quot;nick&quot; => &quot;nora&quot;); # Now create references to them $ra = \$a; # $ra now &quot;refers&quot; to (points to) $a $rarray = \@array; $rhash = \%hash; #You can create references to constant scalars in a similar fashion: $ra = \10; $rs = \&quot;hello world&quot;;
  • 34. References Dereferencing: Means getting at the value that a reference point to. #Look nicely: @array = (10, 20); $ra = \@array; is similar to $ra =[10, 20] # Notice the square braces. %hash = (&quot;laurel&quot; => &quot;hardy&quot;, &quot;nick&quot; => &quot;nora&quot;); $rhash = \%hash is similar to $rhash = { “laurel”=>”hardy”, “nick” => “nora” }; $ra = \$a; # First take a reference to $a $$ra += 2; # instead of $a += 2; print $$ra; # instead of print $a $rarray = \@array; push (@array , &quot;a&quot;, 1, 2); # Using the array as a whole push (@$rarray, &quot;a&quot;, 1, 2); # Indirectly using the ref. to the array $rhash = \%hash; print $hash{&quot;key1&quot;}; # Ordinary hash lookup print $$rhash{&quot;key1&quot;}; # hash replaced by $rhash
  • 35. Shortcuts with the Arrow Notation $rarray = \@array; print $rarray->[1] ; # The &quot;visually clean&quot; way $rhash = \%hash; print $rhash->{&quot;k1&quot;}; #instead of ........ print $$rhash{&quot;k1&quot;}; # or print ${$rhash}{&quot;k1&quot;}; References
  • 36. Subroutines Q. Why Subroutines exists in Programming? Ans. Do I need to Answer? Defining subroutine: #Defining subroutine: sub mysubroutinename { #### code to do that } How to execute subroutine? How to pass the arguments to subroutines? How to use those arguments passed to the subroutines? Return Values?
  • 37. Modules Perl Allows you to partition your code into one more reusable module. Modules is code reuse We will see: Define Modules using the package keyword Load pre-defined modules using “use” and “require” keywords. Access package specific variables and subroutines using “::” notation. Load Functions at Run Time.
  • 38.  
  • 39. Remember that modules are not intended to be standalone applications we'll never actually be running our module by itself . Instead, we'll be using our module to provide added functionality to a larger script Let us look at the example: Filename is SayHello.pm Modules (Cont…) Package SayHello; sub hello_package { return “Hello, Welcome to the world of Modules”; } 1;
  • 40. Use of “use” keyword This is simplest way to pull the module into your script Searches all the paths in the array @INC until it finds a file Use “require” command: What’s the difference then? When you load a module using use, the module is loaded and executed at compile time , even before the remaining portion of the script is compiled. require pulls in the code of the module at run-time; and if the file cannot be found generates a run time error Note on variables declared with “my” you will not be able to access it from outside the module If you must access a module variable from outside the package you are declaring it in, then you can declare it using the our function use and require – what is that?
  • 41.  
  • 42. For the decent example – may be if you are thinking of contributing to cpan.org- Have a look at http://en.wikipedia.org/wiki/Perl_module bless : This operator takes the reference and converts it into object. Some Useful work module – Employee.pm
  • 43.  
  • 44. CGI Check my other PPT Explore the application available at   http://perl-md5-login.sourceforge.net/ MD5 Algorithm for Encryption The module is used by Yahoo etc
  • 45. Example: Count Click Script #!/usr/bin/perl ######################### ## Author: Sopan Shewale ## Company: Persistent Systems Pvt Ltd ## This script is created for giving demo on click count. ## The Script is support to display increse/decrease ##click's, handles back button of browser, does not ##handle reload stuff. ## also it's based on sessions. ######################## use strict; use warnings; use CGI; use CGI::Session; use CGI::Cookie; my $q = new CGI(); my $sessionid = $q->cookie(&quot;CGISESSID&quot;) || undef; my $session = new CGI::Session(undef, $sessionid, {Directory=>'/tmp'}); $sessionid = $session->id(); my $cookie = new CGI::Cookie(-name=>'CGISESSID', -value=>$sessionid, -path=>&quot;/&quot;); print $q->header('text/html', -cookie=>$cookie); print $q->start_html(&quot;Welcome to Click Count Demo&quot;); print &quot;<h1>Welcome to Click Count Demo</h1>&quot;; my $count = $session->param('count'); ## count-is click count variable if(!defined($count)) { $session->param('count', 0); $count=0;} ### if session is first time created, set count=0 $session->param('count', $count); $count = $session->param('count'); #print &quot;<h1>The Click Count is: $count \n&quot;; ## Form stuff print $q->startform(-method=>'POST'); print $q->submit( -name=>&quot;Increase&quot;, -value=>'Increase1'); print $q->submit( -name=>&quot;Decrease&quot;, -value=>'Decrease1'); print $q->endform();
  • 46. Example: Count Click Script (Cont…) ## Which button is being pressed my $which_button = $q->param('Increase'); if(defined ($which_button)) { print &quot;Increase pressed&quot;; $count = increase_count($count); ## Increase the count since increase button is clicked $session->param('count', $count); }else { $which_button=$q->param('Decrease'); if(defined($which_button)){ print &quot;Decrease pressed&quot;; $count = decrease_count($count); ## Decrease the count since decrease button is clicked $session->param('count', $count); } else {print &quot;You have not pressed any button, seems you are typing/re-typing the same URL&quot;; } } $count = $session->param('count'); print &quot;<h1>The Click Count is: $count \n&quot;; print $q->end_html(); ## increases the count by 1 sub increase_count { my $number = shift; $number = $number +1; return $number; } ## decreases the count by 1 sub decrease_count { my $number = shift; $number = $number -1; return $number; }
  • 47. Using eval String Form: Expression Evaluation The Block Form: Exception Handling Possible to use eval for time-outs. A few more…
  • 49. TWiki
  • 50. bin: view, edit, search etc tools: mailnotify template: Templates (view.tmpl), also has skin related data lib: modules and required libraries, plugin code data: webs directories (e.g. Main,TWiki) and each directory inside contains the topics from that web. The topics are companied by there version history pub: Attachments of the topics and commonly shared data
  • 51. Basic request in TWiki from browser side, calls some script from bin Directory (like views) which Loads setlib.cfg from the same directory. Which loads LocalSite.cfg from lib directory. The LocalSite.cfg has site related Configuration variables defined. Then the relevant modules are loaded, topic are read, macros expanded, the content converted into html page and sent back to browser, request is completed. Now a days the LocalSite.cfg is written using Web-Interface, For example http://mytwikiste.com/bin/configure TWiki (Cont…)
  • 52. TWiki (Cont…) What is plugins?: One can add plugins to TWiki to extend the functionality of TWiki without altering the core code. Plugin approach lets us do following: add virtually unlimited features while keeping the main TWiki code compact and efficient; heavily customize an installation and still do clean updates to new versions of TWiki; rapidly develop new TWiki functions in Perl using the Plugin API.
  • 53. TWiki Community: The plugins are located at http://twiki.org/cgi-bin/view/Plugins Each plugin has couple of topics on the site for support, development, installation instruction. Let us look at example: http://twiki.org/cgi-bin/view/Plugins/CommentPlugin : Has comment Plugin, with installation instructions, syntax etc. http://twiki.org/cgi-bin/view/Plugins/CommentPluginDev : Used for support activity and comments from public. http://twiki.org/cgi-bin/view/Plugins/CommentPluginAppraisal : Appraisal purpose, any one can appraise the plugin. The plugin developer must read: TWiki::Func ( http://twiki.org/cgi-bin/view/TWiki/TWikiFuncModule ) code. That’s the Plugin API. TWiki (Cont…)
  • 54. General Instructions for creating Plugin The plugin name is in the format <name>Plugin. The code goes in <TWikiRootDirectory>/lib/TWiki/Plugings/ directory. TWiki provides plugin called EmptyPlugin which can be used as Template to develop new plugin. The code of Empty Plugin is <TWikiRootDirectory>/lib/TWiki/Plugins/EmptyPlugin.pm Create a <name>Plugin topic in the TWiki web. The topic contains the settings, installation instructions and syntax of the plugin. Enable Plugin by using configure e.g. http://yourtwikiinstalled.com/bin/configure TWiki (Cont…)
  • 55. HelloPlugin %HELLO{format=“Dear $first $last, $brWelcome to the world of TWiki” first=“Hari” last=“Sadu”}% Dear Hari Sadu, Welcome to the world of TWiki This should get expanded to:
  • 56.  
  • 57.  
  • 58. Database Connectivity DBI Module General process used in database activity Connect Prepare sql query Execute Finish Disconnect Best document available at http://search.cpan.org/src/TIMB/DBI_AdvancedTalk_2004/index.htm
  • 59. Exercise Q. 1: Given array, delete the duplicate entries – the order should be reserved e.g. For input Array - (“Proxy”, “ Proxy”, “smtp”, “smtp”, “smtp”, “mail”, 25) - output should be (“proxy”, “smtp”, “mail” 25);
  • 60. References Online http://learn.perl.org/library/beginning_perl/ - Simon Cozens http://ebb.org/PickingUpPerl/ http:// gnosis.cx/publish/programming/regular_expressions.html http://perldoc.perl.org/perlretut.html Books Advanced Perl Programming – By Sriram Srininivasan Learning Perl Objects, References, & Modules – Randal L. Schwartz Intermediate Perl – Randal L. Schwartz Perl for Systems Administration – David N. Blank Mastering Algorithms with Perl – Jon Orwant et all People to Track Randal L. Schwartz Simon Cozens Convey - ???
  • 61. References (Cont…) Tools Web Application Development - Frameworks CGI::Application ( http:// cgiapp.erlbaum.net / ) Catalyst ( http:// www.catalystframework.org / ) PageKit Web Application Framework ( http:// www.pagekit.org / ) Combust web site Framework ( http:// combust.develooper.com / ) Mason ( http://www.masonhq.com/ ) Embperl ( http:// perl.apache.org/embperl / ) Template Tools – used in Web Application Development Template Toolkit http://www.tt2.org/ HTML::Template http://html- template.sourceforge.net / Petal - http://search.cpan.org/~bpostle/Petal-2.19/lib/Petal.pm Other TWiki ( http://twiki.org ) – Enterprise Collaboration Platform Sympa - Mailing List Mhon-Arch – Mail Archiving Soltion Webmin – Unix System Administration Application