INTRODUCTION to PERL PART 1
Background Practical Extraction and Report Language → Perl Developed in 1987 by Larry Wall
SCALAR — The Basic Unit of Perl Scalar = number or string $scalar = “that’s me!”; # ‘$’ is used to declare scalars Default value: “undef” 0 as a number NULL as a string
SCALAR — The Basic Unit of Perl $alice = 5; $bob = “5”; Dynamically typed $alice == $bob # true Can be accessed inside a string literal print “What comes after 4? $alice”; # What comes after 4? 5
Operators Operators Assignment = Arithmetic + - * / String . x Comparison == != < > <= >= eq ne lt gt le ge
ASSIGNMENT OPERATOR $var = “value”; # value
ARITHMETIC OPERATORS $var = 10 + 2; # 2 $var = 10 – 2; # 8
STRING OPERATORS $var = “Hello ” . “world!”; # Hello world! $var = “Yeah! ” x 3; # Yeah! Yeah! Yeah!
NUMBER COMPARISON OPERATORS $alice == $bob; $alice != $bob; $alice < $bob; $alice > $bob; $alice <= $bob; $alice >= $bob;
STRING COMPARISON OPERATORS $alice eq $bob; $alice ne $bob; $alice lt $bob; $alice gt $bob; $alice le $bob; $alice ge $bob;
IF — CONTROL STRUCTURES if (condition) { # code goes here! } else {
While — Control Structures while (condition) { # code goes here! }
STANDARD INPUT <STDIN> $input = <STDIN>;
LISTS & Arrays List = collection of scalars (“that’s”, “me!”); # lists are enclosed in parentheses Array = variable that stores a list @array = (“that’s”, “me!”); # arrays are declared with ‘@’ Accessed with ‘$’ and ‘[]’ $item = $array[0]; # that’s