免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1022 | 回复: 0
打印 上一主题 下一主题

求指导,这个pl文件大致描述了什么概念? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-15 11:29 |只看该作者 |倒序浏览
#!/usr/bin/perl

use integer;

$0 =~ s{.*[/\\]}{};
my $Usage = "$0 [-c charset] [-f regexp] -e element [name=value [name=value]] [-e element ...] files\n"
          . "    -c The charset of the input doctument.\n"
          . "    -f Define the field separator.\n"
          . "    -e Define the output xml element.\n"
          . "    The name=value is the attributes of the element.\n";

my $F = "";
my $charset = "UTF-8";
my @element;

while (defined($ARGV[0]))
{
    if ($ARGV[0] eq '--')
    {
        shift;
        last;
    }
    elsif ($ARGV[0] =~ m/^-[cC](\S*)/)
    {
        shift;
        $charset = $1 || shift;
    }
    elsif ($ARGV[0] =~ m/^-[fF](.*)/)
    {
        shift;
        $F = $1 || shift;
    }
    elsif ($ARGV[0] =~ m/^-[eE]\s*(\S*)/)
    {
        shift;
        my $elem = $1 || shift;
        die $Usage unless $elem;
        push @element, [$elem];
    }
    elsif ($ARGV[0] =~ m/^(\S+)\s*=\s*['"]?(.*)["']?$/)
    {
        shift;
        die $Usage unless @element;
        push @{$element[$#element]}, [$1, xmlEscape($2)];
    }
    elsif ($ARGV[0] =~ m/^-/)
    {
        die $Usage;
    }
    else
    {
        last;
    }
}

die $Usage unless $element[0];
print "<?xml version=\"1.0\" encoding=\"$charset\"?>\n";

for (my $i=0; $i<@element; $i++)
{
    print "<$element[$i][0]";
    for (my $j=1; $j<@{$element[$i]}; $j++)
    {
        print " $element[$i][$j][0]=\"$element[$i][$j][1]\"";
    }
    print ">\n";
}

if (length($F))
{
    while (<>)
    {
        chomp;
        next if /^\s*$/;
        split /$F/o, $_, -1;

        my $priority = m/[:\s](error|warning)[:\s]/i ? " priority=\"\L$1\"" : "";
        print "<message$priority>\n";
        for (my $i=0; $i<@_; $i++)
        {
            print "<F$i>", xmlEscape($_[$i]), "</F$i>\n";
        }
        print "</message>\n";
    }
}
else
{
    while (<>)
    {
        chomp;
        next if /^\s*$/;
        my $priority = m/[:\s](error|warning)[:\s]/i ? " priority=\"\L$1\"" : "";
        print "<message$priority>", xmlEscape($_), "</message>\n";
    }
}

for (my $i=$#element; $i>=0; $i--)
{
    print "</$element[$i][0]>\n";
}


sub xmlEscape
{
    my $str = shift;
    return $str unless $str;

    $str =~ s/&/&amp;/g;
    $str =~ s/'/&apos;/g;
    $str =~ s/"/&quot;/g;
    $str =~ s/</&lt;/g;
    $str =~ s/>/&gt;/g;
    $str =~ s/([\0-\037\177])/sprintf "&#%d;", ord($1)/eg;
    $str =~ s/^(\s+)/" " x length($1)/e;
    return $str;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP