Perl why use hash references




















With references, you can create not only two-dimensional but also multidimensional data structures. Noticed that backslash is used in front of the scalar variable. There are two parts:. Creating a hash reference is similar to creating an array reference, for example, suppose you have a hash:.

In this tutorial, you have learned basic Perl reference including scalar reference, array reference, and hash reference. We will show you various types of references in Perl in the next tutorial so make sure that you understand the concepts in this tutorial before moving on. It autoquotes the string on its left and makes hash initializations less syntactically noisy. A hash ref is an abbreviation to a reference to a hash. References are scalars, that is simple values.

It is a scalar value that contains essentially, a pointer to the actual hash itself. Link: difference between hash and hash ref in perl - Ubuntu Forums. There is also another link here that has more information on perl and references.

See perldoc perlreftut which is also accessible on your own computer's command line. A reference is a scalar value that refers to an entire array or an entire hash or to just about anything else. Names are one kind of reference that you're already familiar with. Think of the President of the United States: a messy, inconvenient bag of blood and bones.

But to talk about him, or to represent him in a computer program, all you need is the easy, convenient scalar string "Barack Obama". References in Perl are like names for arrays and hashes. They're Perl's private, internal names, so you can be sure they're unambiguous. Unlike "Barack Obama", a reference only refers to one thing, and you always know what it refers to. If you have a reference to an array, you can recover the entire array from it. If you have a reference to a hash, you can recover the entire hash.

But the reference is still an easy, compact scalar value. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What's the difference between a hash and hash reference in Perl? Ask Question. Asked 11 years, 11 months ago. Active 5 years, 8 months ago. Viewed 55k times. Since a reference to an array or hash is a single value, the reference to the whole array takes only one positional argument.

There is also an added performance benefit of passing references instead of whole arrays and hashes. In computer science, this is known as "passing by reference" instead of "passing by value". Lastly, when you pass a reference into a function, that function can change the value of the passed variable "in place" without getting returned explicitly. This technique is useful at time, but sometimes these "side effect" practices are discouraged, so use this only when it makes sense, okay?

While this example demonstrated the concept, this is one of those cases where it is not okay to do this. I would instead not use the reference and have the code return the new value.

NOTE: This is more advanced and references techniques from later in this document. If you are new to this topic, feel free to skip onto the next section. In the Ruby 1. This is how a perl function is invoked, where the parameters are assembled into an array, and the function must parse out the variables at the positions it expects.

However, any name-value pairs specified at the end of the call are put into a hash, which is still passed as the last element of the args array. If we don't expect the args array to end with a hash, we can use this trick to pop off the hash as the last element of the args array, which we can use to control preferences for the function.

We can use this trick as well with Perl when we also would not otherwise expect the last argument to be a hash reference or a closure anonymous function reference. If it was, I use pop to take it off the end of the array, otherwise I set it to a default empty value. A table or 2-dimensional array is not a native perl datatype as it is in many languages. Instead, perl allows you to build your own as an array of arrays.

Now since perl array elements can only be scalar variables, we need to use a reference instead. So a perl table is actually an array of references.

Often, it's best to bite the bullet and fully embrace a reference when you are using a data structure like this. It helps me to think of it as a starting point, and makes the syntax more friendly in the long run. See how I defined the table using the [ ] array reference syntax? It should make it more clear to write and read. Also, see the difference of addressing table elements set up as starting with an array instead of a array reference?

It can trip you up, and I suggest always use a reference to avoid the confusion, because mixing syntax styles in a program is painful.

Using a consistent syntax within a large program and complex data structures reduces chances for errors. Again, let's start with a reference to the result set. Did you follow all that? You may want to parse through it a few times. Here are a few notes:. Now for the fun part. Okay, now imagine you need to write a subroutine that takes a reference, and need to determine what the datatype is for the reference? The ref unary operator takes a variable and returns the datatype.



0コメント

  • 1000 / 1000