References and Anonymous Structures - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

References and Anonymous Structures

Description:

A reference can 'refer' to conventional data types (scalars, arrays, hashes) and ... You don't want to be bogged down with having to give them names each time. ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 25
Provided by: joel6
Category:

less

Transcript and Presenter's Notes

Title: References and Anonymous Structures


1
References and Anonymous Structures
  • References to existing variables
  • Using references in subroutines
  • References to anonymous structures
  • Nested data structures
  • Circular references and other nasties

2
What's a reference?
  • A fundamental Perl data type (scalar, array,
    hash, etc)
  • Loosely equivalent to C (and other language)
    pointers

3
What's a reference?
  • A reference can 'refer' to conventional data
    types (scalars, arrays, hashes) and others
    (subroutines, typeglobs, filehandles)
  • The "pointer" is the reference the memory
    address being pointed to is the referent.
  • Perl takes care of memory issues, memory leaks,
    dangling pointers, etc. Isn't that great?!
  • A reference is just another scalar

4
Examples of references
  • Some variables
  • dog "bark"
  • _at_animals qw(cat cow iguana)
  • sounds ( dog gt dog,
  • cat gt "meow",
  • iguana gt "blarg" )
  • Here are references to above variables
  • rs_dog \dog
  • ra_animals \_at_animals
  • rh_sounds \sounds

5
Dereferences
  • Dereferencing means getting at the value that a
    reference points to.
  • If r is a reference, use r, _at_r, or r
    (depending on the data type of the value r
    refers to).
  • You must use the correct prefix!

6
Dereference Examples
  • Scalars
  • dog 'bark'
  • rs_dog \dog
  • print rs_dog prints 'bark'
  • number 1
  • rs_number \number
  • rs_number 5
  • print rs_number prints '6'
  • print number also prints '6'

7
Dereference Examples
  • Arrays
  • _at_animals qw(cat cow iguana)
  • ra_animals \_at_animals take a reference
  • push (_at_animals, 'goat')
  • push (_at_ra_animals, 'goat') same thing
  • print animals1 prints 'cow'
  • print ra_animals1 also prints 'cow'
  • Alternate (CANONICAL) notation for above
  • print ra_animals-gt1 prints 'cow'

8
Dereference Examples
  • Hashes
  • sounds (dog gt 'bark',
  • cat gt 'meow',
  • iguana gt 'blarg')
  • rh_sounds \sounds
  • sounds'pig' 'oink'
  • rh_sounds'pig' 'oink' same thing
  • print rh_sounds'dog' prints 'bark'
  • Alternate (CANONICALA) version of above
  • print rh_sounds-gt'dog' prints 'bark'

9
Canonical way to deref arrays and hashes
  • Please use the arrow notation rather than "double
    dollar sign" for dereferencing hashes and arrays
  • _at_animals qw(cat cow iguana)
  • ra_animals \_at_animals
  • print ra_animals1 . "\n" NO
  • print ra_animals-gt1 . "\n" YES
  • sounds (dog gt 'bark',
  • cat gt 'meow')
  • rh_sounds \sounds
  • rh_soundssheep 'baa' NO
  • rh_sounds-gtsheep 'baa' YES

10
Using References
  • Thats great, but why use references?
  • Doesnt it just make things more complicated
    looking with all the arrows?

11
Using References
  • References let you efficiently pass arrays and
    hashes to subroutines, since you're only passing
    the reference, not the entire structure.
  • Essential for passing more than one list to a
    subroutine (2 arrays, 3 hashes, etc)
  • Essential for creating complex data structures
    (more on that later)

12
Problem!
  • add patients in _at_new_patients to patients
  • my _at_new_patients qw(fido snowball spike)
  • my patients (fifi gt 3,
  • rex gt 12,
  • garfield gt 1)
  • add_new_patients (_at_new_patients, patients)
  • sub add_new_patients
  • my (_at_new_patients, patients) _at__ OOPS
  • for my patient (_at_new_patients)
  • patientspatient 1

13
Passing references to subroutines
  • my _at_new_patients qw(fido snowball spike)
  • my patients (fifi gt 3,
  • rex gt 12,
  • garfield gt 1)
  • my ra_new_patients \_at_new_patients
  • my rh_patients \patients
  • add_new_patients(ra_new_patients, rh_patients)
  • sub add_new_patients
  • my (ra_new_patients, rh_patients) _at__
  • for my patient (_at_ra_new_patients)
  • rh_patients-gtpatient 1

14
View Of Perl Internals
  • See demonstration on chalkboard.
  • (how Perl does reference counting, garbage
    collection, how references point to values, etc)

15
Anonymous Storage
  • Anonymous storage refers to creating values that
    aren't associated with any variable.
  • "Taking references to existing data is helpful,
    but for dynamic programming, it become
    cumbersome. You need to be able to grow data
    structures at will, to allocate new arrays and
    hashes (or scalars or functions) on demand. You
    don't want to be bogged down with having to give
    them names each time."
  • (from the Perl Cookbook)

16
Anonymous Storage arrays
  • ra_array
  • creates an empty, anonymous array (the
  • right side of the equation), and a
  • reference to that array (left side)
  • ra_animals 'dog', 'cat', 'iguana'
  • creates a 3-element anonymous array, and
  • a reference to that array.
  • NOTE it's shorthand for doing this
  • _at_animals qw(dog cat iguana)
  • ra_animals \_at_animals

17
Anonymous storage arrays
  • ra_animals 'dog', 'cat', 'iguana'
  • print ra_animals-gt2 prints 'iguana'
  • ra_animals2 ('dog', 'cat', 'iguana')
  • What is this? A gotcha, NOT a
  • reference...

18
Anonymous Storage hashes
  • rh_hash
  • creates an empty anonymous hash
  • rh_sounds dog gt 'bark',
  • cat gt 'meow',
  • iguana gt 'blarg'
  • creates a 3-key anonymous hash.
  • NOTE it's shorthand for this
  • sounds (dog gt 'bark',
  • cat gt 'meow',
  • iguana gt 'blarg')
  • rh_sounds \sounds

19
Anonymous storage hashes
  • rh_sounds dog gt 'bark',
  • cat gt 'meow',
  • iguana gt 'blarg'
  • print rh_sounds-gt'cat' prints ?
  • rh_sounds-gt'pig' 'oink'

20
Nested Data Structures
  • Arrays and hashes can contain only scalars, so
    they can't directly contain another array or hash
  • But wait, references are scalars, and references
    can point to arrays or hashes!
  • So to build a nested data structure, use
    references for one or more elements in an array
    or hash

21
Nesting in Action w/ Arrays
  • _at_canines qw(fido spike)
  • _at_felines qw(fluffy muffy)
  • ra_canines \_at_canines
  • ra_felines \_at_felines
  • _at_pets (ra_canines, ra_felines)
  • print pets1-gt0 prints 'fluffy'
  • print pets0-gt0 prints what?

22
More array nesting
  • or do it anonymously...
  • _at_pets ( 'fido', 'spike',
  • 'fluffy', 'muffy' )
  • print pets1-gt0 prints what?
  • print pets0-gt1 prints what?
  • yet another way to write it...
  • _at_pets ( qw(fido spike),
  • qw(fluffy muffy) )

23
Nesting in Action w/ Hashes
  • _at_canines qw(fido spike)
  • _at_felines qw(fluffy muffy)
  • rh_canines \_at_canines
  • rh_felines \_at_felines
  • pets ( canines gt rh_canines,
  • felines gt rh_felines )
  • print pets'felines'-gt0 prints 'fluffy'
  • print pets'canines'-gt1 prints what?

24
More hash nesting
  • or, do it anonymously
  • pets ( canines gt 'fido', 'spike',
  • felines gt 'fluffy', 'muffy' )
  • print pets'felines'-gt0 prints 'fluffy'
  • print pets'canines'-gt1 prints what?
  • yet another way to write it...
  • pets ( canines gt qw(fido spike),
  • felines gt qw(fluffy muffy) )
Write a Comment
User Comments (0)
About PowerShow.com