Chapter 11 The Perl Scripting Language Dr. Marjan Trutschl - - PowerPoint PPT Presentation

chapter 11 the perl scripting language
SMART_READER_LITE
LIVE PREVIEW

Chapter 11 The Perl Scripting Language Dr. Marjan Trutschl - - PowerPoint PPT Presentation

Chapter 11 The Perl Scripting Language Dr. Marjan Trutschl marjan.trutschl@lsus.edu Louisiana State University, Shreveport, LA 71115 Chapter 11 The Perl Scripting Language Intro to Perl Working with Files Help Sort Terminology


slide-1
SLIDE 1

Chapter 11 The Perl Scripting Language

  • Dr. Marjan Trutschl

marjan.trutschl@lsus.edu Louisiana State University, Shreveport, LA 71115

slide-2
SLIDE 2

Chapter 11 The Perl Scripting Language

¤ Intro to Perl ¤ Help ¤ Terminology ¤ Running a Perl Program ¤ Syntax ¤ Variables ¤ Control Structures ¤ Working with Files ¤ Sort ¤ Subroutines ¤ Regular Expressions ¤ CPAN Modules ¤ Examples

slide-3
SLIDE 3

Intro to Perl

¤ The Perl motto is "There's more than one way to do it." Divining how many more is left as an exercise to the reader.

¤ Originally created by Larry Wall as a tool to process reports at NASA, AKA the Practical Extraction and Report Language ¤ Perl is easy, flexible and processes text information quickly ¤ Since most graphics are text at some level, Perl has been adapted to CGI pretty widely ¤ It’s also great at RegExp and works very well with Linux by allowing embedded bash in a Perl script or Perl in a bash script

  • Yep. This guy.
slide-4
SLIDE 4

Help

¤ Help

¤ There are numerous resources for Perl, so please don’t stop with just the text ¤ Try reading the documentation…it’s actually very well written and provides sound advice ¤ Also, if you’re in a hurry, try this tutorial: ¤ http://www.perl.com/pub/2008/04/23/a-beginners- introduction-to-perl-510.html

Perl is already installed on the Sun servers or any OSX system, but some Linux distributions (like raspbian) don’t ship with it. Simply install it with ‘apt-get’ or your favorite package manager.

slide-5
SLIDE 5

Help

¤ Documentation

¤ Because Perl’s restrictions can be turned off, it’s easy to use it for simple tasks with sloppy code ¤ Turn on ‘strict’ and ‘warnings’ to tighten up your code ¤ Perl supports object oriented code, though there is no special syntax for it, feel free to write good code ¤ Constructors can be created using the ‘bless’ function ¤ Use the ‘perldoc’ utility to document

Larry is very religious, and has a good sense of humor about it.

slide-6
SLIDE 6

Terminology

¤ Terminology

¤ Module: Similar to an API, it’s a piece of code containing several functions that work together ¤ Distribution: A set of modules that perform a task ¤ Package: The Perl term for a namespace, it defines a variable in relation to a distribution or module to prevent collisions ¤ Block: Statements within curly braces that defines a scope ¤ Package variable: A variable with a scope limited to the current package ¤ Lexical variables: Locally defined variables by the ‘my’ keyword, since the ‘local’ keyword was already taken

slide-7
SLIDE 7

Terminology

¤ Terminology (cont)

¤ List: A set of comma separated values in parentheses ¤ Array: An ordered list of scalar variables, indexed by number ¤ Hash: Unordered collection of scalar values indexed by their associated string key ¤ Scalar: A value, in the form of a number, string or reference ¤ Compound Statement: A collection of statements, within

  • ne or more blocks
slide-8
SLIDE 8

Running a Perl Program

¤ Running Perl

¤ Perl can be run from the command line or a script ¤ Use a ‘.pl’ file extension when writing scripts and a shebang using the ‘which’ utility ¤ Terminate lines in a script with a semicolon ¤ Just like bash, use ‘chmod’ to give execute permission to the script and run it with ‘./’ from within the same directory

The ‘use strict’ and ‘use warnings’ enforce better coding practices, but are optional

slide-9
SLIDE 9

Syntax

¤ Perl Syntax

¤ Perl’s syntax is similar to C, which is similar to bash ¤ Quotation Mark: These act like bash, where double quotes allow expansion of variables and single quotes do not. ¤ Statement: Each statement is terminated by a semicolon, unless it’s the final statement in a block ¤ Slash: The lead guitarist for Guns and Roses, or a delimiter for Regular Expressions ¤ Backslash: Escape character ¤ Hashmark: #comments #sorrynotsorry

slide-10
SLIDE 10

Variables

¤ Three Types of Variables

¤ $scalar: Scalar variables contain a single value ¤ @array: Arrays contain multiple values, indexed by number and starting at 0 ¤ %hash: Hashes contain references to data, or an associative array

¤ Syntax

¤ Variable names are case sensitive and can include letters, digits and the underscore ¤ Package Qualifiers are designated by ‘::’ or a single quote

slide-11
SLIDE 11

Variables

¤ Usage of Variables

¤ If a lexical (local) variable has the same name as a package variable, Perl uses the lexical variable within the block ¤ Use ‘my’ to keep all variables local, particularly in larger software packages, to prevent confusion ¤ Use ‘our’ to use a package variable of the same name ¤ Variables do not need to be declared before usage, which can lead to bugs ¤ Turn on ‘use strict’ to prevent this to prevent typos from turning into bugs ¤ Turn on ‘use warnings’ to get a notification that a variable is used without initialization

slide-12
SLIDE 12

Variables

¤ Scalar Variables

¤ Scalar, or singular, variables are prefixed by ‘$’ and hold a single value ¤ Using ‘my’ will identify them as Lexical (or local) variables

¤ Scalars and RegExp

¤ When Perl finds a RegExp match, then it will store it in a variable $1 through $9 ¤ Since these are lexical variables and easily clobbered, keep them from disappearing outside of the block by assigning them to another variable

slide-13
SLIDE 13

Variables

¤ Array Variables

¤ Arrays used zero based indexing, so they start at 0 ¤ There is no set limit to the size of an array, but it will return ‘undef’ when an array index is accessed out of bounds ¤ Double quote strings in arrays ¤ There are many different ways to deal with arrays, but a lot fewer when ‘strict’ is enabled ¤ Access sequential array items using an array slice ¤ ‘..’ Prints continuous entries, while ‘,’ prints specific entries ¤ Use double quotes to print spaces between entries

slide-14
SLIDE 14

Variables

¤ Array Sample

¤ Here are some simple array samples to try out for practice Why would you ever need an array that’s not a linked list? Perl dispenses with the unnecessary.

slide-15
SLIDE 15

Variables

¤ Hash Variables

¤ A hash, unlike an array, is unordered and values are accessed by a string value called a key ¤ Hashing will speed up finding data in large data sets, and Perl runs like many others at O(1) with a worst case O(n) ¤ This makes the normal use case to use a hash to link to a scalar value, improving the search performance Hashes in Perl are a little different. Since they only map to one value (which can be an array), they are ideally suited as dictionaries. Once the search key is placed in a hash table, the search is basically free. The value in the dictionary can easily be stored in a format for an address book or other kind of table

slide-16
SLIDE 16

Control Structures

¤ Perl Control Structures

¤ Perl operates like most other C based languages with some minor differences ¤ if/unless: Simplifies some nested if/else statements when a nullifier is required ¤ foreach: There is an alternative ‘for’ syntax that might look familiar, but they are synonymous ¤ last and next: Like skips or jumps, ‘next’ will return directly to the conditional while ‘last’ exits from the loop ¤ while and until: These are identical, except that ‘while’ continues to loop when the conditional evaluates to false and ‘until’ continues until it evaluates to true

slide-17
SLIDE 17

Working with Files

¤ File Handles

¤ Refers to a file or process that is open for reading or writing ¤ Similar to bash, Perl opens handles for input, output and error ¤ STDIN, STDOUT and STDERR ¤ Other handles must be manually opened ¤ This sounds hard, but really, they’re not very heavy ¤ Give the file handle any name you like ¤ Once opened, simply write to it using the ‘print’ function ¤ Or assign the handle to STDOUT so all output defaults to it ¤ “Magic” file handles are set through command line arguments

slide-18
SLIDE 18

Working with Files

¤ File Handles

¤ A brief note about file handles: The convention is to use all caps when writing ‘bareword’ file handles, because they are global variables. Because of this, they can cause collisions and can be more easily used by hackers. Since they are input directly, they also don’t give the program a chance to filter the input through string validation. Get into the habit of using three argument opens. Many reasonable people disagree on this topic, but the safest route is three argument file handles by reference. ¤ Optionally, this is also where you would specify character encoding, as part of the second argument

slide-19
SLIDE 19

Working with Files

slide-20
SLIDE 20

Working with Files

¤ Further Notes

¤ ‘chomp’ and ‘chop’ remove extra newlines and spaces, respectively ¤ ‘$!’ holds the last system error, if you’d like to print a full error message with some verbosity ¤ ‘$ARGV’ holds the argument from the command line, useful if your program reads in a filename

slide-21
SLIDE 21

Sort

¤ Sort

¤ The Perl ‘sort’ is a little more refined than bash ¤ It sorts numerically or alphabetically, based on ‘locale’ setting ¤ ‘reverse’ doesn’t sort, it simply swaps array values This sort might not work as

  • expected. It sorts based on

character sequence in the ASCII table. There are several ways to deal with this.

slide-22
SLIDE 22

Sort

¤ Sort

¤ If you’d like to sort numbers as integers, you’ll need to get it to compare things differently This sort works as expected with integers, but uses a couple of

  • dd things.

The ‘$a’ and ‘$b’ variables should

  • nly be used for sorting (although

they work outside of it) because they are specialized to the task. Don’t lexicalize them with ‘my’.

The ‘<=>’ “spaceship operator” is a three way comparison

  • perator, which returns 1, 0 or -1.

So you quickly get less than, greater than and equal to results from one comparison

slide-23
SLIDE 23

Subroutines

¤ Locality and Subroutines

¤ In Perl, all variables default to package level ¤ Local variables are declared with the ‘my’ keyword and package variables are declared with ‘our’ ¤ When creating subroutines, sometimes we want variables to be accessible outside of their block, so package is good ¤ The @_ array variable is local to the subroutine, but aliases the values in the calling argument, allowing it to pass parameters ¤ So you can pass in as many parameters as you’d like. ¤ Of course, you might want to check it explicitly, because it might not actually work.

slide-24
SLIDE 24

Subroutines

¤ Example

¤ Turn off ‘strict’ and use whatever variables you like ¤ Just be careful that your code actually works properly

slide-25
SLIDE 25

Regular Expressions

¤ Regular Expressions and Perl

¤ Perl handles regular expression very well and it’s a very common usage of Perl to use single lines in bash for RegExp ¤ Also see Appendix A in the text for further reading, this module assumes knowledge of RegExp

¤ Syntax and the ‘=~’ operator

¤ On the command line, the options ‘-l’, -n and ‘-e’ are common, applying a ‘chomp’ to the line, reading line by line and allowing Perl to run on the command line, respectively ¤ The ‘-e’ must be the last option

slide-26
SLIDE 26

Regular Expressions

¤ Example

¤ Of course, RegExp can be used inside of Perl scripts or passed on the command line, but here is some pretty simple syntax to use the command line version

  • The ‘-n’
  • ption

reads in line by line for the input file

  • The ‘e’ is the

flag that tells it to run on the command line

  • Take your

pick of several syntaxes for this kind of statement

  • Pass the

filename as a command line argument

slide-27
SLIDE 27

CPAN Modules

¤ CPAN Modules – Comprehensive Perl Archive Network

¤ CPAN Modules are like an API library, but since Perl doesn’t really have a central development team, it’s more than that ¤ Check out the www.CPAN.org website and see if there are any modules you’d like to install, and then call those modules like an API ¤ The normal installation of Perl comes with numerous CPAN modules ¤ The above website is a great place to get started

slide-28
SLIDE 28

Examples

¤ Examples

¤ There are several useful examples in the text, find them at: ¤ http://www.sobell.com/CR3/code/

slide-29
SLIDE 29

Summary

¤ Intro to Perl ¤ Help ¤ Running a Perl Program ¤ Syntax ¤ Variables ¤ Control Structures ¤ Working with Files ¤ Sort ¤ Subroutines ¤ Regular Expressions ¤ CPAN Modules ¤ Examples

slide-30
SLIDE 30

Work Cited

¤ Cozens, Simon. "Hash Crash Course." - Perl.com. Tom Christiansen Perl Training, 2 Nov.

  • 2006. Web. 22 Feb. 2016. <http://www.perl.com/pub/2006/11/02/all-about-hashes.html>.

¤ Sobell, Mark G. “The Perl Scripting Language” A Practical Guide to Linux Commands, Editors, and Shell Programming. Upper Saddle River, NJ: Prentice Hall/Pearson, 2013. N.

  • pag. Print.

¤

  • Wikipedia. Wikimedia Foundation, n.d. Web. 21 Dec. 2015. <https://en.wikipedia.org/

wiki/X_Window_System>. ¤ "Perldata." Perldata. Perl 5 Porters, n.d. Web. 08 Feb. 2016. <http://perldoc.perl.org/ perldata.html>. ¤ "Perl Programming Documentation." Perl Programming Documentation. Perl 5 Porters, n.d. Web. 04 Feb. 2016. <http://perldoc.perl.org/perl.html>. ¤ Wall, Larry. "Larry Wall's Very Own Home Page." Larry Wall's Home Page. Larry Wall, n.d.

  • Web. 04 Feb. 2016. <http://www.wall.org/~larry/>.