LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1060|回复: 0

perl求教

[复制链接]
发表于 2007-5-12 11:46:57 | 显示全部楼层 |阅读模式
拿到一个perl脚本,执行时提示出错,由于从来没有接触过,不太清楚是什么问题,望高手们帮帮忙
谢谢

程序pyro
======================================
#!/usr/bin/perl -w
my $infilename = $ARGV[0];
my $basevar = "\\@$ARGV[1]\\@";

my $pickatrandom = 1;
if (($ARGV[2]) && ($ARGV[2] eq "all"))
{
    $pickatrandom = 0;
}

# Seed the output file
my $outfilename = "/tmp/pyro.txt";
open(OUTFILE, "> $outfilename")
    or die "Couldn't open $outfilename for writing: $!\\n";
print OUTFILE "$basevar\\n";
close(OUTFILE);

local $/;
undef $/;

open(INFILE, "< $infilename")
    or die "Couldn't open $infilename for reading: $!\\n";
$infilecontents = <INFILE>;
close(INFILE);

open(OUTFILE, "< $outfilename")
    or die "Couldn't open $outfilename for reading: $!\\n";
$outfilecontents = <OUTFILE>;
close(OUTFILE);

while ($outfilecontents =~ /\\@[A-Za-z0-9]+\\@/)
{
    local $/;
    undef $/;

    open(OUTFILE, "< $outfilename")
        or die "Couldn't open $outfilename for reading: $!\\n";
    $outfilecontents = <OUTFILE>;
    close(OUTFILE);

    # $baseline is the first line in OUTFILE with a variable.

    if ($outfilecontents =~ /^(.*?)(\\@\\w+\\@)(.*?)$/m)
    {
        $baseline = "$1$2$3";
        $varname=$2;
    }
    chomp $varname;

    if ($infilecontents =~ /\\#$varname\\n(.*?)\\n\\n/s)
    {
        $varblock = "$1\\n";
    }
    else
    {
        die "Did not find variable $varname in $infilename.\\n";
    }

    @varblockarr = split (/\\n/, $varblock);
    @outlinesarr = ();

    if ($pickatrandom)
    {
        # Generate a random string from the input elements
        $randline = $varblockarr [ rand @varblockarr ];
        $curline = $baseline;
        chomp ($curline);
        chomp ($randline);
        $curline =~ s/$varname/$randline/;
        push (@outlinesarr, $curline);
    }
    else
    {
        # Generate all possible combinations of the input elements
        foreach $varline (@varblockarr)
        {
            $curline = $baseline;
            chomp ($curline);
            chomp ($varline);
            $curline =~ s/$varname/$varline/;
            push (@outlinesarr, $curline);
        }
    }

    $outlines = join ("\\n", @outlinesarr);
    $outfilecontents =~ s/\\Q$baseline\\E/$outlines/s
        or die "baseline not found.\\n";
    $outfilecontents =~ s/\\n\\n/\\n/mg;
    open(OUTFILE, "> $outfilename")
        or die "Couldn't open $outfilename for writing: $!\\n";
    print OUTFILE $outfilecontents;
    close(OUTFILE);
}

if ($pickatrandom)
{
    print $outfilecontents;
}
======================================

utopia.dat
======================================
#@place@
@type@ made of @material@ @location@, inhabited by @inhabitants@, @govt@, @special@

#@type@
a cavern
a city
a country
a forest
a jungle
a mountain
a planet
a sealed habitat
a village
an island

#@material@
a superstrong material
crystal
flesh
gold
======================================

$ ./pyro utopia.dat place
提示错误
Use of uninitialized value in join or string at ./pyro line 3.
\\@\n

如果正常运行的话,应该是这样
$ ./pyro utopia.dat place
a mountain made of rubber in the sky, inhabited by fish, ruled by a hereditary monarch,
where learning is exalted
$ ./pyro utopia.dat place
an island made of crystal in the fourth dimension, inhabited by aliens, an anarchy, which is microscopic
$ ./pyro utopia.dat place
a sealed habitat made of paper in the dream world, inhabited by giants, a
socialist utopia, where learning is exalted
$ ./pyro utopia.dat place
an island made of flesh in the fourth dimension, inhabited by fairies, an anarchy,
which is boundless
$ ./pyro utopia.dat place
a mountain made of a superstrong material in the dream world, inhabited by apes, an
anarchy, where cannibalism is practiced
$ ./pyro utopia.dat place
a cavern made of porcelain in the polar regions, inhabited by intelligent plants,
ruled by a council of many species, which is microscopic
$ ./pyro utopia.dat place
a sealed habitat made of stone in an unknown place, inhabited by giants, ruled by a
magical elite, where everyone is insane
$ ./pyro utopia.dat place
a sealed habitat made of flesh in the desert, inhabited by giants,
ruled by a corporation, where cannibalism is practiced
$ ./pyro utopia.dat place
an island made of paper under the sea, inhabited by fairies, an anarchy, which is
boundless
$ ./pyro utopia.dat place
a forest made of flesh in an unknown place, inhabited by fairies, ruled by computer,
exceedingly warlike
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表