You are redeclaring a lexically scoped variable to override a package scoped variable. This is complicated, and I've already rewritten my post about 3 4 times to sort it out:)

Essentially the two %templates variables you have aren't the same -- you are referencing one and obliterating it by replacing it with another. If you move the my above the subroutine definition, this works, and this is the cleanest way to do it I know of, as %templates has some assigned value when you use it -- and thus my will not be stomping over your old work by redefining an existing variable later on.

our creates global variables (and mysteriously allows the post-assignment to work -- which in theory it should not if it was a top-down compile), but don't use our since globals are often evil. Always declaring with my is a very safe bet, especially if you do it towards the top of your function and/or package like you were coding to an anal C compiler.

Using strict inside that top package would have made this more clear that your variable wasn't well defined. Somewhat more clear, anyway. It's a good reason to use strict.

There is also a decent article that a lot of Monks mention, that you might want to read: Coping With Scoping.

I'm looking forward to Perl6's more object-like syntax, so hopefully this will simplify itself. One can hope.


In reply to Re: blessing a hash ref? by flyingmoose
in thread blessing a hash ref? by stonecolddevin

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.