Download presentation
Presentation is loading. Please wait.
1
An Introduction to Perl
Part I Jonathan Stanley CS265
2
An Introduction to Perl
Part I Introduction Scalar Data Variables Operators If-Else Control Structures While Control Structures Standard Input Lists and Arrays
3
Introduction What is Perl?
Perl is a high-level dynamic programming language which was originally developed by Larry Wall in Perl borrows features from other programming languages including C and shell scripting. Perl is nicknamed "the Swiss Army chainsaw of programming languages" due to its flexibility and power. The newest version is 5.5.8
4
Scalar Data Numbers Strings Perl has two types of numbers:
Integers => 4 or 20 Floating point numbers => 2.3 or 4.5 Implementation => $var=4; or Implementation => $var=3.5; A string is a sequence of characters In Perl, strings can have single or double quotes. Implementation => $var=‘a’; Implementation => $var=“new/nLine”; In regards to the numbers, they have the same format. Strings can either be declared with single or double quotes (most programmers use double). There is a special value undef, which is initially assigned to all variables. It acts as zero when applied to numbers, and as an empty string when applied to strings.
5
Variables In Perl, variables are declared with the $ symbol.
They may contain a combination of letters, numbers, and underscores but cannot begin with a number. Comments are also denoted as #. $Num2=5; #Num2 has the value of 5 $my_name=‘Jon’; #my_name has the value “Jon”
6
==, !=, <, >, <=, >=
Numerical Operators Assignment Operators =, +=, -=, *=, /=, %=, **= $var=265; $var-=265; $var+=265; Arithmetic Operators +, -, *, /, %, ** $var=1+3; $var=9–4; $var=3*2; $var=8/2; $var=6%4; $var=3**2; Comparison Operators ==, !=, <, >, <=, >= $var==1; $var!=0; = Normal Assignment += Add and Assign -= Subtract and Assign *= Multiply and Assign /= Divide and Assign %= Modulus and Assign **= Exponent and Assign
7
Contamination Operator
String Operators Assignment Operators =, .= $var=“yes” $var.=“ or no” Comparison Operators eq, ne, lt, gt, le, ge $var eq “yes”; $var gt “apple”; Logical Operators &&, ||, ! ($var==“yes”) && ($var2==2) ($var==“yes”) || ($var2==2) ($var==“yes”) != ($var2==2) Contamination Operator . (dot symbol) “Jon”.“Stanley” Multiplier Operator x “yes”x3
8
If-Else Control Structures
if ($class eq “CS265”) { print “What a good day :)” ; } else print “What a bad day :(”; Always use curly braces!
9
While Control Structures
while($counter <= 3) { $counter++; print $counter; }
10
Standard Input $var=<STDIN> ; print “What is you name?";
chomp ($name = <>); print “What is your major?"; chomp ($major= <>); print “$name is currently studying $major at Drexel University."; <STDIN> stands for standard input. It can be abbreviated by using simple <>. This reads a full line from standard input and store it in a variable. The chomp function simply removes any erroneous line breaks and spacing from the end of our string. Chomp should be used whenever input is acquired from your users.
11
Lists and Arrays A list is an ordered collection of scalars while an array is a variable that holds lists. @list1=(“jon”, “jacob”, “jingleheimer”, “schmidt”); @list2=(0..8); #prints jacob #prints … 8 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. @list1.shift(); @list1.unshift(); #After both, list1 contains 2 entries. @list2.push(9); #adds 9 to the end
12
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.