2. Hello World – First Program $ cat HelloWorld.pl #!/usr/bin/perl print "Welcome to the world of Perl Programming !"; $ 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 "Please write your name:"; $user = <STDIN>; print "Welcome to the world of perl, You are : $user";
4. Simple example – way to execute external command [sopan@ps3724 tutorial]$ cat df.pl #!/usr/bin/perl print "Method1:"; my $out1 = system("df -k"); print "The output of df -k command is : $out1"; 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 "Method2:"; my $out2 = `df -k`; print "The output of df -k command is : $out2"; 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.
7.
8.
9.
10.
11.
12.
13.
14. Regular expression (Cont…) Let us look at the example if ("Hello World " =~ /World/) { print "It matches"; } else { print "It doesn't match"; } The sense of =~ is reversed by !~ operator.
15. Regular Expression (Cont…) The literal string in the regexp can be replaced by a variable: $greeting = "World"; if ("Hello World" =~ /$greeting/) { print "It matches"; } else { print "It doesn't match"; } Regexp’s are case sensitive
16.
17.
18.
19.
20.
21.
22.
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 "abcde". Regular Expression (Cont…)
24.
25.
26.
27.
28.
29.
30.
31.
32.
33. References # Create some variables $a = "mama mia"; @array = (10, 20); %hash = ("laurel" => "hardy", "nick" => "nora"); # Now create references to them $ra = a; # $ra now "refers" to (points to) $a $rarray = array; $rhash = hash; #You can create references to constant scalars in a similar fashion: $ra = 0; $rs = amp;quot;hello world";
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
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("CGISESSID") || undef; my $session = new CGI::Session(undef, $sessionid, {Directory=>'/tmp'}); $sessionid = $session->id(); my $cookie = new CGI::Cookie(-name=>'CGISESSID', -value=>$sessionid, -path=>"/"); print $q->header('text/html', -cookie=>$cookie); print $q->start_html("Welcome to Click Count Demo"); print "<h1>Welcome to Click Count Demo</h1>"; 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 "<h1>The Click Count is: $count "; ## Form stuff print $q->startform(-method=>'POST'); print $q->submit( -name=>"Increase", -value=>'Increase1'); print $q->submit( -name=>"Decrease", -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 "Increase pressed"; $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 "Decrease pressed"; $count = decrease_count($count); ## Decrease the count since decrease button is clicked $session->param('count', $count); } else {print "You have not pressed any button, seems you are typing/re-typing the same URL"; } } $count = $session->param('count'); print "<h1>The Click Count is: $count "; 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; }
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