cs 105 perl: data types nathan clement 15 may 2014
TRANSCRIPT
![Page 1: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/1.jpg)
CS 105 Perl:Data Types
Nathan Clement
15 May 2014
![Page 2: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/2.jpg)
Agenda
• Paper Survey• Perl’s basic data types– Scalars– Arrays– Hashes
• Definedness• Truth• Basic control flow– if statements– while loops
![Page 3: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/3.jpg)
Carriage Returns
/usr/bin/perl;^M: bad interpreter: No such file or directory
• UNIX/Windows environment problem– Newline– Fixing it
![Page 4: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/4.jpg)
Types of variables
Many languages such as C, C++, and Java:• Primitive data types– Integers, characters,
floating-point numbers, booleans
• Composite data types– Arrays, Structures,
Classes
In Perl• Singular: scalars• Plural– arrays– hashes
![Page 5: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/5.jpg)
Sigils
A sigil is a prefix that denotes the type of the value being specified
Sigils for Perl’s fundamental data types:• $ for scalars• @ for arrays• % for hashes
![Page 6: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/6.jpg)
Sigils
• You might be asking at this point,– What are all those $@%* signs for?
• My response would be:– Watch your $@*$!% mouth, buddy!
![Page 7: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/7.jpg)
Scalars
Scalars can store both numbers and strings.The following are all valid values for scalars:• 0• 3.14159• "" (empty string)• "Just a string"
![Page 8: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/8.jpg)
Example Scalars
Initializing scalars with constants
$zero = 0;$pi = 3.14159;$empty = "";$foo = "just a string";$atoms = 6.022e23;
![Page 9: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/9.jpg)
Identifiers
Identifiers are the names of variables.Valid identifiers in Perl• Must begin with a letter or underscore• Can contain letters, numbers, and underscores• Are case sensitive (Foo and foo are distinct)• Like C, Java, and many other languages
![Page 10: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/10.jpg)
Manipulating numeric scalars
You can do typical arithmetic with Perl scalars.$m = ($y2 - $y1) / ($x2 - $x1);$y = $m * $x + $b;$a += $b; # same as $a = $a + $b;$a++; # $a += 1;Perl even has an exponentiation operator: **$result = $base ** $exponent;
![Page 11: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/11.jpg)
Manipulating string scalars
You can manipulate string values, too.Concatenation (. operator):$a = "foo";$b = "bar";$c = $a . $b; # "foobar"$c .= $a; # "foobarfoo"For more operators, see perlop.
![Page 12: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/12.jpg)
Sigils: Example
• $a is a scalar• @a is an array• %a is a hash
Remember that a sigil denotes the type of the value, not the type of the variable. For example,• $a[0] is a scalar member of the array @a
![Page 13: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/13.jpg)
Using sigils
Sigils denote the type of the value, not the type of the variable.$a is a scalar value stored in the scalar variable $a.$a[0] is a scalar value stored in the array @a.$a{“foo”} is a scalar value stored in the hash %a.
The three data types have separate namespaces: $a, @a, and %a can all coexist
Sigil rule
![Page 14: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/14.jpg)
Arrays and Hashes: An Overview
Arrays and Hashes• are containers or collections• store scalarsArrays (@)• ordered• indexed by integers• their index is specified inside square brackets [ ]Hashes (%)• unordered• indexed by strings (called keys)• their index is specified inside curly brackets { }
![Page 15: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/15.jpg)
Setting and Using Array Elements
$a[0] = "foo";$a[1] = "bar";$a[100] = 1;
$a[2] = $a[0] . $a[$a[100]]; # "foobar"
Although we’re using the scalar sigil ($),all the data we’ve modified is in @a.You can copy arrays.@b = @a;
![Page 16: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/16.jpg)
Setting and Using Hash Elements
$a{"foo"} = "bar";$a{"bar"} = "quux";$a{"foobaz"} = $a{"foo"} . $a{$a{"foo"}};
print $a{"foobaz"}; # displays "barquux"
Although we’re using the scalar sigil ($),all the data we’ve modified is in %a.You can copy hashes, too.%b = %a;
![Page 17: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/17.jpg)
Sigil Rule
• How to tell the difference?
• Remember dwimmy?
Context
![Page 18: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/18.jpg)
Definedness
We can refer to Perl variables that technically don’t exist. Such variables are undefined.If we’ve never set the value of a scalar, it’s undefined.# no scalars have been defined yet$a = 10;But we can use such a variable, and Perl won’t complain (by default). Its value will be undefined, however.The undefined value is called undef.
![Page 19: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/19.jpg)
Definedness, continued
# no scalars have been defined yet$a = 10;$a = $b;But $b has never been initialized; it is undef.So $a has been set to undef.
![Page 20: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/20.jpg)
Controlling Definedness
Variables can be set to undef in two ways.• setting a variable to undef (noun form)• undefining a variable with undef (verb form)$a = undef; # undef as nounundef $b; # undef as verbSetting a variable to any other value causes it to be defined.
![Page 21: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/21.jpg)
Definedness for Arrays and Hashes
undef can be used to undefine arrays and hashes, too, but only in the verb form.undef @a; # @a ceases to existundef %b; # Goodbye, %b.@a = undef; # WRONG # actually @a = (undef);
An empty array is not undef, nor is an empty hash.
![Page 22: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/22.jpg)
Testing for Definedness
Test whether a variable is defined with defined.$a = 10;defined($a); # returns truedefined($b); # returns false
![Page 23: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/23.jpg)
Truth
Five values in Perl are false.• undef• ""• 0• "0"• ()Everything else is true.These rules are defined in perlsyn at the Truth and Falsehood heading.
![Page 24: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/24.jpg)
Manipulating Truth
Perl has the following logical operators:• Negation !• Logical and &&• Logical or ||just like C, C++, Java…
![Page 25: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/25.jpg)
The Truthiness of Truth Operators
How negation (!) works:• !$a returns the empty string if $a is true• !$a returns 1 if $a is false
![Page 26: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/26.jpg)
More Truthiness of Truth Operators
How logical and (&&) works: $a && $b returns• $a if $a is false• $b otherwise
How logical or (||) works: $a || $b returns• $a if $a is true• $b otherwise
![Page 27: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/27.jpg)
A word about functions
Perl comes with a lot of built-in functions.We’ve used several of them already:• print• defined• undefTo learn about the rest, see perlfunc.
![Page 28: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/28.jpg)
if statement
A simple example of an if statement:if ($a) { print "the variable is true\n";}
![Page 29: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/29.jpg)
if with else
if ($rich) { print "I am the 1\%\n";} elsif ($poor) { print "I’m economically disadvantaged\n";} else { print "I’m disappearing!!\n";}
How to handle the age-old “Dangling Else” problem
![Page 30: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/30.jpg)
A Linguistic Twist
Perl allows conditionals to follow the statement they conditionalize.print "true\n" if $a;
This is described in perlsyn under the heading Statement Modifiers.
![Page 31: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/31.jpg)
A Linguistic Contortion
A statement modifier can modify multiple statements, but they must be wrapped in a do block.do { print "true\n"; rejoice($a);} if $a;
![Page 32: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/32.jpg)
More Linguistic Awesomeness
Perl includes an unless keyword that can be used in the place of if, but the conditional is reversed.do { print "oh no!\n"; emergency($a);} unless $a;
![Page 33: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/33.jpg)
while loop
A simple while loop:while ($a > 0) { print $a." bottles of beer.\n"; $a--;}
![Page 34: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/34.jpg)
until loop
until is like while with the conditional reverse (just like if and unless).until ($a <= 0) { print $a." bottles of beer.\n"; $a--;}
![Page 35: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/35.jpg)
while at the end
The loop keywords while and until can be used as statement modifiers, too.$a-- until ($a <= 0);
![Page 36: CS 105 Perl: Data Types Nathan Clement 15 May 2014](https://reader035.vdocument.in/reader035/viewer/2022070404/56649f345503460f94c50bc9/html5/thumbnails/36.jpg)
Statement modifiers vs. Expectations
Perl has a special case for do blocks modified by while and until. Normally the conditional in the statement modifier is evaluated first. Not in this case:do { $a--; print "Mmmm, beer.\n";} until ($a <= 0);Perl will behave according to your expectations here, but note that Perl is being dwimmy.