0

Sorry to disturb you with such a silly question, I'm new at Perl.

I'm trying to modify parsing subroutine, written by my colleague and have problems with functions in perl.
It returns empty value, I don't understand why? Have already read reference sites, seen examples and they are obvious. Here's the code of function:

sub parseHTML
{
    my ($node, $depth) = @_;
    my $str = ' ';
    if (ref $node) 
    {
        if ($node->tag () ne "script" && $node->tag () ne "style")
        {
            my @children = $node->content_list ();
            for my $child_node (@children) 
            {
                parseHTML ($child_node, $depth + 1);
            }
        }
    }

    else 
    {
        $str = $str.$node."\n";
        #print $str;
    }
    return $str;
}

And then I try to use it:

my $parser = HTML::TreeBuilder->new ();
$parser->parse ($cont);
my $Parsed = parseHTML ($parser, 0);
print "$Parsed\n";
#parseHTML ($parser, 0);

The return value is empty. However, if I decide to print data right in function, uncomment string:
print $str; and use parseHTML ($parser, 0); instead, it works, and there's an output.

Where could be the mistake? Data in function seems to be local.
Here's the complete code listing as well.

7
  • $str is not changed in ref $node == true section. Commented Aug 13, 2014 at 12:32
  • You never set the value of $str in the true section of your if statement. Commented Aug 13, 2014 at 12:32
  • Are you sure you don't want just $node->as_text? Commented Aug 13, 2014 at 12:38
  • Thanks, everybody, now I understand Commented Aug 13, 2014 at 15:11
  • @Borodin, I didn't know it's possible... :) So, everything is simplier?) Commented Aug 13, 2014 at 15:13

2 Answers 2

1

You have to concat the $str returning from parseHTML

$str .= parseHTML ($child_node, $depth + 1);

or you can use a pointer this way:

...
my $Parsed;
parseHTML ($parser, 0,\$Parsed);
....

sub parseHTML
{
    my ($node, $depth, $out) = @_;
    my $str = ' ';
    if (ref $node)
    {
        if ($node->tag() ne "script" && $node->tag() ne "style")
        {
            my @children = $node->content_list ();
            for my $child_node (@children)
            {
                parseHTML ($child_node, $depth + 1,$out);
            }
         }
    }

    else
    {
        $$out .= $node."\n";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You forgot to add to $str in the "then" part of the if.

parseHTML ($child_node, $depth + 1);

should be

$str .= parseHTML ($child_node, $depth + 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.