Create rich data structures using mixed reference types.
- Much of the power of references is the ability to mix different types of data into one data structure. Here's a working copy of mixed.pl from chapter 11 of the Exercise Files. And you'll notice that I have here a reference to an anonymous array with anonymous hashes in each element of the array. So, the square brackets at the beginning and end here of this structure, that indicates the anonymous array. And then, each of these lines is an anonymous hash enclosed in the curly brackets, with name, instrument, and genre for each of the keys.
And then, down here for each loop, I go through the array, and for each element of the array, then I de-reference the hash for the results. And so, when I run this, you see I get, in the order, because it's an array and a for each loop, I get Jimi Hendrix, Miles Davis, Yo-Yo Ma, and Frank Zappa. Now, given this structure, I can add a row or add a record with a push, like that. And then, when I run it, you see we have one more element in our array, Elton John, plays piano and plays rock.
In fact, I could just start with an empty anonymous array, like this, and each of these could be a push, like this. And then, these need semicolons at the ends of them. And now, again, when we run it, we have exactly the same result. But we started with an empty anonymous array, and we've simply pushed elements onto it, which is probably a very common way to do something like this.
What if we want more than one item in a particular place? Like, for instance, this, under instrument, Elton John both plays piano and he sings, right? And so, if I put this in square brackets, now I have an anonymous array as an element in my hash. And if I run this, you'll notice that it says ARRAY there. And that's because instrument, now, in that one case, it actually has an array reference.
And you see, there's an address next to the array. That address, of course, will be different on your computer. That indicates that it's an array reference. And so, what I can do, here, is I can come in, and I can say, my instrument, I'm just gonna say i-n-s-t, equals, and I check to see if it's a reference. And if it is, if it equals the string ARRAY. And then, I can use a conditional operator. And so, I'm just gonna put in my punctuation there. And if it is an array, I'm gonna use the join function, which we'll learn about later.
But, what join does is, it joins an array into a string with a separator. And so, you'll see what this looks like. And if it's not a reference, then I simply put in the original de-referenced string. And now, over here in my say, I'm gonna take this out and just put in my instrument scalar. And now, when I run it, you'll notice that for Elton John, it says Piano/Vocal, because I have both of these elements in there.
And for the other ones, they're scalars. So, it's able to tell using the ref function, if it's an array or if it's not an array. And if it's not an array, we just assume that it's a scalar. And this allows us to optionally use an array for a field that has more than one value. Of course, we can do this for genre, we can do this for a lot of purposes. This is just to illustrate that references are an extremely powerful tool in Perl. Using references, you can create a broad variety of data structures, with elegance, and simplicity, and flexibility.
I strongly encourage you to take the time to experiment with this feature, so that you may fully understand it and harness its remarkable power in your own scripts.
Watch to learn the details of the Perl syntax, from variables, conditionals, loops, and data structures to regular expressions, functions, and references. A quick-start guide is included for experienced developers who want to get up and running with Perl 5 fast, and the entire course is recommended for both new and experienced programmers alike. Later chapters cover file handling and reusing code with Perl modules, plus Perl best coding practices.
- Understanding Perl's general syntax and the anatomy of a Perl script
- Writing statements and expressions
- Creating assignments
- Working with variables and strings
- Using data types effectively
- Defining logical flow with conditionals and loops
- Using special variables
- Using Perl operators
- Performing simple Perl programming tasks with expressions
- Matching data
- Defining and calling functions
- Using references
- Handling files in the file I/O
- Using built-in functions
- Reusing code with modules
- Coding with Perl best practices
Skill Level Intermediate
Programming Foundations: Refactoring Codewith Simon Allardice1h 44m Intermediate
1. Setting Up
About Perl3m 36s
2. Quick Start
3. Basic Syntax
4. Values and Variables
7. Special Variables
9. Regular Expressions
11. References and Structures
12. File I/O
13. Built-In Functions
15. Best Practices
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.