http://www.perlmonks.org?node_id=901332

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

I have a Perl program to parse the xml file, Here I have declared the final arrayref $ups_able_info as global. Instead of this I want to make it local to the sub parsexml() and pass the same to the twig handle 'parse_table_info'. If I try to pass the arryref to the twig handle getting the ERROR: 'Global symbol "$ups_table_info" requires explicit package'. Can any one help me on this
my ($ups_able_info) = ([]); &parseXml($xmlfile); sub parseXml{ my $twig1 = new XML::Twig( twig_handlers => { 'ups:TABLE_INFO/ups:fiel +d' => \&parse_table_info} ); } ### twig handler to parse columns. sub parse_table_info { my( $twig, $table_info)= @_; my $table_column = {}; $table_column->{$table_info->first_child_text('ups:tag')} = $ +table_info->first_child_text('ups:ui_name'); push(@{$ups_table_info}, $table_column); }

Replies are listed 'Best First'.
Re: Passing parameters for XML::twig handler
by toolic (Bishop) on Apr 26, 2011 at 12:33 UTC
    my ($ups_able_info) = ([]); ... push(@{$ups_table_info}, $table_column);
    You made a typo. You declared a variable called $ups_able_info with my, but then you try to use a variable of a different name, $ups_table_info. You probably want $ups_able_info to be $ups_table_info.
Re: Passing parameters for XML::twig handler
by mirod (Canon) on Apr 27, 2011 at 08:27 UTC

    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.

      Great illustration ! thank you !!