From: Andrew Gaffney <mailto:[EMAIL PROTECTED]> wrote:

: I've created a module that uses HTML::Parser to parse some
: HTML and create a tree structure. Someone had suggested to
: use HTML::TreeBuilder, but my HTML contains HTML::Mason
: code embedded, and HTML::TreeBuilder doesn't handle that
: well at all. HTML::TreeBuilder also adds <body>, <head>,
: and <html> tags when there aren't any in the document it
: is parsing. The files I'm using this with are only parts
: of HTML pages, so I don't want that stuff added.
: 
: My module works well enough, but I'm getting to the point
: where I need multiple parse trees existing at the same
: time in a mod_perl environment. The way my module is now,
: they could get mixed up, because I can't find a way to
: pass a custom variable to the event handler subroutines
: of HTML::Parser. 
: 
: I've figured that if I subclass it, I can create a new
: object for each parse tree instead of just returning an
: array reference. Here is my current code: 
: 
: package SkylineEdit;
: 
: use HTML::Parser ();
: 
: @ISA = ('Exporter');
: @EXPORT = ('html_to_htmltree', 'htmltree_to_html',
: 'get_node_content',
: 'set_node_content');

    Comment out the exporter stuff and start a new module.
For lack of imagination I used SkylineEditOO.pm. This
should cache everything the way you want and requires
little editing of SkylineEdit.pm.

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper 'Dumper';

use SkylineEditOO;

my $htmltree = SkylineEditOO->new();
$htmltree->html_to_htmltree( 'foo.html' );

print Dumper $htmltree->html_to_htmltree();


__END__

package SkylineEditOO;

use strict;
use warnings;

use SkylineEdit;

sub new {
    my $class = shift;
    bless {
        html_to_htmltree => '',
        htmltree_to_html => '',
        get_node_content => '',
        set_node_content => '',
    }, $class;
}

sub html_to_htmltree {
    my $self = shift;
    $self->{html_to_htmltree} =
        SkylineEdit::html_to_htmltree( @_ ) if @_;
    return $self->{html_to_htmltree};
}

sub htmltree_to_html {
    my $self = shift;
    $self->{htmltree_to_html} =
        SkylineEdit::htmltree_to_html( @_ ) if @_;
    return $self->{html_tree_to_html};
}

sub get_node_content {
    my $self = shift;
    $self->{get_node_content} =
        SkylineEdit::get_node_content( @_ ) if @_;
    return $self->{get_node_content};
}

sub set_node_content {
    my $self = shift;
    $self->{set_node_content} =
        SkylineEdit::set_node_content( @_ ) if @_;
    return $self->{set_node_content};
}

1;



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to