PERL - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

PERL

Description:

PERL – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 23
Provided by: mm197
Category:
Tags: perl | chomp

less

Transcript and Presenter's Notes

Title: PERL


1
PERL
  • Practical Extraction and Report Language

2
Introduction
  • PERL is designed to assist the programmer with
    common tasks that are probably too heavy or too
    portability-sensitive for the shell, and yet too
    weird or short-lived or complicated to code in C
    or some other UNIX glue language.
  • Common on UNIX systems.
  • Good for administrators
  • Create your own tools
  • C-like language, but no main!
  • Examples come from OReilly text Learning Perl

3
Compiled vs Interpreted Languages
  • Compiled reduced from source code to native
    (binary) code.
  • Interpreted computer executes the program line
    by line.
  • No compile errors (obviously)
  • Runtime errors
  • PERL completely reads and parses the program
    before execution.

4
Hello World
  • !/usr/bin/perl w
  • About to print Hello!
  • print ("Hello, world!\n")
  • Perl must be the first line in your program (the
    w is to produce extra warning info)

5
Declaring Simple Variables
  • Not necessary to declare the type!
  • print "What is your name? "
  • read from the command line
  • name ltSTDINgt
  • remove any whitespace
  • chomp (name)

6
Conditionals
  • !/usr/bin/perl
  • Get user name
  • print "What is your name? "
  • name ltSTDINgt
  • chomp (name)
  • Note eq compares two strings (dont use )
  • if (name eq "Randal")
  • print "Hello, Randal! How good of you to be
    here!\n"
  • else
  • print "Hello, name!\n" ordinary greeting

7
Guessing the Secret Word
  • !/usr/bin/perl -w
  • secretword "llama"
  • the secret word
  • print "What is your name? "
  • name ltSTDINgt
  • chomp name
  • if (name eq "Randal")
  • print "Hello, Randal! How good of you to be
    here!\n"
  • else
  • print "Hello, name!\n" ordinary greeting
  • print "What is the secret word? "
  • guess ltSTDINgt
  • chomp (guess)
  • while (guess ne secretword)
  • print "Wrong, try again. What is the secret
    word? "
  • guess ltSTDINgt
  • chomp (guess)

8
Arrays/Lists
  • Creating an array
  • _at_words ("camel","llama","alpaca")
  • _at_words qw(camel llama alpaca)
  • Accessing an array
  • if (words0 eq Chihuahua)
  • Note the , not _at_

9
Hashes
  • Hash holds a key and value
  • Creating a hash
  • words qw(
  • fred camel
  • barney llama
  • betty alpaca
  • wilma alpaca
  • )

10
  • !/usr/bin/perl
  • words qw(
  • fred camel
  • barney llama
  • betty alpaca
  • wilma alpaca
  • )
  • print "What is your name? "
  • name ltSTDINgt
  • chomp (name)
  • if (name eq "Randal")
  • print "Hello, Randal! How good of you to be
    here!\n"
  • else
  • print "Hello, name!\n" ordinary
    greeting
  • secretword wordsname get the secret
    word
  • print "What is the secret word? "
  • guess ltSTDINgt
  • chomp (guess)
  • while (guess ne secretword)

11
Regular Expressions
  • The strength of PERL
  • Regular expressions are denoted using / /
  • means all

12
The Match Expression
  • is the match operator (begin with)
  • Example
  • if (name /Randal/) ...
  • Says match if begins with Randal
  • Unfortunately,doesnt match randal, and matches
    Randall
  • Better example
  • if (name /Randal\b/i) ...
  • \b says it cant be followed by a number or
    character
  • i means ignore case

13
Another Match Example
  • print "any last request? "
  • does the input begin with a y?
  • note could be (ltSTDINgt /y/i)
  • if (ltSTDINgt /yY/)
  • print Just what might that request be?"
  • discard a line of standard input
  • ltSTDINgt
  • print "Sorry, I'm unable to do that.\n"

14
Substitution
  • Substitution finds a regular expression and
    replaces it with a string
  • s/ltwhat youre looking forgt/ltreplacementgt/
  • Example
  • s/abc/def/
  • Replaces all occurrences of a, any number of bs,
    ending in c with def
  • Another Example
  • which "this is a test"
  • which s/test/quiz/ which is now
    "this is a quiz"
  • Advanced
  • \W is a non-word character and is all
  • name s/\W.//
  • Replaces everything after the first word with
    nothing

15
Translation
  • Translation converts characters
  • name tr/A-Z/a-z/
  • This converts upper to lower case
  • The A-Z denotes all the characters in between

16
Example
  • Find if the sentence contains bird
  • what "bird"
  • sentence "Every good bird does fly."
  • if (sentence /\bwhat\b/)
  • print "The sentence contains the word
    what!\n"

17
Splitting and Joining
  • Splitting breaks strings into fields, and puts
    the into an array
  • Joining glues fields back together
  • Split example
  • line "merlyn11810Randal/home/merlyn/usr/b
    in/perl"
  • split line, using as delimiter
  • _at_fields split(//,line)
  • now _at_fields is ("merlyn","","118","10","Randal",
  • "/home/merlyn","/usr/bin/perl")
  • Join example
  • bigstring join(,_at_fields)
  • glue is the delimiter and list is the array

18
Functions
  • Use the sub keyword
  • Example
  • sub myFunction
  • code
  • Call the function by its name
  • Can create local variables using my keyword
  • my (local_var)

19
Parameters
  • Parameters are passed through something called _at__
  • Example
  • sub addTwoNums
  • my (num1, num2) _at__
  • return (num1num2)
  • addTwoNums(1, 7)

20
Files
  • Create a filehandle
  • Note that you get 3 of them for free ltSTDINgt
  • Example
  • sub init_words
  • open (WORDSLIST, "wordslist.txt")
  • reads an entire line
  • while (name ltWORDSLISTgt)
  • chomp (name)
  • word ltWORDSLISTgt
  • chomp (word)
  • wordsname word
  • close (WORDSLIST)

wordslist file fred camel barney llama betty alpac
a wilma alpaca
21
Writing Modes
  • A file can be opened for
  • Reading open INFILE, lt, filename
  • Writing open OUTFILE, gt, filename
  • Appending open OUTFILE, gtgt, filename

22
Conclusions
  • PERL is great for
  • Learn the basics of programming
  • Parsing files
  • Writing simple tools
  • UNIX environments
  • System administrators
  • PERL isnt popular because
  • Lack of graphics (though there is Tcl/Tk)
  • LOTS of options for parsing
  • Better for UNIX than Windows?
  • Looks cryptic at times
Write a Comment
User Comments (0)
About PowerShow.com