SlideShare a Scribd company logo
Basic Introduction To Perl Programming
Perl is a highly capable, feature-rich programming language.  Perl can be used both in Procedure Oriented & Object Oriented mode. Perl as a programming is preferred when application extensively requires text processing (basically used in Network Programming e.g. Socket class programming) CGI” stands for “Common Gateway Interface.  CGI is a program intended to be run on the web.
#!/usr/bin/perl The above line specifies the code following it is a perl program and should be save with a .pl extension Scalar Variables   Contains a single piece of data i.e. 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 Example:  #!/usr/bin/perl  # DEFINE SOME SCALAR VARIABLES  $number = 5;  print $number;
Array Variables( List Variables)  Arrays contain a list of scalar data (single elements). A list can hold an unlimited number of elements. In Perl, arrays are defined with the at (@) symbol Example: #!/usr/bin/perl #DEFINE SOME ARRAYS @days = ("Monday", "Tuesday", "Wednesday"); print @days;  print  “@days”; [Displays the o/p nicely] Hash  Hashes are complex lists with both a  key  and a  value  part for each element of the list. We define a hash using the percent symbol (%).
Example  #!/usr/bin/perl #DEFINE SOME ARRAYS %coins = ("Quarter", 25, "Dime", 10, "Nickle", 5); print %coins; File names, variables, and arrays are all case sensitive. If you capitalize variable  name when you define it, you must capitalize it to call it .
To define a string we use single or double quotations, you may also define them with the  q  subfunction.  Example   #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #HTTP HEADER # DEFINE SOME STRINGS $single = 'This string is single quoted'; $double = &quot;This string is double quoted&quot;; $userdefined = q^Carrot is now our quote^; print $single.&quot;<br />&quot;; print $double.&quot;<br />&quot;; print $userdefined.&quot;<br />&quot;;
String Manipulation   Character Description \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 Applies a carriage return \t Applies a tab to the string \f Applies a form feed to the string \b Backspace
Character Description \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  #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #HTTP HEADER # STRINGS TO BE FORMATTED $mystring = &quot;welcome to TCS!&quot;;  $newline = &quot;welcome to  \n TCS!&quot;; $capital = &quot; \u welcome to TCS!&quot;; $ALLCAPS = &quot; \U welcome to TCS!&quot;; # PRINT THE NEWLY FORMATTED STRINGS print $mystring.&quot;<br />&quot;; print $newline.&quot;<;br />&quot;; print $capital.&quot;<br />&quot;; print $ALLCAPS&quot;;
String Method  Substring()  :  Used to grab a sub string out of a string as well as replace a substring with another.  Example   #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;;  # DEFINE A STRING TO REPLACE $mystring = &quot;Hello,  I am Bunty Ray!&quot;; # PRINT THE ORIGINAL STRING print &quot;Original String: $mystring<br />&quot;; # STORE A SUB STRING OF $mystring, OFFSET OF 7 $substringoffset = substr($mystring, 7); print &quot;Offset of 7: $substringoffset<br />&quot;;  O/p:  Hello,  I am Bunty Ray  I am Bunty Ray
Arithmetic Operators Assignment Operator Logical Operator Operator Use + Addition - Subtraction * Multiplication ** Exponents % Modulus / Division Operator Use Syntax + = Addition ($x += 10) - = Subtraction ($x -= 10) * = Multiplication ($x **= 10) ** = Exponents ($x **= 10) % = Modulus ($x %= 10) / = Division ($x /= 10) Operator Use &&, and Associates two variables using AND ||, or  Associates two variables using OR
Relational Operators Operator Use Syntax Result ==,eq Equal To 5 == 5 5 eq 5 True != ,ne Not Equal To 6 !=5 6 ne 5 True < ,lt Less Than 7<4 7 lt 4 False >,gt Greater Than 7>4 7 gt 4 True <=,le Less Than Equal To 7<== 11 7 le 11 True >=, ge Greater Than Equal To 7>=11 7 ge 11 False
Indexing  Each element of the array can be indexed using a scalar version of the same array. When an array is defined, PERL automatically numbers each element in the array beginning with zero. This phenomenon is termed  array indexing. Example: #!/usr/bin/perl  print &quot;content-type: text/html \n\n&quot;;  # DEFINE AN ARRAY  @coins = (&quot;Quarter&quot;,&quot;Dime&quot;,&quot;Nickel&quot;);  # PRINT THE WHOLE ARRAY  print &quot;@coins&quot;;  print &quot;<br />&quot;;  print $coins[0]; #Prints the first element Quater print &quot;<br />&quot;;  print $coins[1]; #Prints the 2nd element Dime Elements can also be indexed backwards using  negative  integers instead of positive numbers. print $coins[-1]  #Prints the last element Nickel print $coins[-2]; #Prints 2nd to last element Dime
qw Subroutine If the array you wish to build has more than 5 elements. Use this neat little subroutine to remove the need for quotes around each element when you define an array. Syntax : @coins = qw(Quarter Dime Nickel ….); Length of an Array Retrieving a numerical value that represents the length of an array is a two step  process. First, you need to set the array to a scalar variable, then just print the new variable to the browser as shown below. Example @nums = (1 .. 20); print scalar(@nums).&quot;<br />&quot;; $nums = @nums; print &quot;There are $nums numerical elements<br />&quot;; # In both cases it prints 20
Adding and Removing Elements from & to an Array Example @solom = (“Manoj”,”Mukesh”,”Suresh); push(@solom, Ramesh); # Adds Ramesh to the end of the Array delete $solom [1];  # Removes the value on index 1 i.e. Mukesh Methods use push() adds an element to the end of an array.  unshift() adds an element to the beginning of an array pop() removes the last element of an array shift()  removes the first element of an array.
splice() Replacing elements is possible with the  splice()  function Syntax: splice(@array,first-element,sequential_length,name of new elements). Example @nums = (1..20); splice(@nums, 5,5,21..25);  print &quot;@nums&quot;; Here in the above example the replacement begins after the 5th element, starting  with the number 6 in the example above. Five elements are then replaced from 6-10 with the numbers 21-25. split() Transform a string into an array. The   split function requires two arguments, first the  character of which to split and also the string variable .
Example #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #HTTP HEADER # DEFINED STRINGS $astring = &quot;Rain-Drops-On-Roses-And-Whiskers-On-Kittens&quot;; $namelist = &quot;Larry,David,Roger,Ken,Michael,Tom&quot;; # STRINGS ARE NOW ARRAYS @array = split('-',$astring); @names = split(',',$namelist); # PRINT THE NEW ARRAYS print @array.&quot;<br />&quot;; print &quot;@names&quot;; In the first example, the split was called at each hyphen. In the latter example the names were split by a comma, allowing for the split to take place between each name O/P : Rain Drops On Roses And Whiskers On Kittens   Larry David Roger Ken Michael Tom
join() join() function to rejoin the array elements and form one long, scalar string. #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #HTTP HEADER # A COUPLE OF ARRAYS @array = (&quot;David&quot;,&quot;Larry&quot;,&quot;Roger&quot;,&quot;Ken&quot;,&quot;Michael&quot;,&quot;Tom&quot;); @array2 = qw(Pizza Steak Chicken Burgers); # JOIN 'EM TOGETHER $firststring = join(&quot;, &quot;,@array); $secondstring = join(&quot; &quot;,@array2); # PRINT THE STRINGS print &quot;$astring<br />&quot;; print &quot;$string&quot;; O/p: David,Larry,Roger,Ken,Michael,Tom   Pizza Steak Chicken Burgers
Control Structures If / unless statements While / until statements For statements Foreach statements Last , next , redo statements && And || as control structures
In PERL files are given a name, a handle, basically another way of saying alias. All input and output with files is achieved through filehandling. Filehandles are also a means by one program may communicate with another program. Assigning Handles A filehandle is nothing more than a nickname for the files you intend to use in your PERL scripts and programs. A handle is a temporary name assigned to a file. Example #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;;  $FilePath = “/export/home/bray/test.html&quot; open( HANDLE,  $FilePath, O_RDWR) or die &quot;$filepath cannot be opened.&quot;; printf HANDLE &quot;Welcome to TCS!&quot;; close (HANDLE); die Function  It is used to kill your scripts and helps pinpoint where/if your code is failing Assigning The handle
Open A File #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #The header $FH = &quot;filehandle&quot;; $FilePath = &quot;myhtml.html&quot;; open(FH, $FilePath, permissions); or sysopen(FH, $FileName, permission); Permission Opening The File O_RDWR Read and Write O_RDONLY Read Only O_WRONLY Write Only O_CREAT Create the file O_EXCL Stops if file already exists O_APPEND Append the file O_NONBLOCK Non-Blocking  usability
Following the example above when we called our HTML file handle using the input operator, PERL automatically stored each line of the file into a global array . #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #The header $HTML = &quot;myhtml.html&quot;; open (HTML) or die &quot;Can't open the file!&quot;; @fileinput = <HTML>; print $fileinput[0]; print $fileinput[1]; print $fileinput[2]; print &quot;<table border='1' align='center'><tr> <td>Dynamic</td><td>Table</td></tr>&quot;; print &quot;<tr><td>Temporarily Inserted</td> <td>Using PERL!</td></tr></table>&quot;; print $fileinput[3]; close (HTML);
Remove a File Remove Multiple Files #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #The header $file = &quot;newtext.txt&quot;; if (unlink($file) == 0) { print &quot;File deleted successfully.&quot;; } else { print &quot;File was not deleted.&quot;; } #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #The header @files = (&quot;newtext.txt&quot;,&quot;moretext.txt&quot;,&quot;yetmoretext.txt&quot;); foreach $file (@files) { unlink($file); }
Compile /usr/bin/perl –wc <code.pl> Execute ./code.pl 08/05/10 Document Owner : BUNTY RAY
Ad

More Related Content

What's hot (19)

Abuse Perl
Abuse PerlAbuse Perl
Abuse Perl
Casey West
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
Sopan Shewale
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
Elie Obeid
 
Cleancode
CleancodeCleancode
Cleancode
hendrikvb
 
Subroutines in perl
Subroutines in perlSubroutines in perl
Subroutines in perl
sana mateen
 
Perl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally InsanePerl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally Insane
Ricardo Signes
 
Scalar data types
Scalar data typesScalar data types
Scalar data types
Krasimir Berov (Красимир Беров)
 
Perl 101 - The Basics of Perl Programming
Perl  101 - The Basics of Perl ProgrammingPerl  101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl Programming
Utkarsh Sengar
 
Regular expressions in Perl
Regular expressions in PerlRegular expressions in Perl
Regular expressions in Perl
Girish Manwani
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
Logan Palanisamy
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
Dave Cross
 
Subroutines
SubroutinesSubroutines
Subroutines
Krasimir Berov (Красимир Беров)
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
Dave Cross
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
Varadharajan Mukundan
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
Dave Cross
 
Awk programming
Awk programming Awk programming
Awk programming
Dr.M.Karthika parthasarathy
 
perl course-in-mumbai
 perl course-in-mumbai perl course-in-mumbai
perl course-in-mumbai
vibrantuser
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
Krasimir Berov (Красимир Беров)
 

Similar to CGI With Object Oriented Perl (20)

LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
Dave Cross
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Dave Cross
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
Marcos Rebelo
 
Tutorial perl programming basic eng ver
Tutorial perl programming basic eng verTutorial perl programming basic eng ver
Tutorial perl programming basic eng ver
Qrembiezs Intruder
 
Php2
Php2Php2
Php2
Keennary Pungyera
 
Perl Xpath Lightning Talk
Perl Xpath Lightning TalkPerl Xpath Lightning Talk
Perl Xpath Lightning Talk
ddn123456
 
PERL Unit 6 regular expression
PERL Unit 6 regular expressionPERL Unit 6 regular expression
PERL Unit 6 regular expression
Binsent Ribera
 
Javascript
JavascriptJavascript
Javascript
vikram singh
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Raghavan Mohan
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
Bernard Loire
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
birbal
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
zone
 
Javascript
JavascriptJavascript
Javascript
guest03a6e6
 
John Rowley Notes
John Rowley NotesJohn Rowley Notes
John Rowley Notes
IBAT College
 
Prototype js
Prototype jsPrototype js
Prototype js
mussawir20
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administration
Mohammed Farrag
 
perltut
perltutperltut
perltut
tutorialsruby
 
Php Loop
Php LoopPhp Loop
Php Loop
lotlot
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
勇浩 赖
 
LPW: Beginners Perl
LPW: Beginners PerlLPW: Beginners Perl
LPW: Beginners Perl
Dave Cross
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Dave Cross
 
Tutorial perl programming basic eng ver
Tutorial perl programming basic eng verTutorial perl programming basic eng ver
Tutorial perl programming basic eng ver
Qrembiezs Intruder
 
Perl Xpath Lightning Talk
Perl Xpath Lightning TalkPerl Xpath Lightning Talk
Perl Xpath Lightning Talk
ddn123456
 
PERL Unit 6 regular expression
PERL Unit 6 regular expressionPERL Unit 6 regular expression
PERL Unit 6 regular expression
Binsent Ribera
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Raghavan Mohan
 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
Bernard Loire
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
birbal
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
zone
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administration
Mohammed Farrag
 
Php Loop
Php LoopPhp Loop
Php Loop
lotlot
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
勇浩 赖
 
Ad

Recently uploaded (20)

Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Ad

CGI With Object Oriented Perl

  • 1. Basic Introduction To Perl Programming
  • 2. Perl is a highly capable, feature-rich programming language. Perl can be used both in Procedure Oriented & Object Oriented mode. Perl as a programming is preferred when application extensively requires text processing (basically used in Network Programming e.g. Socket class programming) CGI” stands for “Common Gateway Interface. CGI is a program intended to be run on the web.
  • 3. #!/usr/bin/perl The above line specifies the code following it is a perl program and should be save with a .pl extension Scalar Variables Contains a single piece of data i.e. 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 Example: #!/usr/bin/perl # DEFINE SOME SCALAR VARIABLES $number = 5; print $number;
  • 4. Array Variables( List Variables) Arrays contain a list of scalar data (single elements). A list can hold an unlimited number of elements. In Perl, arrays are defined with the at (@) symbol Example: #!/usr/bin/perl #DEFINE SOME ARRAYS @days = (&quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;); print @days; print “@days”; [Displays the o/p nicely] Hash Hashes are complex lists with both a key and a value part for each element of the list. We define a hash using the percent symbol (%).
  • 5. Example #!/usr/bin/perl #DEFINE SOME ARRAYS %coins = (&quot;Quarter&quot;, 25, &quot;Dime&quot;, 10, &quot;Nickle&quot;, 5); print %coins; File names, variables, and arrays are all case sensitive. If you capitalize variable name when you define it, you must capitalize it to call it .
  • 6. To define a string we use single or double quotations, you may also define them with the q subfunction. Example #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #HTTP HEADER # DEFINE SOME STRINGS $single = 'This string is single quoted'; $double = &quot;This string is double quoted&quot;; $userdefined = q^Carrot is now our quote^; print $single.&quot;<br />&quot;; print $double.&quot;<br />&quot;; print $userdefined.&quot;<br />&quot;;
  • 7. String Manipulation Character Description \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 Applies a carriage return \t Applies a tab to the string \f Applies a form feed to the string \b Backspace
  • 8. Character Description \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
  • 9. Example #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #HTTP HEADER # STRINGS TO BE FORMATTED $mystring = &quot;welcome to TCS!&quot;; $newline = &quot;welcome to \n TCS!&quot;; $capital = &quot; \u welcome to TCS!&quot;; $ALLCAPS = &quot; \U welcome to TCS!&quot;; # PRINT THE NEWLY FORMATTED STRINGS print $mystring.&quot;<br />&quot;; print $newline.&quot;<;br />&quot;; print $capital.&quot;<br />&quot;; print $ALLCAPS&quot;;
  • 10. String Method Substring() : Used to grab a sub string out of a string as well as replace a substring with another. Example #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; # DEFINE A STRING TO REPLACE $mystring = &quot;Hello, I am Bunty Ray!&quot;; # PRINT THE ORIGINAL STRING print &quot;Original String: $mystring<br />&quot;; # STORE A SUB STRING OF $mystring, OFFSET OF 7 $substringoffset = substr($mystring, 7); print &quot;Offset of 7: $substringoffset<br />&quot;; O/p: Hello, I am Bunty Ray I am Bunty Ray
  • 11. Arithmetic Operators Assignment Operator Logical Operator Operator Use + Addition - Subtraction * Multiplication ** Exponents % Modulus / Division Operator Use Syntax + = Addition ($x += 10) - = Subtraction ($x -= 10) * = Multiplication ($x **= 10) ** = Exponents ($x **= 10) % = Modulus ($x %= 10) / = Division ($x /= 10) Operator Use &&, and Associates two variables using AND ||, or Associates two variables using OR
  • 12. Relational Operators Operator Use Syntax Result ==,eq Equal To 5 == 5 5 eq 5 True != ,ne Not Equal To 6 !=5 6 ne 5 True < ,lt Less Than 7<4 7 lt 4 False >,gt Greater Than 7>4 7 gt 4 True <=,le Less Than Equal To 7<== 11 7 le 11 True >=, ge Greater Than Equal To 7>=11 7 ge 11 False
  • 13. Indexing Each element of the array can be indexed using a scalar version of the same array. When an array is defined, PERL automatically numbers each element in the array beginning with zero. This phenomenon is termed array indexing. Example: #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; # DEFINE AN ARRAY @coins = (&quot;Quarter&quot;,&quot;Dime&quot;,&quot;Nickel&quot;); # PRINT THE WHOLE ARRAY print &quot;@coins&quot;; print &quot;<br />&quot;; print $coins[0]; #Prints the first element Quater print &quot;<br />&quot;; print $coins[1]; #Prints the 2nd element Dime Elements can also be indexed backwards using negative integers instead of positive numbers. print $coins[-1] #Prints the last element Nickel print $coins[-2]; #Prints 2nd to last element Dime
  • 14. qw Subroutine If the array you wish to build has more than 5 elements. Use this neat little subroutine to remove the need for quotes around each element when you define an array. Syntax : @coins = qw(Quarter Dime Nickel ….); Length of an Array Retrieving a numerical value that represents the length of an array is a two step process. First, you need to set the array to a scalar variable, then just print the new variable to the browser as shown below. Example @nums = (1 .. 20); print scalar(@nums).&quot;<br />&quot;; $nums = @nums; print &quot;There are $nums numerical elements<br />&quot;; # In both cases it prints 20
  • 15. Adding and Removing Elements from & to an Array Example @solom = (“Manoj”,”Mukesh”,”Suresh); push(@solom, Ramesh); # Adds Ramesh to the end of the Array delete $solom [1]; # Removes the value on index 1 i.e. Mukesh Methods use push() adds an element to the end of an array. unshift() adds an element to the beginning of an array pop() removes the last element of an array shift() removes the first element of an array.
  • 16. splice() Replacing elements is possible with the splice() function Syntax: splice(@array,first-element,sequential_length,name of new elements). Example @nums = (1..20); splice(@nums, 5,5,21..25); print &quot;@nums&quot;; Here in the above example the replacement begins after the 5th element, starting with the number 6 in the example above. Five elements are then replaced from 6-10 with the numbers 21-25. split() Transform a string into an array. The split function requires two arguments, first the character of which to split and also the string variable .
  • 17. Example #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #HTTP HEADER # DEFINED STRINGS $astring = &quot;Rain-Drops-On-Roses-And-Whiskers-On-Kittens&quot;; $namelist = &quot;Larry,David,Roger,Ken,Michael,Tom&quot;; # STRINGS ARE NOW ARRAYS @array = split('-',$astring); @names = split(',',$namelist); # PRINT THE NEW ARRAYS print @array.&quot;<br />&quot;; print &quot;@names&quot;; In the first example, the split was called at each hyphen. In the latter example the names were split by a comma, allowing for the split to take place between each name O/P : Rain Drops On Roses And Whiskers On Kittens Larry David Roger Ken Michael Tom
  • 18. join() join() function to rejoin the array elements and form one long, scalar string. #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #HTTP HEADER # A COUPLE OF ARRAYS @array = (&quot;David&quot;,&quot;Larry&quot;,&quot;Roger&quot;,&quot;Ken&quot;,&quot;Michael&quot;,&quot;Tom&quot;); @array2 = qw(Pizza Steak Chicken Burgers); # JOIN 'EM TOGETHER $firststring = join(&quot;, &quot;,@array); $secondstring = join(&quot; &quot;,@array2); # PRINT THE STRINGS print &quot;$astring<br />&quot;; print &quot;$string&quot;; O/p: David,Larry,Roger,Ken,Michael,Tom Pizza Steak Chicken Burgers
  • 19. Control Structures If / unless statements While / until statements For statements Foreach statements Last , next , redo statements && And || as control structures
  • 20. In PERL files are given a name, a handle, basically another way of saying alias. All input and output with files is achieved through filehandling. Filehandles are also a means by one program may communicate with another program. Assigning Handles A filehandle is nothing more than a nickname for the files you intend to use in your PERL scripts and programs. A handle is a temporary name assigned to a file. Example #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; $FilePath = “/export/home/bray/test.html&quot; open( HANDLE, $FilePath, O_RDWR) or die &quot;$filepath cannot be opened.&quot;; printf HANDLE &quot;Welcome to TCS!&quot;; close (HANDLE); die Function It is used to kill your scripts and helps pinpoint where/if your code is failing Assigning The handle
  • 21. Open A File #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #The header $FH = &quot;filehandle&quot;; $FilePath = &quot;myhtml.html&quot;; open(FH, $FilePath, permissions); or sysopen(FH, $FileName, permission); Permission Opening The File O_RDWR Read and Write O_RDONLY Read Only O_WRONLY Write Only O_CREAT Create the file O_EXCL Stops if file already exists O_APPEND Append the file O_NONBLOCK Non-Blocking usability
  • 22. Following the example above when we called our HTML file handle using the input operator, PERL automatically stored each line of the file into a global array . #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #The header $HTML = &quot;myhtml.html&quot;; open (HTML) or die &quot;Can't open the file!&quot;; @fileinput = <HTML>; print $fileinput[0]; print $fileinput[1]; print $fileinput[2]; print &quot;<table border='1' align='center'><tr> <td>Dynamic</td><td>Table</td></tr>&quot;; print &quot;<tr><td>Temporarily Inserted</td> <td>Using PERL!</td></tr></table>&quot;; print $fileinput[3]; close (HTML);
  • 23. Remove a File Remove Multiple Files #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #The header $file = &quot;newtext.txt&quot;; if (unlink($file) == 0) { print &quot;File deleted successfully.&quot;; } else { print &quot;File was not deleted.&quot;; } #!/usr/bin/perl print &quot;content-type: text/html \n\n&quot;; #The header @files = (&quot;newtext.txt&quot;,&quot;moretext.txt&quot;,&quot;yetmoretext.txt&quot;); foreach $file (@files) { unlink($file); }
  • 24. Compile /usr/bin/perl –wc <code.pl> Execute ./code.pl 08/05/10 Document Owner : BUNTY RAY

Editor's Notes

  • #2: BUNTY RAY BT-CISP Infrastructure Designer