Hello,

Here is something I cooked in 4 minutes. Here is how it works:

  1. Slurp the entire file into a string, removing all white spaces since we won't need them.
  2. Get the text matching "@somesthing{somestuff}"
  3. Print the opening tag "<something>"
  4. Match somestuff with "name:method(size);"
  5. Print the xml element '<method name="name" width="size"/>'
  6. Goto 4 until somestuff is depleted
  7. Print closing tag "</something>"
  8. Goto 2 until the file is depleted
There are other ways this can be done of course, but this is how I'd do it.
my $str = join '', map{s/\s+//g;$_}<DATA>; while($str =~ /@(\w+){([^}]*)}/g){ my ($int, $imp) = ($1,$2); print "<$int>\n"; printf qq{\t<%s name="%s" width="%s"/>\n}, $2,$1,$3 while $imp=~/([^:]+):([^(]+)\((\d+)\);/g; print "</$int>\n"; } __DATA__ @interface { i : in(1); o : out(1); } @interface2 { b : out(1); a : in(1); } @interface3 { x : in(1); y : in(1); } @interface4 { f : out(1); b : out(1); } OUTPUT: <interface> <in name="i" width="1"/> <out name="o" width="1"/> </interface> <interface2> <out name="b" width="1"/> <in name="a" width="1"/> </interface2> <interface3> <in name="x" width="1"/> <in name="y" width="1"/> </interface3> <interface4> <out name="f" width="1"/> <out name="b" width="1"/> </interface4>
Of course, this does not handle nested interfaces; did you want that?

Hope this helps,,,

Aziz,,,


In reply to Re: How do I replace brackets with XML tags? by abstracts
in thread How do I replace brackets with XML tags? 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.