Introduction to Perl - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Introduction to Perl

Description:

Introduction to Perl – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 46
Provided by: paull3
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Perl


1
Introduction to Perl
2
What is Perl
  • Perl is an interpreted language. This means you
    run it through an interpreter, not a compiler.
  • Similar to shell script but lot easier and more
    powerful.
  • Perl is free to download and is also available
    for Windows and Macintosh.
  • File name extension .pl
  • The first line of a perl program should tell
    where to find the perl intepreter
  • !/usr/bin/perl

3
Steps to Run a Perl Program
  • Use your favorite editor to create a Perl
    program, say test.pl.
  • Change the file permission and make it
    executable.
  • chmod 700 test.pl
  • Run Perl program
  • test.pl

4
Comments
  • The pound sign "" is the symbol for comment
    entry.
  • Exception First line, !/usr/bin/perl, tells
    where to find the Perl compiler on your system

5
Variables
  • Three types of variables.
  • Scalar
  • Array
  • Hash

6
Scalars
  • Scalar means single value
  • In C/C, many different kinds of single values
  • int, float, double, char, bool
  • In Perl, Scalar variable can hold all these
    types, and more, such as string.
  • In Perl, we do not need to declare the data type
    of variables

7
Scalars
  • All Scalar variables begin with a
  • Examples foo, a, zebra1, F87

8
Scalar Assignments
  • Scalars hold any data type
  • foo 3
  • d 4.43
  • temp Z could be double quote
  • My_String Hello, Im Paul. could be single
    quote
  • value TRUE

9
Arithmetic in Perl
a 1 2 Addition b 3 - 4
Subtraction c a b
Multiplication d a / b Division a
9 10 Exponentiation b 5 2
Modulo
10
Single and double quotes
  • a 'apples' you can also use double quote
  • b 'bananas' you can also use double quote
  • print 'a and b'
  • display a and b
  • print "a and b"
  • display apples and bananas
  • Single quotation marks do not interpret, and
    double quotation marks do

11
Arrays
  • Concept is the same as in C/C
  • Groups of other values
  • much more dynamic than C/C
  • no declaration of size, type
  • can hold any kinds of value, and multiple kinds
    of values
  • All array variables start with the _at_ character
  • _at_array, _at_foo, _at_My_Array, _at_temp34
  • Array index stars from 0

12
Array Operations
  • _at_a1 (3, 2, 1, 4)
  • _at_a2 (34, z, Hi, 43.2)
  • Assignment
  • a22 X _at_a2 (34, z, X, 43.2)
  • Copy array
  • _at_a3 _at_a1 _at_a3 (3, 2, 1, 4)
  • Merge array
  • _at_a5 (_at_a1, _at_a2) _at_a5 (3, 2, 1, 4, 34, z,
    Hi, 43.2)
  • How about this operation
  • _at_a1 (_at_a1, _at_a2) _at_a1 (3, 2, 1, 4, 34, z,
    Hi, 43.2)

13
Sort Array
  • _at_a1 (3, 2, 1, 4)
  • Sort array
  • _at_a4 sort (_at_a1) _at_a4 (1, 2, 3, 4)
  • How about this one?
  • _at_a1 (3, 22, 11, 4)
  • _at_a4 sort (_at_a1) _at_a4 (11, 22, 3, 4)
  • This is same as
  • _at_a4 sort a cmp b (_at_a1)
  • Array is sorted alphabetically (elements are
    considered as string)

14
Sort Array (continued)
  • Sorted alphabetically
  • _at_a1 (3, 22, 11, 4)
  • _at_a4 sort (_at_a1)
  • or _at_a4 sort a cmp b (_at_a1)
  • _at_a4 (11, 22, 3, 4)
  • Sort numerically
  • _at_a1 (3, 22, 111, 4)
  • _at_a4 sort a ltgt b (_at_a1)
  • _at_a4 (3, 4, 11, 22)
  • Note
  • Inside sort block, two variables must be a and
    b
  • If a and b is exchanged, the sorting order is
    changed

15
More about arrays
  • special variable for each array
  • _at_foo (3, 25, 43, 31)
  • foo (a variable last index of _at_foo, which is
    3).
  • foo1 (size of array _at_foo, which is 4).

16
Program Flow if statement
  • if (x y)
  • ...
  • elsif (x (y1))
  • ...
  • else
  • ...

17
Program Flow Comparing variables
Numbers ! lt gt
Strings eq ne lt gt
18
Program Flow Logical operators
  • (logical and)
  • (or)
  • ! (negation)

19
Program Flow (Loops)
  • for (t 0 t lt 100 t)
  • ...
  • while (x y)
  • ...

20
foreach Statement
  • This statement takes an array variable and
    assigns one item at a time to a scalar variable,
    executing a block of code.
  • For example, _at_list an array
  • foreach var (_at_list)

21
Basic IO
  • Output to terminal
  • The print statement.
  • Example
  • print My name is name\n
  • Input from keyboard
  • The ltgt operator
  • Example
  • input ltgt
  • read one line from keyboard, and save in variable
    input

22
Task 1
  • Write a Perl program to ask the user to enter a
    name, then it will display
  • Hello name_user_rntered

23
Perl Program for Task 1
  • !/usr/bin/perl
  • print "Enter your name"
  • name ltgt
  • print "Hello name\n"

24
Chomp
  • When reading in, carriage return (\n) is
    included.
  • Usually dont want that.
  • chomp will take off the last character of a
    string, if it is a \n.
  • chomp (foo)

25
Perl Program for Task 1 (revised)
  • !/usr/bin/perl
  • print "Enter your name"
  • name ltgt
  • chomp(name)
  • print "Hello name\n"

26
Read / Write to Files
  • To read and write to files we should create
    something called handles which refer to the
    files.

27
Read from Files
  • To create a file handle for reading
  • Use the OPEN command
  • Example
  • open(filehandle1,"filename1")

28
Read from Files (continued)
  • Once the file handles have been obtained, we can
    read data (line by line) from the file.
  • Example
  • _at_lines ltfilehandle1gt
  • This will result in each line being read from the
    file pointed by the file handle and all lines are
    stored in the array variable _at_lines, where index
    0 (lines0) contains first line of the file,
    index 1 (lines1) contains second line of the
    file, and so on.

29
Read from Files (continued)
  • After read file, we should close the file.
  • close(filehandle1)

30
Task 2
  • Write a Perl program that can read a file
    (test.cpp) and display each line with a line
    number

31
Perl program for Task 2
  • !/usr/bin/perl
  • open(fh1, "test.cpp")
  • _at_input_linesltfh1gt
  • chomp(_at_input_lines)
  • close(fh1)
  • i1
  • foreach line(_at_input_lines)
  • print "i line\n"
  • ii1

32
Write to Files
  • To create a file handle for writing
  • Use the OPEN command
  • Example
  • open(filehandle2, "gtfilename2")

33
Write to Files (continued)
  • Once the file handles have been obtained, we can
    write data to the file.
  • Example
  • print filehandle2 "linevalue"
  • This will result in the value of linevalue being
    written to the file pointed by the filehandle2.

34
Write to Files (continued)
  • After write to file, we should close the file.
  • close(filehandle2)

35
Task 3
  • Rewrite the Perl program for Task 2 so that the
    result will be write to a file (test_c.cpp)
    instead of displaying on the screen.

36
Perl program for Task 3
  • !/usr/bin/perl
  • open(fh1, "test.cpp")
  • _at_input_linesltfh1gt
  • chomp(_at_input_lines)
  • close(fh1)
  • i1
  • open(fh2, "gttest_c.cpp")
  • foreach line(_at_input_lines)
  • print fh2 "i line\n"
  • ii1
  • close(fh2)

37
Subroutines (functions)
  • To define your own subroutine, use the keyword
    sub
  • Can be defined anywhere in your program
  • sub function_name
  • commands

38
Function Calls
  • Name getname() return a value
  • getname() not returning a value

39
Parameters of Functions
  • Parameters are passed a function as an array.
  • The parameter is taken in as an array which is
    denoted by _at__ inside the function.
  • So if you pass only one parameter the size of
    array _at__ will be one. If you pass two parameters
    then the _at__ size will be two and the two
    parameters can be accessed by _0,_1 , and
    so on.

40
Subroutines
  • !/usr/bin/perl
  • result max(11, 12)
  • Print The largest number is result \n
  • sub max
  • if(_0 gt _1)
  • return _0
  • else
  • return _1

Output The largest number is 12
41
More About Functions
  • The variables declared in the main program are by
    default global so they will continue to have
    their values in the function also.
  • Local variables are declared by putting my key
    word while declaring the variable.

42
Subroutines local variable example
  • !/usr/bin/perl
  • sub max
  • my _at_num _at__
  • if(num0 gt num1)
  • return num0
  • else
  • return num1
  • result max(11, 12)
  • Print The largest number is result \n

43
A routine (user defined) to read web pages
  • sub getweb
  • my url _0
  • require LWPUserAgent
  • my ua LWPUserAgent-gtnew
  • ua-gttimeout(10)
  • ua-gtenv_proxy
  • my response ua-gtget(url)
  • return response-gtcontent

This routine takes one parameter (a web address)
and returns the contents of a web page as one
string
44
Task 4
  • Display the html code of www.google.com

45
Perl program for task 4
  • !/usr/bin/perl
  • google getweb("http//www.google.com")
  • print google the entire page is saved as one
    string
  • sub getweb
  • my url _0
  • require LWPUserAgent
  • my ua LWPUserAgent-gtnew
  • ua-gttimeout(10)
  • ua-gtenv_proxy
  • my response ua-gtget(url)
  • return response-gtcontent
Write a Comment
User Comments (0)
About PowerShow.com