G'day Priyo,

Welcome to the monastery.

Your code raised a number of issues which I'll address first. My comments assume you've posted all relevant code; however, your problem description suggests otherwise. Of course, I have no way of knowing what you've left out; just bear that in mind as you read the following. (These guidelines will help you post a better question next time: "How do I post a question effectively?".)

Here's a basic script (along the lines that I think you want) which addresses these issues. Consider this a template for reworking your code.

#!/usr/bin/env perl use strict; use warnings; use XML::Parser; my $parser = XML::Parser::->new(Handlers => { Start => \&hdl_start, End => \&hdl_end, Char => \&hdl_char, Default => \&hdl_def, Final => \&hdl_final, }); my @xml_files = qw{./pm_xml_parser_test_file1.xml ./pm_xml_parser_test_file2.xml +}; for (@xml_files) { _init_handler_vars('filename'); get_value($_); } sub get_value { my $file = shift; my $returned = $parser->parsefile($file); if (defined $returned) { print "Wanted content: '$returned'\n"; } else { print "Wanted content not found!\n"; } return; } # Private scope for handler variables { my ($wanted_element, $in_wanted_element, $wanted_content, $found_wanted_content); sub _init_handler_vars { $wanted_element = shift; $in_wanted_element = 0; $wanted_content = ''; $found_wanted_content = 0; return; } sub hdl_start { my ($expat, $element, $attr, @vals) = @_; if ($element eq $wanted_element) { $in_wanted_element = 1; } return; } sub hdl_end { my ($expat, $element) = @_; if ($element eq $wanted_element) { $in_wanted_element = 0; } return; } sub hdl_char { my ($expat, $string) = @_; if ($in_wanted_element) { if ($string) { $found_wanted_content = 1; $wanted_content = $string; } } return; } sub hdl_def { my ($expat, $string) = @_; # Default handler actions here return; } sub hdl_final { my ($expat) = @_; return unless $found_wanted_content; return $wanted_content; } }

Most of what I have here should be obvious from the issues I identified (and supporting documentation) above. Note that the variables $wanted_element, $in_wanted_element, $wanted_content and $found_wanted_content are only visible to _init_handler_vars() and the handlers themselves (hdl_*()).

Here's my (minimal) test data:

$ cat ./pm_xml_parser_test_file1.xml <root_element> <filename>pm_xml_parser_test_file1.xml</filename> </root_element> $ cat ./pm_xml_parser_test_file2.xml <root_element> <filename>pm_xml_parser_test_file2.xml</filename> </root_element>

Here's the output:

$ pm_xml_parser_test.pl Wanted content: 'pm_xml_parser_test_file1.xml' Wanted content: 'pm_xml_parser_test_file2.xml'

-- Ken


In reply to Re: scope of variable by kcott
in thread scope of variable by priyo

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.