动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   Cgi/Perl → 阅读文章

 如何将Perl代码着色

作者来源: 
阅读 2131 人次 , 2006-2-16 14:43:00 

先决条件

cpan Syntax::Highlight::Perl

代码解释

perldoc Syntax::Highlight::Perl
perldoc里或许解释清楚了,但是蛮长了,我没耐心看。使用里面的代码一点反应都没有。
use Syntax::Highlight::Perl;

my $formatter = new Syntax::Highlight::Perl;
print $formatter->format_string($my_string);
不想仔细看完全文,最主要是我懒,看英文头痛得很。
还好可以Search, 找到了Coloring perl code in HTML
对着那代码,改写了些许颜色。将它span里改写成使用class而不是style.这样比较容易知道哪些词是属于哪一部分。
完成后的代码如下。现炒现卖,直接将它着色。
打算迟些时候将它做为Eplanet的新功能。

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>";
  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:如何用Net::SMTP发送邮件
· 下一篇:如何安装FastCGI
· 获取随机的密码
· CGI教程(9)编写安全的CGI脚本
· CGI教程(3)
· 利用Perl开发Internet/Intranet应用之二(5)
· Perl教学 第八篇 子程序


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077