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?".)
-
You don't declare "$d".
("use strict;" would have picked this up — see strict)
-
You don't assign a value to "$d".
("use warnings;" would have picked this up — see warnings)
-
You haven't shown any input: I have no idea what data you're processing.
-
You haven't shown your output: "everything works fine" (and similar prose) is not particularly helpful.
-
Don't use prototypes unless you really need them and know exactly what you're doing (see perlsub - Prototypes).
-
Don't use Indirect Object Syntax (see perlobj - Invoking Class Methods).
-
You create a new $parser each time getValue() is called: this is unnecessary.
-
You don't capture the return value of $parser->parsefile() (see XML::Parser - METHODS).
-
You don't read the arguments passed to your handlers (see XML::Parser - HANDLERS).
-
You define your handlers inside the getValue subroutine. I don't know what you were trying to achieve by doing this.
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'