Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a piece of code that I'm using Perl to convert to XML. I'm not quite sure how to convert the closing brackets to closing xml tags. For instance, I want to convert:
@interface{ code that I want to keep intact so I can change it too }
to
<interface> code that I want to keep intact so I can change it too </interface>
There are several closing brackets like this, and I'm not sure how to specify specifically which one is which.

Edit: chipmunk 2001-08-02

Replies are listed 'Best First'.
Re: How do I replace brackets with XML tags?
by chromatic (Archbishop) on Aug 02, 2001 at 21:08 UTC
    Keep a stack of tagnames around. This will even work if things are nested. (Of course, if the file is in bad shape, you'll have to go through by hand, but that's the case anyway.) Pseudo-code:
    # find something like @interface push @stack, 'interface'; # parse lines until reaching a closing curly my $tagname = pop @stack; print "</$tagname>";
Re: How do I replace brackets with XML tags?
by abstracts (Hermit) on Aug 03, 2001 at 00:09 UTC
    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,,,

Re: How do I replace brackets with XML tags?
by cacharbe (Curate) on Aug 02, 2001 at 19:33 UTC
    I THINK I know what you are trying to ask, but maybe you could provide us with a concrete example of before and after so that we can better get our monk-minds around the problem.

    C-.

      Before:
      @interface { i : in(1); o : out(1); }
      After:
      <interface> <in name="i" width="1"/> <out name="o" width="1"/> </interface>
      Thanks.

      Edit: chipmunk 2001-08-02

        I screwed up on my reply. Here's another try,
        leaving off the opening on the xml tags so it's
        not interpreted as html:
        Before:

        @interface {
        i : in(1);
        o : out(1);
        }

        After:

        interface>
        in name="i" width="1"
        out name="o" width="1"/
        /interface>

        The main trick is changing that closing bracket,
        since there are other closing brackets that have
        different names.
        Thanks.