CSC 4630 - PowerPoint PPT Presentation

About This Presentation
Title:

CSC 4630

Description:

... (camel llama alpaca); #set multiple secret words print What is your name? ; chomp($name = ); if ($name eq Randal ) { print Hello, $name! – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 13
Provided by: support
Category:
Tags: csc | alpaca

less

Transcript and Presenter's Notes

Title: CSC 4630


1
CSC 4630
  • Perl 1

2
Perl
  • Practical Extraction and Support Language
  • A glue language under UNIX
  • Written by Larry Wall
  • Claimed to be the most portable of scripting
    languages
  • See www.perl.com

3
Perl by Example
  • Perl is more powerful than awk, but looks
    somewhat the same.
  • Perl is more flexible than shell scripts, but
    uses some of the same constructs.
  • Perl is based on C, and in fact the interpreter
    is written in C, but is easier to use than C.

4
Perl by Example (2)
  • Beginning assumptions and notations
  • Statements are terminated by (semicolon)
  • Comments start anywhere on a line with and end
    at the end of the line. Thus, you can write
    explanations on the line for the line. But they
    cant be two line explanations.
  • Built-in functions neither require nor forbid
    ( ) around their arguments

5
Perl by Example (3)
  • First line of Perl program should be a directive
    to the interpreter e.g. (either)
  • ! /opt/local/bin/perl w
  • ! /usr/bin/perl w
  • Invoke by perl ltfilenamegt or ltfilenamegt
  • Scalar variables have names that start with and
    have values that are either strings or double
    precision real numbers.

6
Exercise 1
  • ! /opt/local/bin/perl w
  • print (Hello, world!\n)

7
Exercise 2
  • ! /opt/local/bin/perl w
  • print What is your name?
  • name ltSTDINgt
  • chomp (name)
  • print Hello, name!\n

8
Exercise 3
  • !/opt/local/bin/perl w
  • print What is your name?
  • name ltSTDINgt
  • chomp (name)
  • if (name eq Randal)
  • print Hello, name! Glad youre here\n
  • else
  • print Hello, name!\n ordinary greeting

9
Exercise 4
  • ! /opt/local/bin/perl w
  • secretword llama the secret word
  • print What is your name?
  • chomp(name ltSTDINgt)
  • if (name eq Randal)
  • print Hello, name! Glad youre here\n
  • else
  • print Hello, name!\n
  • print What is the secret word?
  • chomp(guess ltSTDINgt)
  • while (guess ne secretword)
  • print Wrong, try again. Secret word?
  • chomp(guess ltSTDINgt)

10
Non-scalar Variables
  • For example, arrays
  • Identifier starts with an _at_
  • Can be initialized with a list of strings
  • _at_words (camel, llama, alpaca)
  • Or with the qw operator (which saves keystrokes)
  • _at_words qw (camel llama alpaca)
  • Elements accessed as scalars, so
  • words0 is camel, words1 is llama
  • NB Index origin is 0
  • i 2 wordsi has value alpaca

11
Exercise 5
  • !/opt/local/bin/perl w
  • _at_swords qw (camel llama alpaca) set multiple
    secret words
  • print What is your name?
  • chomp(name ltSTDINgt)
  • if (name eq Randal)
  • print Hello, name! Glad youre here\n
  • else
  • print Hello, name!\n
  • print What is the secret word?
  • chomp(guess ltSTDINgt)
  • i 0 index in swords array
  • correct maybe status of search
  • while (correct eq maybe)
  • if(guess eq swordsi) correct yes
    set flag on success
  • elsif (i lt 2) i not success, try
    next secret word
  • else
  • print Wrong, try again. Secret word?
    no word matches, ask again
  • chomp(guess ltSTDINgt)
  • i0 reset index in secret word array

12
Scalar operators
  • Numeric - /
  • Numeric comparison lt lt gt gt !
  • String . (for concat) x (for multiple
    concat)
  • String comparison lt le eq ge gt ne
  • Identifier ltlettergt ltletter digit under gt
  • Assignment
  • (carries value of expression)
  • binary assignment . etc.
  • increment prefix, postfix i i
  • increment then assign vs. assign then increment
Write a Comment
User Comments (0)
About PowerShow.com