use Syntax::Highlight::Perl; my $formatter = new Syntax::Highlight::Perl; print $formatter->format_string($my_string);不想仔细看完全文,最主要是我懒,看英文头痛得很。
Perl.css" type=text/css rel=stylesheet>highlightPerl.css - 此CSS文档可随意更改。
.vs { color:#080; }
.va { color:#f70; }
.vh { color:#80f; }
.vt { color:#f03; }
.sub { color:#980; }
.qr { color:#ff8000; }
.str { color:#000; }
.cm { color:#008080;font-style:italic; }
.cmp { color:#014;font-family: garamond,serif;font-size:11pt; }
.bw { color:#3A3; }
.pk { color:#900; }
.nb { color:#f0f; }
.op { color:#000; }
.sym { color:#000; }
.kw { color:#00f; }
.bo { color:#f00; }
.bf { color:#001; }
.char { color:#800; }
.dr { color:#399;font-style:italic; }
.lb { color:#939;font-style:italic; }
.ln { color:#000; }
highlight.pl #!/usr/bin/Perl -T use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; my $cgi = new CGI; print $cgi->header; use Syntax::Highlight::Perl; my $color_table = { 'Variable_Scalar' => 'vs', 'Variable_Array' => 'va', 'Variable_Hash' => 'vh', 'Variable_Typeglob' => 'vt', 'Subroutine' => 'sub', 'Quote' => 'qr', 'String' => 'str', 'Comment_Normal' => 'cm', 'Comment_POD' => 'cmp', 'Bareword' => 'bw', 'Package' => 'pk', 'Number' => 'nb', 'Operator' => 'op', 'Symbol' => 'sym', 'Keyword' => 'kw', 'Builtin_Operator' => 'bo', 'Builtin_Function' => 'bf', 'Character' => 'char', 'Directive' => 'dr', 'Label' => 'lb', 'Line' => 'ln', }; my $formatter = Syntax::Highlight::Perl->new(); $formatter->define_substitution('<' => '<', '>' => '>', '&' => '&'); # HTML escapes. # install the formats set up above while ( my ( $type, $class ) = each %{$color_table} ) { $formatter->set_format($type, [ qq~<span class=\"$class\">~, '</span>' ] ); } print qq~<link rel="stylesheet" href="highlightPerl.css" type="text/css" />~; print '<pre>'; while (<DATA>) { print $formatter->format_string; } print "</pre>";





