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.


In reply to Re^9: Preparing data for Template by haukex
in thread Preparing data for Template by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.