Top Banner
Perl Tutorial For Novice Part 1 Suresh Solaimuthu
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Perl_Tutorial_v1

Perl Tutorial For NovicePart 1

Suresh Solaimuthu

Page 2: Perl_Tutorial_v1

History

• Creator, Maintainer, Chief Architect – Larry Wall

• Practical Extraction and Report Language

• Pathologically Eclectic Rubbish Lister

• Pearl

• Features from C, awk, tcl/tk

Page 3: Perl_Tutorial_v1

Basic

• Use any editor to write a Perl program

• Extension is .pl

• Run in Unix as $perl <filename>

• Make it executable and run as $./<filename>

Page 4: Perl_Tutorial_v1

Hello World!

• Always the first line is #!<pathtoperl>

• print prints to the standard output

• print can also be used for printing into files

#!/usr/local/bin/perl

print “Hello World\n”;

Page 5: Perl_Tutorial_v1

Standard Input/Output

• Get the input from the user using <STDIN>– $x = <STDIN> gets the input from the user

• Print to the standard output– print $x prints the value of $x

– print “hello “,”world”,”\n” prints hello world and newline character

– print “hello ”.”world”.”\n” also prints hello world and newline character

– So what's the difference?!?!

Page 6: Perl_Tutorial_v1

Data Types

• Scalar

• Arrays

• Associative

Page 7: Perl_Tutorial_v1

Scalar Variables

• Basic kind

• Can hold both numerics and strings and interchangeable– Eg.: $temp = ‘hi’

– $temp = 9

• Starts with “$” symbol followed by a letter and then by letters, numbers or underscores

• Case sensitive

Page 8: Perl_Tutorial_v1

Numbers

• Integers and Floats

• Internally, Perl computes with double float

• Integer Literals– 25

– 013 and 13 are different!!!!

• Float Literals– 1.3

– -13e-19 == -1.3E-19

Page 9: Perl_Tutorial_v1

Strings

• Sequence of characters

• Each character is 8-bit

• No limit on size!

Page 10: Perl_Tutorial_v1

String Literals

• Single quoted– Anything inside the quotation has no special

meaning except ' and \

– 'hey'

– 'hey\twazzup' is hey\twazzup

• Double quoted– Some characters have special meanings

– “hey\twazzup” is hey wazzup

Page 11: Perl_Tutorial_v1

Scalar Operators

• Numbers– Mathematical Operators +,-,/,*,%– Comparison Operators <, <=, ==, >=, >, !=

• String– Repetition – x

• “Hey” x 2 = “HeyHey”

– Concatenation - .• “James”.” “.”Bond” = “James Bond”

– Comparison lt, le, eq, ge, gt, ne

Page 12: Perl_Tutorial_v1

Number <--> String Operators

• Careful with the Operators!

• (1+1) x 3 = 222

• “a” + “b” is not an error

• Be CAREFUL!

Page 13: Perl_Tutorial_v1

Assignment Operators

• Assignment $LHS = $RHS– The value on the right is assigned to the left– $x = ($y = 13)– $x = $y = 13

• $x and $y has the value 13

• Binary Assignment– If the variable in LHS and RHS are same

– $x = $x + 13 $x += 5

– Similarly, for other binary operators

Page 14: Perl_Tutorial_v1

Auto [Increment, Decrement]

• Similar to C

• For both integers and float

• ++ operator adds 1 to its operand

• -- operator subtracts 1 from its operand

• $x = $y++ is different from $x = ++$y

Page 15: Perl_Tutorial_v1

Chop and Chomp

• Chop– Removes and returns the last character from the

input– $x = “huh\n”– chop ($x) makes $x = “huh”– chop ($x) makes $x = “hu”

• Chomp– Removes only the “\n” from the input– $x = “huh\n”;– chomp ($x) makes $x = “huh”– chomp ($x) makes $x = “huh”

Page 16: Perl_Tutorial_v1

Array

• List is ordered scalar data

• Array holds list

• No limits

• Array variable name starts with @– @var1

• Individual elements can be accessed using $– $var1[0] is the first element

Page 17: Perl_Tutorial_v1

Array Examples

• List literals– (1,2,3)

– (“hello”,1,1.2)

– ($x+$y,10)

– List constructor• (1..5) is (1,2,3,4,5)

• Array– @a = (“hey”,”how”,”are”,”you”)

Page 18: Perl_Tutorial_v1

Array Functions

• Sort– @x = sort (@y) will sort the array y and store it

in x• @x = sort (“b”,”a”,”c”) will make @x = (“a”,”b”,”c”)

• @x = sort (3,12,4,15) will make @x = (12,14,3,4)!!

• Sort by number– @x = sort {$a <=> $b} (3,12,4,15) will make @x

= (3,4,12,15)

Page 19: Perl_Tutorial_v1

Array Functions (cont.)

• Reverse reverses the order of the elements in the array– @x = reverse (3,2,8) will make @x = (8,2,3)

• Chomp removes the “\n” from all the elements of the array– @x = chomp (“hello\n”,”hey\n”) will make @x =

(“hello”,”hey”)

Page 20: Perl_Tutorial_v1

Regular Expressions

• Useful and Powerful string manipulation functions

• RE is a pattern to be matched against a string

• The regular expression is contained within slashes and the matching operator is =~

Page 21: Perl_Tutorial_v1

Is it easy?!?

• To find a pattern “hahaha” in a string $x– $x =~ /hahaha/

– If the above statement is true then “hahaha” is present in $x

Page 22: Perl_Tutorial_v1

Regular Expression Characters

• Some special regular expression characters– . Single Character except newline

– ^ Beginning of line

– $ End of line

– * Zero or more of the last character

– + One of more of the last character

– ? Zero or one of the last character

Page 23: Perl_Tutorial_v1

Examples

• p.f

• ^the

• end$

• abac*

• ^$

Page 24: Perl_Tutorial_v1

Some more symbols

• Square brackets– To match any one character inside the bracket

– Inside the bracket “^” indicates not

– And “-” indicates between

• Parenthesis– To group characters together

• “|”– Either or

Page 25: Perl_Tutorial_v1

Examples

• [aeiou]

• [^aeiou]

• [a-z]

• [0-9]

• [a-zA-Z0-9]

• hello|hey

• (ab)*

Page 26: Perl_Tutorial_v1

Substitution

• $varname =~ s/old/new– The regular expression old will be replaced by

new

• $varname =~ s/old/new/g– All the old regular expressions will be replaced

by new

Page 27: Perl_Tutorial_v1

Split

• Splits a string based on the regular expression given– @parts = split (/<regExp>/, $x)

– Eg.: $x = 1:2:3:4

– @parts = split (/:/, $x)

– @parts = (1,2,3,4)

Page 28: Perl_Tutorial_v1

To be Continued!