Web Client Programming with Perl-Chapter 7: Graphical Examples with Perl/Tk- P2 - Pdf 76

Chapter 7: Graphical Examples with Perl/Tk- P2

The do_search( ) function will take an optional $url argument, to give it an
alternative place to connect to. Otherwise it expects $word to contain
something. We just hit Return from the entry widget, so $word contains the
string "example", and $url is undefined. If we accidentally hit Return before
typing anything, we don't want to search for a nonstring, so we return from
the subroutine if that's the case:
$INFORMATION = "Connect: $url";

$text->configure(-cursor=> 'watch');
$mw->idletasks;
We give the user some feedback by placing along the bottom of the
application a "Connect..." string, and we also change the cursor to a watch.
$mw->idletasks just tells the window to do anything it was waiting to do, so
that we can actually see the watch and information string:
my $request = new HTTP::Request('GET', $url);

my $response = $ua->request($request);
if ($response->is_error) {
$INFORMATION = "ERROR: Could not retrieve
$url";
} elsif ($response->is_success) {
my $html = parse_html($response->content);

## Clear out text item
$text->configure(-state => "normal");

$text->delete('1.0', 'end');
$html->traverse(\&display_html);
$text->configure(-state => "disabled");

$tag = $node->endtag;
}

## Gets rid of any 'extra' stuff in the tag, and
saves it
if ($tag =~ /^(<\w+)\s(.*)>/) {
$tag = "$1>";
$extra = $2;
}

if (exists $html_action{$tag}) {
$html_text =~ s/\s+/ /g;
&{ $html_action{$tag} }($tag,
$html_text);
$html_text = "";
}
}
1;
}
That's the entire function, but it does quite a bit. The $node could either be
an object or a simple text string. For the simple case, when it's just text, we
append it to any prior text (remember, we could be ignoring HTML tags,
along the way, that had text before them) and save it for future use. If $node
is an object pointer, then we have to determine what kind it is, and decide if
we care about the HTML tag it's telling us about.
HTML tags usually come in pairs, so $startflag tells us when we found the
first of a pair. We want to know what that tag was, so we call the starttag
method. Certain tags have other information associated with them (i.e., the
<A> tag), and we want to save that for future use in $extra. Remember that
we are trying to get just the plain simple tag to use in our lookup array.

to use that tag to format the text. For our headings, we set up that text tag to
be a font-changing tag:
sub paragraph {
&flush_text(@_);
$text->insert('end', "\n\n");
}
A paragraph marker, <P>, just means insert a few returns. We also have to
flush out any text prior to it:
sub line_break {
&flush_text(@_);
$text->insert('end', "\n");
}
Similar to <P>, the <BR> also just inserts a return:
sub draw_line {


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status