next up previous contents
Next: Files Input Up: References Previous: Interpolating Functions Inside Double-Quoted

Summary

In this chapter you learned about references. References are scalar variables used to hold the memory locations. When references are dereferenced, the actual value is returned. For example, if the value of the reference is assigned like this: $refScalar = \10, then, dereferencing $refScalar would be equal to 10 and would look like this ${$refScalar}. You always can create a reference to a value or variable by preceding it with a backslash. Dereferencing is accomplished by surrounding the reference variable in curly braces and preceding the left curly brace with a character denoting what type of reference it is. For example, use @ for arrays and & for functions.

There are five types of references that you can use in Perl. You can have a reference to scalars, arrays, hashes, functions, and other references. If you need to determine what type of reference is passed to a function, use the ref() function.

The ref() function returns a string that indicates which type of reference was passed to it. If the parameter was not a reference, the undefined value is returned. You discovered that it is always a good idea to check reference types to prevent errors caused by passing the wrong type of reference. An example was given that caused an error by passing a scalar reference when the function expected an array reference.

A lot of time was spent discussing data records and how to access information stored in them. You learned how to step through dissecting a dereferencing expression, how to dynamically add new data records to an associative array, and how to add new data members to an existing record.

The last thing covered in this chapter was how to interpolate function calls inside double-quoted strings. You'll use this technique-at times-to avoid using temporary variables when printing or concatenating the output of functions to other strings.


next up previous contents
Next: Files Input Up: References Previous: Interpolating Functions Inside Double-Quoted
dave@cs.cf.ac.uk