Create and use references to arrays and anonymous arrays.
- [Voiceover] Array references are a common technique in Perl. They are simple, convenient, and you'll probably use them often. Here's a working copy of arrayref.pl from chapter 11 of the exercise files. You'll notice down here on line seven, I have an array and that array is initialized using the quote word operator. And then I take the reference of the array using the reference backslash operator, and I assign it to the scalar, which now contains the reference to the array.
And then I use that reference for the foreach loop. I'm dereferencing using the at operator to say that I'm getting the entire array from the reference. And the reference itself is inside of curly brackets. So when I save this and run it, you see it does exactly what we expect. It passes each individual element of the array to the console using say. It's worth noting here that in many cases, it's possible and allowable and legal to omit those curly brackets.
And when I save this and run it, you'll notice it still works. Personally, I suggest that you resist the temptation to do this. The curly brackets are necessary where there may be sufficient ambiguity, confuse the parser, and you can't always really tell in advance when that's gonna be. I mean you could if you were to really analyze it, but when you're just typing out code, you're not always gonna notice all those circumstances. And that can be difficult to debug. So it's just two characters, and for my part, I try to remember to always include, and sometimes I forget.
But I do try to remember to always include them, even though it's optional. You can initialize the reference directly with an anonymous array instead of initializing it from the array, so I can put in some square brackets here, and I can just take this quote word with the string in it and I can paste that in, and now I don't need this line anymore, and I have the reference directly from an anonymous array. So inside the square brackets here, that's an anonymous array. And anonymous array returns a reference, and I assign that to my scalar, and now I have a reference to the array.
It works exactly the same. We'll go ahead and save and run this. You see we have exactly the same result. We just just don't need the actual array. So if you don't have a otherwise need for the array, then oftentimes it's a lot more convenient to simply initialize your reference from an anonymous array just like this. Now it's important to note that an anonymous array is not the same as an anonymous list. If I use parentheses here instead of the square brackets, I now have a list. I'm gonna go ahead and just put in 1, 2, 3 here, and I now have a list, and you'll notice I'm getting errors already.
If I try to run this, you see I get an error, because you cannot take a reference from a list. Now sometimes Perl might not complain, and it might just give you references, individual references to one, two, and three, especially if you have variables in there instead of literals. Actually with the literals, it probably won't, because that's not legal either. But what's important to realize here is that the parentheses makes a list, not an array, and a list is a literal object, and so it doesn't have an address, and so you can't take a reference to it.
Instead, let's go ahead and put in the square brackets here, and that is an anonymous array, and that works. When I run this, I now get one, two, and three as the values. So it's important to remember, parentheses do not give you an anonymous array. They give you an anonymous list, which you cannot use a reference with, so you need the anonymous array, and the square brackets are what will give you the anonymous array. So you can access individual elements of the target array by dereferencing the array like this.
And so I'm gonna say $ and, because I want an individual element, and I'm gonna say ref inside the curly braces, and I'm gonna index that with the element number zero, which is the number one, and I'm gonna just assign a string to that instead, one. And now when I go ahead and run this, you see I have a one in that first position in my result. So what I've done is I've dereferenced the array, and I've accessed an individual member of the array, and I've changed its value.
So it's actually much easier and more common to do this using the arrow operator like this. This is the easy way, and this is the way you will most often see this. If I go ahead and save and run this, you'll see I get exactly the same result. So with the arrow operator is simpler, and it's the more common way to do this. So array references are very common in Perl. You'll see more examples of this in the rest of the course.
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