If I understand correctly, you want to call that like, writeBasicTag( 'year' ); , and produce XML with data from $year. You are trying to do it with symbolic references, which will work under no strict 'refs'; , but is not best practice. If you change 'XXX' to '$$tag', you'll get what you want, but there is a better way.

The rule of thumb is: When you want to use symrefs, use a hash instead, my %stuff = ( year => '1999', foo => 'bar', baz => 'quux', ); Now there is no reason to restrict yourself to single arguments. You ought to be able to call your sub for a list of tags, so let's rewrite to loop over @_,

sub writeBasicTag { for (@_) { $writer->startTag($_); $writer->characters($stuff{$_}); $writer->endTag($_); } 1; }
Note that there is a closure on %stuff (as well as $writer), so attention must be paid to scoping and initialization.

Update: ++The Mad Hatter for typo correction.

After Compline,
Zaxo


In reply to Re: indirectly accessing variable by Zaxo
in thread indirectly accessing variable by santellij

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.