From the course: Perl 5 Essential Training
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Finding the type of a reference - Perl Tutorial
From the course: Perl 5 Essential Training
Finding the type of a reference
- [Voiceover] Sometimes you may need to know the type of a reference, so you can process it accordingly. Perl provides the built-in function "ref" for this purpose. Here is a working copy of ref.pl from Chapter 11 of the Exercise Files. And you will notice up here around line seven, I declare a reference to an anonymous array, and in line eight, I declare a reference to anonymous hash. So, on line seven it's an array ref, and on line eight it's a hash ref. And then I call display_ref twice, once with each of these, and display_ref simply uses the ref function to display the type of the reference. So when I run this, you'll notice that we have two results, one says "array", and one says "hash". The display_ref for the array reference displays the word "array", and for the hash reference, it displays the word "hash". And that's because the ref function right here returns a string, and the string is a representation of type of reference it is. So, for an array reference, it will say…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.