|
拿到一个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 |
|