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 @_,
Note that there is a closure on %stuff (as well as $writer), so attention must be paid to scoping and initialization.sub writeBasicTag { for (@_) { $writer->startTag($_); $writer->characters($stuff{$_}); $writer->endTag($_); } 1; }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |