CT30675E Application Development for the Web - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

CT30675E Application Development for the Web

Description:

Assignment 1A will not be uploaded onto holst. ... (Similar to property lists in Director Lingo) 'FirstName' = 'Christian' 'LastName' = 'Cooper' ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 27
Provided by: christia45
Category:

less

Transcript and Presenter's Notes

Title: CT30675E Application Development for the Web


1
CT30675EApplication Developmentfor the Web
  • Week 3Arrays, Hashes and Subroutines

2
Whats in store this week
  • Assignment Details
  • (Brief) Review of Week 2
  • Arrays (_at_)
  • Hashes ()
  • Subroutines ()
  • verdi.tvu.ac.uk

3
Assignment Details
  • Assignment 1A will not be uploaded onto holst.
    Instead, it is to be submitted online as a zip
    archive, consisting of the myWebSite/Perl
    directory with your CGI scripts written to run on
    a Windows-based machine running IndigoPerl
  • Assignment 1B will be uploaded to holst.
  • More details about this at the end...

4
(Brief) Review of Week 2
  • Introduction to Perl
  • Basics
  • Scalar Variables ()
  • Quoted Stings (",')
  • Control Statements
  • Sequence
  • Selection (if ... else, unless ... else)
  • Iteration (while, until, do ... while, do ...
    until, for, foreach)
  • CGI.pm

5
Arrays (_at_)
  • Arrays are ordered lists of scalars
  • They are indexed by number
  • Like in C/Java, array indices start at 0 - be
    careful of those off-by-one errors!
  • Unlike Java, trying to access an element out of
    bounds does not result in an error

6
Arrays (_at_)
  • Negative subscripts count from the end.
  • Entire arrays are prefixed with the symbol
  • _at_
  • e.g. _at_days, _at_array
  • Think of the _at_ prefix as something like these
    or those

7
Arrays (_at_)
  • However, the elements in the array are scalars,
    and should be referred to as such
  • days0, array5

_at_days
8
Arrays (_at_)
  • You can also refer to slices of arrays
  • _at_days0..2 ("Su","Mo","Tu")
  • (Note the _at_ prefix - these are lists)
  • You can get the size of an array by using the
    prefix e.g. days

9
Arrays (_at_)
  • This returns the value of the highest index in
    the array - which is 1 less than the actual size
    of the array e.g. days is 6
  • Or you can simply call the array in scalar
    context
  • scalar(_at_days) this is 7

10
Arrays (_at_)
  • Arrays are sized dynamically
  • Referring to an array index out of bounds will
    grow that array to that size

days10 "Miracle"
11
Arrays (_at_)
  • Alternatively, you can adjust the size of an
    array by assigning to
  • If you assign a smaller number, the array will be
    truncated - assigning -1 will make the array
    empty
  • If you assign a larger number, the array will
    grow, and the new elements will be undef - just
    as when referring to an out-of-bounds index.

12
Arrays (_at_)
days 10
days 3
13
Arrays (_at_)
  • Array Operations
  • push
  • Appends a scalar or list to the end of the array.
  • pop
  • removes the last element, decreasing the size of
    the array by one.
  • shift
  • removes the first element, shuffling the others
    down.
  • unshift
  • inserts a scalar or list into the start of an
    array, pushing the exisiting up.
  • splice

14
Arrays (_at_)
  • Initialising an array can be done in a number of
    ways
  • push/unshift/splice functions
  • push _at_days, (Mo,Tu,We)
  • _at_days (Mo,Tu,We)

15
Hashes ()
  • A hash is an associative array of scalars.
  • An unordered collection, indexed by keys
  • (Similar to property lists in Director Lingo)

FirstName gt Christian LastName gt
Cooper StudentID gt 10999999
16
Hashes ()
  • Entire hashes are prefixed with the symbol
  • e.g. properties, hash
  • As with arrays, a hash element is scalar and must
    be prefixed with a
  • e.g. propertiescolour
  • Note the braces around the property name

17
Hashes ()
properties
FirstName gt Christian LastName gt
Cooper StudentID gt 10999999
18
Hashes ()
  • Like arrays, they grow and shrink dynamically
  • Calling a hash element by key automatically
    creates the element if it didnt exist
  • You can preallocate size to the hash by using the
    keys() function
  • e.g. keys(properties) 64

19
Hashes ()
  • Operations on Hashes
  • keys
  • returns a list of all the keys in the hash
  • values
  • returns a list of all the values in the hash
  • each
  • returns a (key, value) pair of the next element
    in the hash
  • exists
  • checks for the existence of the hash (or array)
    element

20
Hashes ()
  • delete
  • deletes the element from the hash (this function
    also works on array elements)
  • Perl will return each, keys and values in a
    seemingly random order.
  • However, the order is guaranteed to be the same
    for values and keys/each.
  • The each and foreach functions can be used to
    iterate over the whole hash.

21
Subroutines ()
  • Perl, like most languages allows the definition
    of user-defined functions
  • Subroutines
  • Subroutines can be declared anywhere in a perl
    file (they are all generated at compile-time), or
    loaded in from modules

22
Subroutines ()
  • To write a subroutine you use the sub function
  • sub hello
  • print hello, world\n
  • return
  • Typically, you will give a subroutine an
    identifier
  • The identifier of the subroutine above is hello

23
Subroutines ()
  • The identifier of a function is usually prefixed
    with the symbol
  • ...and followed by round brackets
  • ()
  • ...though the prefix (and parentheses) can be
    omitted in certain situations so long as it is
    not ambiguous

24
Subroutines ()
  • ...So the following are all more or less
    equivalent
  • call_function()
  • call_function()
  • Subroutines can take parameters
  • print square(5)
  • call_function(name, number)

25
Subroutines ()
  • Parameters are passed in the array _at__ which can
    only be accessed by the subroutine when called
  • So a single parameter would be in _0
  • Two parameters would be in _0 and _1
  • Subroutines can return values back to the main
    program
  • sub square
  • my x _0
  • return x x

26
Subroutines ()
  • Variables can be declared as local to any
    enclosing block structure (stuff in curly braces
    ) using the keyword my
  • so in
  • sub square
  • my x _0
  • if (x 0)
  • my y 3
  • return 0
  • return x x
  • x is private to the subroutine square
  • y is private to the if statement
Write a Comment
User Comments (0)
About PowerShow.com