All of the above comments are valid. But if one: a) has used symbolic refs successfully in other situations; b) does not recognize that symbolic refs find only package variables; c) does not recognize that "my" variables are lexical; d) uses "my" variables frequently in subroutines to avoid the possibility of duplicate variable names; and e) does not recognize that the use of such constructs in a subroutine are "unsafe", "non-robust", "non-versatile", and difficult to maintain; then one can get very frustrated trying to figure out why something isn't working as expected. Some or all of those things applied to me when I wrote that code initially 2-3 years ago. To someone who may be new to Perl, or is not experienced in some of these things, the distinction may boil down to a difference between using "my" and "local" without understanding what is under the covers.

Thanks for your comments. I was not familar with the "map" function mentioned above so I have something new to learn about.

I rewrote the subroutine in the original snippet long ago by passing a list of array refs rather than the letters so the original snippet is only of value to others if this discussion helps them to avoid the pitfalls mentioned.


In reply to Re: my vs. local subtlety by esr
in thread my vs. local subtlety by esr

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.