Hi all,

I'm parsing an XML file using XML:Parser, with the Style set to Subs. What this means is that, when the parser hits <name> it calls the sub 'name'. When it hits </name> it calls the sub 'name_' . I'm using these subs to turn state variables ($state{name}), on and off so that the char_handler knows where to dump the text held between the tags. The problem is that I'm having to write the start and end subroutine for every tag, and they only differ in the name of the state variable.

My question is this : is there any way of generating a subroutine for every element of an array. Thus, if I have an array @tags=(name, gender, address) is there a way of generating the subs name, name_, gender, gender_, description, description_, where each start subroutine turns a state variable named after the tag on, and the end subroutines turn it off ?

Any aid in pursuing the goal of elegance would be greatly appreciated.

Paul

In reply to generating subroutines from array variables by Anonymous Monk

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.