Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Embedding a mini-language for XML construction into Perl

by tmoertel (Chaplain)
on Nov 18, 2005 at 23:46 UTC ( [id://510000]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    # html {
    #     head { title { text "Title" } };
    ...
    #         p { class_ "warning"; text "paragraph" }
    #     };
    # }
    
  2. or download this
    # doc {
    #     my_elem {
    #         # children go here
    #     };
    # };
    
  3. or download this
    # my_elem {
    #     text "some text";
    #     my_attr_ "value";
    # };
    
  4. or download this
    our $__frag;  # points to fragment under active construction
    
    ...
    sub text($) {
        push @{$__frag->[2]}, @_;
    }
    
  5. or download this
    sub define_vocabulary {
        my ($elems, $attrs) = @_;
        eval "sub $_(&) { _elem('$_',\@_) }"     for @$elems;
        eval "sub ${_}_(\$) { _attr('$_',\@_) }" for @$attrs;
    }
    
  6. or download this
    BEGIN {
        define_vocabulary(
    ...
            [qw( src href class style )]
        );
    }
    
  7. or download this
    my $my_doc = doc {
        html {
    ...
    #     ]
    #   ]
    # ]
    
  8. or download this
    use XML::Writer;
    
    ...
        $render_fn->($doc);
        $writer->end();
    }
    
  9. or download this
    render_via_xml_writer( $my_doc, DATA_MODE => 1, UNSAFE => 1 );
    # <html>
    ...
    # <p class="warning">paragraph</p>
    # </body>
    # </html>
    
  10. or download this
    sub render_doc(&) {
        my $docfn = shift;
    ...
            UNSAFE => 1
        );
    }
    
  11. or download this
    render_doc {
        html {
    ...
    # <p>Plus paragraph number 5.</p>
    # </body>
    # </html>
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://510000]
Approved by thor
Front-paged by stvn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-29 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found