Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^9: Preparing data for Template

by haukex (Bishop)
on Dec 31, 2020 at 13:27 UTC ( #11126044=note: print w/replies, xml ) Need Help??

in reply to Re^8: Preparing data for Template
in thread Preparing data for Template

Any thoughts on how to get over this mental block would be appreciated.

Do you have more experience in other languages? For example, has the concept of pointers in C / C++, or the concept of variables holding references to objects in Java "clicked" for you? If so, it's probably possible to transfer that idea to references in Perl. A simplified view is: there's the variable that represents the "raw memory" that actually stores the data, e.g. $scalar, @array, or %hash, and then there are the references that basically store the memory address to another piece of data in a $scalar variable.

When you say my %origvars = ( frames => \&get_frames );, you've got a fresh piece of memory and you're actually storing the key frames with its value there. When you say my $vars = \%origvars;, you're asking Perl to store the address of that piece of memory into the variable $vars, so you're not copying the hash, just saving a reference to it. my $vars = { frames => \&get_frames }; simply combines these two things into one step: it allocates a fresh piece of memory somewhere, stores the hash there (it's called an anonymous hash because you're not giving it a name like %origvars in the previous example), and then gives you a reference to that piece of memory.

Dereferencing basically allows you to work with a reference as if it were the original data structure. So for example, $vars->{frames} doescan be used the same as $origvars{frames}, my %foo = %$vars; makes a shallow copy of the hash into %foo the same way that my %foo = %origvars; does, and so on.

An important concept to remember is that Perl (almost) never implicitly references or dereferences anything, you have to do that through the corresponding operators. So in your example above, my suspicion would be that you've mixed up my %hash = (...); and my $hashref = {...};, or you're expecting a the hashref $vars to be automatically dereferenced where it won't be.

Again, all of this is very much simplified in hopes that it might help make things click. A thorough read of perlref will hopefully help.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11126044]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2021-04-15 03:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found