22
Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180, Fall 2004 1 What is Perl “Practical Extraction and Report Language”. Created, implemented, maintained by Larry Wall. Interpreted language that is optimized for string manipu- lation, I/O, and system tasks. Syntax elements from the C, Bourne shell, csh, awk, sed, grep. (Unlike other interpreted languages, Perl compiles and optimizes the code before running.) 2

Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Perl Tutorial

Diana Inkpen

School of Information Technology and Engineering

University of Ottawa

CSI 5180, Fall 2004

1

What is Perl

� “Practical Extraction and Report Language”.

� Created, implemented, maintained by Larry Wall.

� Interpreted language that is optimized for string manipu-lation, I/O, and system tasks.

� Syntax elements from the C, Bourne shell, csh, awk, sed,grep.

� (Unlike other interpreted languages, Perl compiles andoptimizes the code before running.)

2

Page 2: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Some of Perl’s many strengths

� Speed of development – edit a text file and just run it. Noseparate compiler needed. Short pieces of code that do alot (magic operators).

� Power – Perl’s regular expressions, easy file handling anddata manipulation.

� Flexibility – freedom of expression.� Portability – Perl compilers for all platforms.� Extensibility – modules available on CPAN.� Price – free. Extra: it is fun !!

3

The “Hello, world” program

��������������� �������������� ����������� ������ �!�#"$�%���%�'&(��)*�+�-,

How to run the program written in the file “myfile”:� chmod u+x myfile

� myfile

or� perl -w myfile

4

Page 3: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Basic syntax

� Perl is free form – whitespace doesn’t matter (but betteruse indentation for readability).

� All Perl statements end in a ; (semicolon), like C.

� Comments begin with # (pound sign) and last until theend of line (no multi-line comments).

� Variable names start with $ (or � , %).

5

More programs

� �(� ���!� �� � �� � �������!��� �'�� ����� � ������� � ��� ������ ����-� � � ,� ����-� � ���#, ����� � � ����-� ,� ����� � ����� ���!� " � ���� � ��)*�+�-,

� �(� ���!� �� � �� � �������!��� �'����!� �������*� ,�� � ����-� ,� ����� � ������� � ��� ������ ����-� � � ,� ����-� � ���#, ����� � � ����-� ,� ����� � ����� ���!� " � ���� � ��)*�+�-,

6

Page 4: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Variables

� Don’t need to declare variables, but better to declare them(����� ��� ������ , � � ����-�#,

).

� Cannot declare type (clear from context).� Scalars

� Numbers� Strings

� Arrays of scalars

� Hashes (associative arrays of scalars)

7

Strings

� Single-quoted strings.� � ��� ���!��)�� � � � ��� ��� ��� ��� � ��� � ����)�� �

��� � ��� � � ) � �!� � � � � ����� ��� �� � � �������&

� Double-quoted strings.� � ��� ���!��)�� � � ����� ���������� ������ �!��� � � ���!������& �

� ����� � � ���

� Interpolation takes places only in double-quoted strings.

8

Page 5: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Scalar variables

� Hold a single scalar value (number, string, or reference).

� Begin with $, followed by letter, then possibly otherletters, digits, or underscores.

� ���!� �������*� ,�� � ����� � � � ��� � &�'�%� �-, � � ��� � ��� � ��� ������� ��� ������� �* �!�� �*� � � � � ��� � � �� , � � ����� � � �����-� ��� � ��� � � ������� � � � ��� � � � , � � � ������� � ��� ����� ��� � �

9

Numerical operators

� ���!� �������*� ,�� ��� � � � � � � , � ��� ��� ����� � � � � � ��� � � , � � � ��� ��� ��� ��� � ��� � � ��� ��� � , � � � �������*��-� �'� � �*��� � � � � �� � � ��� � � , � � � ��� �� ��� � � ��� �#, � � " ��� -��& �-�!� �

�� � � � � � ��� � � � �#, � ��� ��� �!� " � � � � ��� ����� � � ��" � � � � � � � � � � ��� , � � � � � ��� �#� " ��� � ��" � � ��" �

10

Page 6: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Comparison operators (1)

� � ������ �-� ��� ��� -�!���� � � �������� ��� � ��� � ��� � ��� � � ��� � ��� � ����� ��� ����� � ����� ��� ����� � � � � ����� ��!����� �����*� � �*��!����� �����*� ��� ��� ��� � ��� � � �!���� � �'�%��� � ���*� � ������ � �'�%��� � ���*� �!� ��� ��� � �%� � � � ���� ��� � ��� ��� ������ � ��� ��� � ��� � � ������ ���!�� � � � �

11

Comparison operators (2)

� ���!� �������*� ,�� � � � � , �� � � � ,� � � � , � ����� �����'�%� � ��� �� � � � � , � ����� �����'�%� � ��� � �� � � � � � , � ����� �����'�%� � ��� � ��� � � ����� � �!���-, � � & � ��������� � �-,� & � � , � ����� �����'�%� � ��� �� & � � � , � ����� �����'�%� � ��� �� � ������ �!���-, � ����� �����'�%� � ���

12

Page 7: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

String operators

� Concatenation (”Hi ” . ”there”;)

� ���!� �������*� ,�� � ��� � �'� � ��� �#� � ,�� � �!������� �� � � � � � �� �'� � � , � ��� ����� ��� �#� � �#� ��� � � � � � �!�*����������'� � ������ � � �-, � ���-� � �-� ������ � � �� � �� � � � � � �%� � )��+� , � � ��� � ��� �*� & !�*� !�'� �� � �� � � � � ���� � � � � �%� � )��+�-, � ���� �

13

Variable interpolation

� �� � � � � �������-� ,�� � � � � ,� ����� � � � ��� ��� ������� ��� � � �*� & � ��� ��� ���� ��� � ��)*�+�-,� �������� ��� ����� �*� ������� ��� �����%� �*� & � ��� � � ���� ��� � � ����� � � � ��� ��� ������� ��� � � �*� & � ��� ��� ���� ��� � ��)*� � ,� �������� ��� ����� �*� ������� ��� � � ��� & � ��� ���� ��!� ��� � ��)��

� Interpolation only in double-quoted strings

14

Page 8: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Print: A list operator

� Accepts a list of things to print, separated by commas.� � ����� � ��� & �� -����� ���'���!& �*� ��� ��� �-" � ���!� " � �����'� ��� �

� ������� �* �!� ��� �!��& ����� � " � � � �"�� �*� & � ���'��� � ��� )��+� ,

� � ����� � ��� & �� -����� ���'���!& �*� ��� ��� � �%��� � ���'� �%��� ��%�������* -��� �� � ����& �%��� � � � ��� & � �����-� ����� )*�+�-,

� � �%��� � � ���!���-� , � ��� � � #,� ����� � � ���%� ) � � � � �!�� � ��� � � )*�+�-,� ����� � � ���%� ) � � � � �!�� � " � � � � � " � )��+� ,� ����� � � ) � � � � ��� � � � ���� � � )��+� ,

15

Arrays

� An array is an ordered list of scalar variables.� �� � ����-� �+,

� ����-��� � ����� ����� � � �-" � � ��� � �+� " � � �������������-"�� � ������+� "�� �!� � ��� ,� ����� � � � ��� � �!��-�*� � � � � ) � ����-� � �!�� � ���� � �!)*�+�-,� ����� � � � ��� �%�*� ��� ��!��-��� � ��� � ����-� � � �� )�� �-,� ����� � � � ��� � � ��� & ��!��-��� � ��� � ����-� � � ��� )�� �-,� ����� � � � ���!�� �!�� � " � ���!��� � � ����-��� � " � ��!�� �*� � �!)�� �-,� ����� � � ���� � � , � ������ � )*�+�-,� ����� � � � ����-� �-�-,� ����� � � � ��� �!� �*� � ����-�*�� ��� � ���� � � � � � ����-� ���)�� �-,

16

Page 9: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Functions for arrays

� ���!� �������*� ,�� � � �#" � (" � � , � � � ����" � " � � ,��� � � � � " � , � � � ��� ��� " � " � " � �� � ���*� � � , � � ��� � " � � ��� � ��" � " � �� � ��� � ��� � � , � � ��� ��" � � ��� � � " � �� � � � � ��� � �#" � , � � � ��� � �#" � " � �� � � �*��� ���%� � � ���� �-,

� � � � �-� � � � � " � , � � � ��� � ����� "$� �%� "$� � � � ���� � " � ��� � �-�-" � � ,� ����� � � � !)*�+�-, � � ��� ������� �����%� �*� � � � � �

17

Hashes

� A hash is an associative array.

� A hash is a list of keys that have values.

� �� � , � � �*� � � ����� � � � � � �����*� �-" � �� ���!�*�+� � � � ����& � ��� ,� ��� ������� �%� ��� � ����� ��&���� ,� ��� ���� �� ����� � ����� �*���������-,�� � � ��� ��&!� ����& � " � ��� ��&�� ��� & � ��� � �� ��� � � � ,

� ��� ��&!� ����& � � %� � � � � � ,

� ��� ��&!� ����& � ����� �� ��� � � � �%� ����� � � � � ,

18

Page 10: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Control structures – IF (1)

� � � ����� ����� � � � � � � �

� ��� ��� ��� � � � ,� � � ��� � � � ��� �-���*��� �

� ��� ��� ��� � � � , � ��� �-���*��� � �!& &����-� ����� � � � � � � � �� � � ��� � � ��� �-���*��� �

� ��� ��� ��� � � � ,�

� � � � � � � � � � ��� � � ��� ������ �!��� � ���� ����� � � � �-, �

19

IF (2). Boolean values

� � � � � � ��� � � ��� ��� � � � � �-, �� � ��� � � � � � � � � ��� � � � � ��� ���������� � � �-, �� � ��� �*� ������ � � �-, �

� � ����� � � � � � �-�!����� � � � � � � ,� � �!� � � � � � � � � ��������� � � � � , �

� Boolean values� False is 0, ””, ”0” , or undef.� True is anything else.

20

Page 11: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Control structures – WHILE

� �����!�!� ����� ����� � � � � � � �

� � � ������� � � � ,�

� � �*��� �%� � ��� !�� � � ��� �!� � � ���� � #,�����!�!� ���� �

� � � �������� ,� � � � � � ���� � � � � � ��� ��� , �

� last; and next;

21

Control structures – FOR

� � �!� � � � � � � ����� ��� ��� ,�� � ����� ��� ��� ,�� � � ��� � � ��� � �

� � � ������� � � � ,�

� � �*� � � �� � �!� !�� � �&��� �� �!� � � � � �! , � � � � , � � � � � �

������� ���*� � ��)�� � � ,�

� � � ������-����� � ��� ��� � � � � �� �!� � � � �� , � � � � � �� , � ��� � �

� ������� ���*� � � � � � �)*�+� � , �

22

Page 12: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Control structures – FOREACH

� � �!�� � � � � ��� � � ��� � � � � �

� � � ������� � � � ,�

� �� � ,� �!�� � � � � � � �

� ������� � � � �)��+�-, �

� � � ������ � � ��� ���� �!�� � � � � �� � � � �

� ������� � � � � ��� � ��)��+� , �

23

Basic I/O

� �� � � � ��� ,� � � ��� � �� ����� �� �!& � ��� �� � � � � � � � ��-�!� � � �� ��� � ��� � ��� �%�!�!� � ��� �� �%����& ��� � ��� !��� �*� & �����������!�!� � � ������ � ����� � � ����� � � � ����� , �

�� ��� � � ������� � �� ,� � � ��� ��� � � � ��� ��� � � � ����� ���!�!������-� ��� � ��� )��+� , �

� � � � � � , � �� �!& � � � �����

� � � � , � �� �!& � �� � � � ��� � � � � ��� �%���!�

� � � ��� �'� �%��& ���!��� � � � ��� � ��� � ���� ����� ��� � �� � ��& � ���� �)�� �-" � � " � � " � � ,

24

Page 13: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Defaults

� � �is the name of the default scalar variable

� � �!�� � � � %� � � � �� ������� � � � � � ��� � � ��)��+� , �

� �����!�!� ����� �� � ������ � �#, �

� �����!�!� ����� �� � ������ , �

25

Regular expressions (R.E.) (1)

� A regular expression describes a set of possible strings.� � � ������ � ���-�� � ������� ���� ��� � � ����� � �* -��� � � � ��� ,�����!�!� � � � � ��� � ����� �� � � � � ����� ��� ���* �� � � � ��� � � � ������#, �

� �� � � � ��� ,�����!�!� ������� �� � � � �* �� � � � ������ � �#, �

26

Page 14: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

R.E. (2) – Single-character matching

� � ������ ��� � � �* ��� , � � " " �!� � ������ ��� � � �� ���� , � �*��� &����� �� ������ ��� ��) & � , � �*��� &����� �� ������ ��� � � � � ������ , � �*��� ������ ������ ���'� ��� � ���� ������ ��� � ��� !� , � � " �*� � ���!��*����� � � ��*�� )�� "

� ) & � � &����� � " ��� ��� ��� ���*� � �%� � � ��� �)*� � �%��� & ���!� " ��� � � �%� �!��� � ��� � � � � � ������ ����� �) � � � ���!� ����� " ��� � �!��� �!��� � ��� � ) ��)��%)*��) � � �) � )�� )�� ��� � �'� � �*� � � � ����� � � ���-������

27

R.E. (3) – Grouping patterns

� Sequence (���* !�

)� Alternation (

��� � ��!�,� � � ��� � � � � �

)� Multiplication

� � � � � � �������* � !� � ���������� � �+, � � � " ��� ���� " � � � � � �������* ��%!� � ���������� � �+, � � � " � ��� ���� "

� � �!� -�!��� � �!� -�!��� �!� � � ��� �* !� -�'������ � ��� & �* ���� �!� -�!�� � � ��� � ���!�!������� -�'�- �� � " � � �� ���%���*� � �*��& � � �� ���� ����

28

Page 15: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Pattern-matching operators (4)

� not match operator� ������ � � � �* ����� ,

� � �� �!� � � �������������-� ��� ��� � ������� -�'�- � � ��� � ����'� �*� ������� � � �-���-���-��� � ����� � � � �*� & � -� �� � )�� �� � ����'� �*� ������� � � �������%�!� �������� � � ��� & �

� �������� )�� " �� � � -� �� ����� )�� �� � � �%�*� & �!� ��� �!�� ��� � ��� � � ���!� � �*� & ��� -�*�� �� �*�-� � ��� � �!��� ��� � ������� �*� !�

� interpolation� � � ��� ��� � � �!� ��� � ,

29

R. E. (5) – Anchoring patterns

� � � ���&�)� �� �'�� ��� � � ���& � � ���'� ��� ��&�!������� � � � � � ��� -�'������ � � � � � � ���!���%�!& � ���

� � �'�� ��� � � � �� ����� ������� � � ���������� � � ����� � � � �� �'�� ��� � � � �*��& � � ��� ������� �������� � � � �����!& �)� �'�� ��� � � � �%�!� & �� � � & ��� �)�� �'�� ��� � � � ��*�� �%�!� & �� � � & ��� �)�� �'�� ��� � � � �� ����� ������� � � ����������) � �'�� ��� � � � �*��& � � ��� �������� � � � ����� -� �� ����� � � � � � �%�� �'& -� �� � ��� � �� � � � ����� -� �� ����� � � � � � �%�� �'& � � � -�'�- � ��� � �

30

Page 16: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

R. E. (6) – Returning matches

� � ������ � ��� � ��&� � � � ,� � � " � �� � � ������ � � � � �* �*&� � � � � ���� � ,

� � ������ ��� � � �* ��&� � � � � ���� ,� � � � ��, � � � � , � � � ��� � ��� " � ��� � �

� � ������ ��� ��� ������ ,� ���� � � � , � � ���� ��� � � �� �'���� � � � , � � �'�� � ��� &�� ��� ��� � � � , � � ��� ��� ��� � �

31

Substitutions

� �����'� �%��� �-� �*���'� �%��� �-� �����'� �%��� � �

� �!�*���'����������� � �*�-�!� ��� �*� ����-�!&�� ��� ��� �

� � ������ � �����* �� ���* +� ,� ������ ��� ����� �� � � ��� , � � � � ��� ��� � � � +�� � � � � � �'� � � �� �� � � � ���+�-,�!� � � � �� ��!� � � , � � � ��� � ����!� ������ �� � ������+�-,�!� � )*��� � ��� � ���� � , � � � ��� � �* ����!� � �* ��!��� � �* � �' ���� � � �-,

32

Page 17: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Translations

� � ��� �������%��-� �����%� � �*�-������ �*���������%�� ��&�� ������� �

� � ������ � � � � � �-,� ������ ��� �� ��� � � ������� � , � � � � ��� ��� ��� �

� � � � �* � � �!� � ,� � ��� � � ������� � � � � � , � � � ��� �* ��* �#�� ��� ������ � � , � � � ��� �* ��* ���� � � � � � � ����� � , � � �� ��� �#" � � ��� �� ��* � �� � � � �!��� ����� � , � � �� ��� �

33

Subroutines (1)

� � �� ��� � �'� � � ����� � � ��� ���!��)�� �-, �

� � �� ��&�& �'� �%�� �� � � � � � � ��� , �� � � ��� � �!� ,

��'��� � � � � � � ,�

� � �� ��&�&� �� � � � � � , �� � � � " � � � � , � ��� � ,

� ������ � � � � � ��� � � ��� ��� � � ���'� ��� � � � � �,

34

Page 18: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Subroutines (2)

� � � �� � � , � ������ �!������ � �'� , � ������ �!���� ����� � ��& & � ���%� ��� " � � , � �

� ����� � ��& & � ��" �#" � "�� � , � �

� � �� ��&�&� �� � � � � #,

� ������ � � � � � � � � � ��� � � ���'� ��� � � � � �,

� � �� ��&�& �'� �%� � ��'� ��� � � � � �� � � � � � � , �

35

Filehandles

� STDIN, STDOUT, STDERR

� �� � � � ��� ,�*���*� � � " � ����& & � � � ��� &���� � � ��� ��� � �*����� ��& &�� � � � ,�*���*� ��� " � ��� �%�!��� � � ��� &���� � ��*� ���'� �*����� � ���!�!� � � �-,�����!�!� � � � � ��� � ����� �� � � � ��� � � � ��� � � ������ � � � � �� ,� ��� � � � � � � ��� ,

���� ��� � � � , ��!� �!� ��� � ,

36

Page 19: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Formats (1)

� ����������� ����� ��������������������������������������! � � � � � � " � � � � � � � � " � � � � � � � � � � � � #%$�&�&"&�&�&�&�&�&�&�&�&"&�&�&�&�&�&�&�&�&�&�&�&�& #'()��*�#%$�&�&"&�&�&�&�&�&�&�&�&"&�&�&�&�&�&�&�&�&�&�&�&�& #'�����������+"+#%$�&�&"&�&�&�&�&�&�&�&�&"&�&�&�&!$�& $�&�&�&�&,#'�-�����.0/1'�+��������2/3'�4��5 � � � � � � " � � � � � � � � " � � � � � � � � � � � �

6� 7����(������(�8��:9<;�;�=�>?7�@�A���B�B!+C*�����EDF>�=:9G������H����"���(I9KJ��L9NM�=�>�>�OP ����� P B���+����F��:9NQ�=�Q�=RA������ ����-FS!���(��:9G����������-FSL9KJ"T:9NM�M�M�U�-

37

Formats (2)

� �(5���WVK���"�����"�����0/FXFY"B��(U)��B�+�X�Z[#"#\�����EX^]��(�)���_-����������`B��(U���B�+R':abX*c�(5���WVK�0/dXF�������eX�Zf#�#\�����gX^]��(�����?�(5���_�������!':abX*ch ����B"�iVj&���Y�Zk`-�����5Wc �!B��������������Vj'(���*�2/3'�����������+"+l/3'�-�����.0/3'�+��������m/3'�4��^5eZn !+5)B����!o09Komch �������pVN���������"�����*Zqc �r��H���S��R�����R�����(�����

s-�B���+��2VK���������"�����*Zect-�B���+��2VN�*Zec

� � � � � � � " � � � � � � � � " � � � � � � � � � � � � #u7����F��������8�� ##\;�;�=">v7�@�A���B�Bv+C������_D(>�= ##w������H��������( J(�?M�=�>�>�O,# � � � � � � " � � � � � � � � " � � � � � � � � � � � �

38

Page 20: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Context considerations

����5�����+�+����(r]��(�������� ��H���B(C)������+ ���

�����������������������������������������������������������������������������������������������������������������������������������������'�+�-���B���� +�-���B���� �����RH���B(C��n����B��r�_'�+�-���B����$���������. B���+�� �����vB���+��!����H���B(C���+ V �^_���������)Z ����B��_�?$���������.$���������. +�-���B���� �����R��������Bd�C���U����!���`��B"�F�*�����+`�?$���������.

V +���*�v��+R'�����������.��ED"Z'���������.�N'���r+�-���B���� �����v'�����r��B��F�*����?���`$���������.'�����������. +�-���B���� �����!+C�U�+�-����5��?��� �����vB���+��`��B"�F�*����r�?$���������.XF'�+�-���B����eX +�-���B����0V ��������5 6 Z �`+������^�8_-��F���������8`�����?-��(�������!���?'�+"-���B����X$���������.eX +�-���B����0V ��������5 6 Z �`+������^�8_-��F���������8`�����v��B"��*�����+ ���`$���������.0/

+���5)����������� U�.r+5)��-���+���)��+� B���+�� ��B���+(�?���`��B�������)������8�S���.�+R�(���H���B(C���+ ������ ���)��+�'(�)��+�)k�'��s +�-���B���� ��������B"�F�*����v����������)��+� h �����!����� S���.!���!'�

39

Command line arguments

� ���!� �������*� ,���!� � � ���*�� � � �'� & ,���!� �%��� � � ��� � �*� � � � � ��� ��� � � �*� � � ��� ,� ��� ����� � , � ��� ��� � � � � , � ��� ����� � � � ,� � � � � �'�%�*� � � � � � � �!� � � ��� �

� ����� � � ��������� ��� ��� ��� � � � � � ��� � � �����!�!��� �-"� � � ��� � � � �� ����� �����!�!���)�� �-, � � ��� ,

� �� �*� � � ��� �� � � � � �!& & � ��� �!�� �� � �� � �*� � � � ��� ���!& & � � " � ��� ��� � ��� � ���* ��� ��� " � �*� � � � ��� �

40

Page 21: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

Optimization for speed

� Perl procedure calls are expensive, especially if you’repassing in arrays or hashes. So feel free to inlinesubroutines called within a tight loop. It’s not as nice aprogramming style, but the performance gain is worth it.You should put comments in the code, however, indicatingwhere you’re doing this and that speed gain is the reasonfor it.

� Use hashes instead of arrays when possible.

41

Perl hints – to be used in A1 to A3

� Write readable code! Perl will let you write cryptic code,but Perl code does not have to be cryptic.

� Use the -w flag. It will give you many warnings thatalmost always point out errors in your code. (but slower)

� Use ”use strict;” at the beginning of your code. It forcesyou to declare your variables and gives you error messageswhich, once fixed, make your code easier to read.

� Read Ch.8 of Programming Perl, Common Goofs forNovices, Efficiency and Programming with Style.

42

Page 22: Perl Tutorial - Engineeringdiana/csi5180/perl_tutorial.pdf · 2004-09-07 · Perl Tutorial Diana Inkpen School of Information Technology and Engineering University of Ottawa CSI 5180,

References

� Schwartz, Randal L. and Christiansen, Tom, LearningPerl, second edition, O’Reilly, 1997.

� Wall, Larry; Christiansen, Tom; and Schwartz, Randal L.,Programming Perl, third edition, O’Reilly, 2000.

� Perl Tutorial http://www.ncsa.uiuc.edu/General/Training/PerlIntro/� Introduction to Perl http://www.cclabs.missouri.edu/

things/instruction/perl/perlcourse.html� Picking Up Perl, a Freely Redistributable Perl Tutorial

Book http://www.ebb.org/PickingUpPerl/

43