Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

The code would look like this:

my ($message_count, $sent_count); my $parser = new XML::Rules ( rules => [ _default => 'content', message => sub { $message_count++; Mail::Sendmail::sendmail( from => $_[1]->{from}, to => $_[1]->{to}, subject => $_[1]->{subject}, body => $_[1]->{body}, ) or warn "Mail Error: $Mail::Sendmail::error"; $sent_count++ unless $Mail::Sendmail::error; return; }, messages => sub { print "SAX Mailer Finished\n$sent_count of $message_count mess +age(s) sent\n"; return; } ]); $parser->parsefile($file);
or, if you did not want to use any external variables:
my $parser = new XML::Rules ( rules => [ _default => 'content', message => sub { $_[3][-1]->{message_count}++; Mail::Sendmail::sendmail( from => $_[1]->{from}, to => $_[1]->{to}, subject => $_[1]->{subject}, body => $_[1]->{body}, ) or warn "Mail Error: $Mail::Sendmail::error"; $_[3][-1]->{sent_count}++ unless $Mail::Sendmail::error; return; }, messages => sub { print "SAX Mailer Finished\n$_[1]->{sent_count} of $_[1]->{mes +sage_count} message(s) sent\n"; return; } ]); $parser->parsefile($file);
or
my $parser = new XML::Rules ( rules => [ _default => 'content', message => sub { my ($tag_name, $tag_hash, $context, $parent_data) = @_; $parent_data->[-1]->{message_count}++; Mail::Sendmail::sendmail( from => $tag_hash->{from}, to => $tag_hash->{to}, subject => $tag_hash->{subject}, body => $tag_hash->{body}, ) or warn "Mail Error: $Mail::Sendmail::error"; $parent_data->[-1]->{sent_count}++ unless $Mail::Sendmail::err +or; return; }, messages => sub { print "SAX Mailer Finished\n$_[1]->{sent_count} of $_[1]->{mes +sage_count} message(s) sent\n"; return; } ]); $parser->parsefile($file);

I guess I should add a few more ways to add data to the parent node. Apart from 'attributename' that sets (and if needed overwrites) the attribute and '@attributename' that appends the value to the array I should also allow '+attributename' and '.attributename'. '-attributename' is not needed, but I wonder whether to add '*attributename'.

With the '+attributename' the code would look like this:

my $parser = new XML::Rules ( rules => [ _default => 'content', message => sub { Mail::Sendmail::sendmail( from => $_[1]->{from}, to => $_[1]->{to}, subject => $_[1]->{subject}, body => $_[1]->{body}, ) or warn "Mail Error: $Mail::Sendmail::error"; return '+message_count' => 1, '+sent_count' => ($Mail::Sendmai +l::error ? 0 : 1); }, messages => sub { print "SAX Mailer Finished\n$_[1]->{sent_count} of $_[1]->{mes +sage_count} message(s) sent\n"; return; } ]); $parser->parsefile($file);

All code except the last snippet is tested :-)

I'll have a look at SAX, currently the module sits on top of XML::Parser:Expat, but I think it should not be a big deal to change that. Or to change the code so that you can choose what to use.


In reply to Re^2: (RFC) XML::Rules - yet another XML parser by Jenda
in thread (RFC) XML::Rules - yet another XML parser by Jenda

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (7)
    As of 2014-09-18 10:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (110 votes), past polls