Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

In order to pass arguments to a handler you need to use a closure:

parse_xml( $xmlfile); sub parse_xml { my( $file)= @_; my $ups_able_info= []; my $twig1 = XML::Twig->new( twig_handlers => { 'ups:TABLE_INFO/ups:field' => sub { parse +_table_info( @_, $ups_able_info); } ) ->parsefile( $file); # now $ups_table_info is filled } sub parse_table_info { my( $twig, $table_info, $ups_able_info)= @_; # note the extra argu +ment my $table_column = {}; $table_column->{$table_info->field('ups:tag')} = $table_info->fie +ld('ups:ui_name'); push(@{$ups_table_info}, $table_column); }

A good introduction to closures in Perl: Achieving closure by Simon Cozens

Also, I made a few cosmetic changes to your code, mostly to make it look a bit more modern:

  • no need for the & before a function name,
  • you mix camel case (parseXml) and underscore separated (parse_table_info) names, pick one scheme and stick to it (also, I hate camelCase ;--)
  • to me my $ups_able_info= []; is a lot more readable than my( $ups_able_info)= ([]);
  • use the XML::Twig->new syntax instead of the old, and potentially dangerous, new XML::Twig
  • I added the call to parsefile, to actually parse the file,
  • I usually write $elt->field( 'foo') instead of the longer $elt->first_child_text( 'foo') (even if it takes me longer to type because I usually type filed first, then go back and fix it ;--(

Also, you data structure seems a bit weird to me, but maybe it makes sense, I don't have enough detail to tell. $ups_table_info will end up looking something like [ { ui_tag => "val" }, {ui_another_tag => "val2"}, ...],, which doesn't look like the easiest to use data.


In reply to Re: Passing parameters for XML::twig handler by mirod
in thread Passing parameters for XML::twig handler by shree

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-16 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found