Perl Library Functions

Similar documents
Perl. Perl. Perl. Which Perl

1. Introduction. 2. Scalar Data

Perl. Many of these conflict with design principles of languages for teaching.

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

Modularity and Reusability I. Functions and code reuse

CHAD Language Reference Manual

package YourModule; require = = qw(munge frobnicate); # symbols to export on request

COP4020 Programming Assignment 1 - Spring 2011

More Perl. CS174 Chris Pollett Oct 25, 2006.

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Run-Time Data Structures

G Programming Languages - Fall 2012

Perl Scripting. Students Will Learn. Course Description. Duration: 4 Days. Price: $2295

COMS 3101 Programming Languages: Perl. Lecture 1

Variables and Bindings

Principles of Computer Science

Learning Perl Objects, References, and Modules

CSCI-GA Scripting Languages

autodie - Replace functions with ones that succeed or die with lexical scope # Recommended: implies 'use autodie qw(:default)'

Ruby: Introduction, Basics

Programming with Java

Lexical Considerations

Object oriented programming C++

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

Week 4 Lecture 1. Expressions and Functions

# Blocking dequeue with 5-second timeout if (defined(my $item = $q->dequeue_timed(5))) { # Work on $item }

Sprite an animation manipulation language Language Reference Manual

Introduction to Perl Session 6. special variables subroutines Introduction to Perl

Project 2: Scheme Interpreter

14. Pointers, Algorithms, Iterators and Containers II

BASH SHELL SCRIPT 1- Introduction to Shell

PERL Scripting - Course Contents

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Data::Dumper - stringified perl data structures, suitable for both printing and eval

Subroutines. Subroutines. The Basics. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc.

Lexical Considerations

Common LISP Tutorial 1 (Basic)

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

COMP284 Scripting Languages Lecture 2: Perl (Part 1) Handouts

Draw a diagram of an empty circular queue and describe it to the reader.

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Data::Dumper - stringified perl data structures, suitable for both printing and eval

Classnote for COMS6100

(Refer Slide Time: 01:12)

It can be confusing when you type something like the expressions below and get an error message. a range variable definition a vector of sine values

Type Checking Binary Operators

Full file at

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

FORM 2 (Please put your name and form # on the scantron!!!!)

Makefile Brief Reference

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

Dynamically-typed Languages. David Miller


CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

15. Pointers, Algorithms, Iterators and Containers II

Functional Programming Languages (FPL)

Web Application Development

CS1 Recitation. Week 2

BLM2031 Structured Programming. Zeyneb KURT

Topic 1: Introduction

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Hands-On Perl Scripting and CGI Programming

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

ENERGY 211 / CME 211. Functions

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Computer Systems Lecture 9

Subroutines in Perl. Jon-Michael Deldin. Dept. of Computer Science University of Montana September 12, 2011

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Call Stacks + On Writing Good Code

Review of the C Programming Language

COMS 3101 Programming Languages: Perl. Lecture 6

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CSE 12 Abstract Syntax Trees

Perl Tutorial. Diana Inkpen. School of Information Technology and Engineering University of Ottawa. CSI 5180, Fall 2004

Bash command shell language interpreter

CS558 Programming Languages

Pointers, Dynamic Data, and Reference Types

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Lecture 5/6: Scripting and Perl

Pointers (continued), arrays and strings

A control expression must evaluate to a value that can be interpreted as true or false.

Chapter 17. Fundamental Concepts Expressed in JavaScript

Functions. Lecture 6 COP 3014 Spring February 11, 2018

perl -MO=Deparse[,-d][,-fFILE][,-p][,-q][,-l] [,-sletters][,-xlevel] prog.pl

컴퓨터개념및실습. 기말고사 review

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

GB Programming Challenges

LECTURE 3. Compiler Phases

COMS 3101 Programming Languages: Perl. Lecture 5

TYPES, VALUES AND DECLARATIONS

C++ Programming Lecture 2 Software Engineering Group

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

This is a list of questions and answers about Unicode in Perl, intended to be read after perlunitut.

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Transcription:

Perl Library Functions Perl has literally hundreds of functions for all kinds of purposes: file manipulation, database access, network programming, etc. etc. It has an especially rich collection of functions for strings. E.g. lc, uc, length. Consult on-line Perl manuals, reference books, example programs for further information.

Defining Functions Perl functions (or subroutines) are defined via sub, e.g. sub sayhello { print "Hello!\n"; } And used by calling, with or without &, e.g. &sayhello; # arg list optional with & sayhello(); # more common, show empty arg list explicitly

Defining Functions Function arguments are passed via a list variable @, e.g. sub mysub { @args = @_; print "I got ",@#args+1," args\n"; print "They are (@args)\n"; } Note that @args is a global variable. To make it local, precede by my, e.g. my @args = @_;

Defining Functions Can achieve similar effect to the C function int f(int x, int y, int z) { int res;... return res; } by using array assignment in Perl sub f { my ($x, $y, $z) = @_; my $res;... return $res; }

Defining Functions Lists (arrays and hashes) with any scalar arguments to produce a single argument list. This in effect means you can only pass a single array or hash to a Perl function and it must be the last argument. sub good { my ($x, $y, @list) = @_; This will not work (x and y will be undefined): sub bad { my (@list, $x, $y) = @_; And this will not work (list2 will be undefined): sub bad { my (@list1, @list2) = @_;

References References are like C pointers (refer to some other objects) can be assigned to scalar variables are dereferenced by evaluating the variable Example: $aref = [1,2,3,4]; print @$aref; # displays whole array... $$aref[0]; # access the first element... ${$aref}[1]; # access the second element... $aref->[2]; # access the third element

Parameter Passing Scalar variable are aliased to the corresponding element of @. This means a function can changed them. E.g. this code sets x to 42. sub assign { $_[0] = $_[1]; } assign($x, 42); Arrays & hashes are passed by value. If a function needs to change an array/hash pass a reference. Also use references if you need to pass multiple hashes or arrays. %h = (jas=>100,arun=>95,eric=>50); @x = (1..10) mysub(3, \%h, \@x); mysub(2, \%h, [1,2,3,4,5]); mysub(5, {a=>1,b=>2}, [1,2,3]); Notation: [1,2,3] gives a reference to (1,2,3) {a=>1,b=>2} gives a reference to (a=>1,b=>2)

Perl Prototypes Perl prototypes declare the expected parameter structure for a function. Unlike other language (e.g. C) Perl prototype s main purpose is not type checking. Perl prototypes main purpose is to allow more convenient calling of functions. You also get some error checking - sometimes useful, sometimes less so. Some programmers recommend against using prototypes. Use in COMP2041/9041 optional. You can use prototypes to define functions that can be called like builtins.

Perl Prototypes Prototypes can cause a reference to be passed when an array is given as a parameter. If we define our version of push like this: sub mypush { my ($array_ref,@elements) = @_; if (@elements) { @$array_ref = (@$array_ref, @elements); } else { @$array_ref = (@$array_ref, $_); } It has to be called like this: mypush(\@array, $x); But if we add this prototype: sub mypush2(\@@) It can be called just like the builtin push: mypush @array, $x;

Recursive example sub fac { my ($n) = @_; return 1 if $n < 1; } return $n * fac($n-1); which behaves as print fac(3); # displays 6 print fac(4); # displays 24 print fac(10); # displays 3628800 print fac(20); # displays 2.43290200817664e+18

Eval The Perl builtin function eval evaluates (executes) a supplied string as Perl. For example, this Perl will print 43: $perl = $answer = 6 * 7; ; eval $perl; print "$answer\n"; and this Perl will print 55: @numbers = 1..10; $perl = join("+", @numbers); print eval $perl, "\n"; and this Perl also prints 55: $perl = $sum=0; $i=1; while ($i <= 10) {$sum+=$i++} ; eval $perl; print "$sum\n"; and this Perl could do anything: $input_line = <STDIN>; eval $input_line;

Pragmas and Modules Perl provides a way of controlling some aspects of the interpreter s behaviour (through pragmas) and is an extensible language through the use of compiled modules, of which there is a large number. Both are introduced by the use keyword. use English; - allow names for built-in vars, e.g., $NF = $. and $ARG = $. use integer; - truncate all arithmetic operations to integer, effective to the end of the enclosing block. use strict vars ; - insist on all variables declared using my.

Standard modules These are several thousand stndard Perl modules into packages. The package name is prefixed with :: Examples: use DB File; - functions for maintaining an external hash use Getopt::Std; - functions for processing command-line flags use File::Find; - find function like the shell s find use Math::BigInt; - unlimited-precision arithmetic use CGI; - see next week s lecture.

CPAN Comprehensive Perl Archive Network (CPAN) is an archive of 150,000+ Perl packages. Hundreds of mirrors, including http://mirror.cse.unsw.edu.au/pub/cpan/ Command line tools to quickly install packages from CPAN.

CPAN Comprehensive Perl Archive Network (CPAN) is an archive of 150,000+ Perl packages. Hundreds of mirrors, including http://mirror.cse.unsw.edu.au/pub/cpan/ Command line tools to quickly install packages from CPAN.