0

我有这个 perl 脚本运行良好,输出显示在控制台中。但是,我的想法是重定向到一个文件....到目前为止,我能够完成一半的任务。除了在“sub writepage”中调用的两个子例程之外,我可以打印所有内容

有任何想法吗?

#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw/strftime/;
#use warnings;
my $TOTALIOS = 'totalios';
my $READP = 'readpe';
my $CACHEP = 'cachehit';
my $CKBSEC = 'currentkb';
my $MKBSEC = 'maximumkb';
my $CIOSEC = 'currentio';
my $MIOSEC = 'maxio';
my $DATESTORE = 'dates.sec';
my $DATECMD = strftime "%m/%d/%y", localtime;
my $TIOS = 'Tios';
my $READp = 'readp';
my $CACHEp = 'cachep';
my $CKBsec = 'ckbsec';
my $MKBsec = 'mkbsec';
my $CIOsec = 'ciosec';
my $MIOsec = 'miosec';
my ($STORENUM, $STORENAME, $IMGPREFIX, $MONTH, $DAY, $YEAR) = &mysubinput;
#my $STORENUM;
#my $STORENAME;
#my $IMGPREFIX;
#my $MONTH;
#my $DAY;
#my $YEAR;
my $outfile = 'datastore.txt';
sub mysubinput
{
    print "Enter the number for the datastore: \n";
        my $STORENUM = <STDIN>;
        chomp($STORENUM);
        print "Enter the name for the datastore e.g: DataStore or ServerStore: \n";
        my $STORENAME = <STDIN>;
        chomp($STORENAME);
        print "Enter the data store image prefix e.g. datastore or serverstore: \n";
        my $IMGPREFIX = <STDIN>;
        chomp($IMGPREFIX);
        print "Enter the month MM: \n";
        my $MONTH = <STDIN>;
        chomp($MONTH);
        print "Enter the day DD: \n";
        my $DAY = <STDIN>;
        chomp($DAY);
        print "Enter the Year YYYY: \n";
        my $YEAR = <STDIN>;
        chomp($YEAR);
    return ($STORENUM, $STORENAME, $IMGPREFIX, $MONTH, $DAY, $YEAR);
#   print "$STORENUM $STORENAME $IMGPREFIX $MONTH $DAY $YEAR\n";
#   print "Not a very interesting routine\n";
#   print "This does the same thing every time\n";
#   print "$TOTALIOS\n";
#   print "Enter the number for the datastore: \n";
#   $line = <STDIN>;
#   chomp($line);
#   print "$line\n";
#   print "$DATECMD\n";
}
sub writedate 
{
    print "! $DATECMD\n";   
}    

sub writesections
{
    print "| [[File:$STORENAME-$STORENUM-$TIOS-$MONTH-$DAY-$YEAR.png| 100px]]\n";
    print "| [[File:$STORENAME-$STORENUM-$READp-$MONTH-$DAY-$YEAR.png| 100px]]\n";
    print "| [[File:$STORENAME-$STORENUM-$CACHEp-$MONTH-$DAY-$YEAR.png| 100px]]\n";
    print "| [[File:$STORENAME-$STORENUM-$CKBsec-$MONTH-$DAY-$YEAR.png| 100px]]\n";
    print "| [[File:$STORENAME-$STORENUM-$MKBsec-$MONTH-$DAY-$YEAR.png| 100px]]\n";
    print "| [[File:$STORENAME-$STORENUM-$CIOsec-$MONTH-$DAY-$YEAR.png| 100px]]\n";
    print "| [[File:$STORENAME-$STORENUM-$MIOsec-$MONTH-$DAY-$YEAR.png| 100px]]\n";
}   

sub writepage
{
    open (FILE, ">> $outfile") || die "problem opening $outfile\n";
    print FILE "Go back to [[MD3000i-Perf]] \n";
    print FILE "{| border=1 cellspacing=5 cellpadding=2 style=text-align: center \n";
    print FILE "|-\n";
    print FILE "! [[DataStore-1]]\n";
    print FILE "! [[DataStore-2]]\n";
    print FILE "! [[DataStore-3]]\n";
    print FILE "! [[DataStore-4]]\n";
    print FILE "! [[DataStore-5]]\n";
    print FILE "! [[DataStore-6]]\n";
    print FILE "! [[DataStore-7]]\n";
    print FILE "! [[DataStore-8]]\n";
    print FILE "! [[DataStore-9]]\n";
    print FILE "|-\n";
    print FILE "| [[File:disk.png |60px]]\n";
    print FILE "| [[File:disk.png |60px]]\n";
    print FILE "| [[File:disk.png |60px]]\n";
    print FILE "| [[File:disk.png |60px]]\n";
    print FILE "| [[File:disk.png |60px]]\n";
    print FILE "| [[File:disk.png |60px]]\n";
    print FILE "| [[File:disk.png |60px]]\n";
    print FILE "| [[File:disk.png |60px]]\n";
    print FILE "| [[File:disk.png |60px]]\n";
    print FILE "|}\n";
    print FILE "{| border=1 cellspacing=5 cellpadding=2 style=text-align: center\n";
    print FILE "|-\n";
    print FILE writedate();
    print FILE "|-\n";
    print FILE writesections();
    print FILE "|-}\n";
    close(FILE);
}

#open (FILE, "> $myfile") || die "Couldn't open $myfile: $!\n";
#print FILE writepage();
#close (FILE);

writepage();
4

3 回答 3

0

使用选择

或者从函数返回一个字符串而不是打印?

于 2012-12-05T02:43:21.697 回答
0

我刚刚将 FILE 替换为词法文件句柄:

sub writedate 
{
    my $fh = shift;
    print $fh "! $DATECMD\n";   
}    

sub writesections
{
    my $fh = shift;
    print $fh "| [[File:$STORENAME-$STORENUM-$TIOS-$MONTH-$DAY-$YEAR.png| 100px]]\n";
    print $fh "| [[File:$STORENAME-$STORENUM-$READp-$MONTH-$DAY-$YEAR.png| 100px]]\n";
    [....]
}



  sub writepage
{
#   open (FILE, ">> $outfile") || die "problem opening $outfile\n";
    open my $fh, '>', $outfile or die "Problem opening $outfile\n";
    print $fh "Go back to [[MD3000i-Perf]] \n";
    print $fh "{| border=1 cellspacing=5 cellpadding=2 style=text-align: center \n";
    print $fh "|-\n";
    [....]
    [....]
    print $fh "|}\n";
    print $fh "{| border=1 cellspacing=5 cellpadding=2 style=text-align: center\n";
    print $fh "|-\n";
    writedate($fh);
    print $fh "|-\n";
    writesections($fh);
    print $fh "|-}\n";
#   close(FILE);
    close $fh;
}
于 2012-12-05T02:49:11.267 回答
-1

您应该使用词法文件句柄。这将使这项任务变得微不足道。通常,它也是处理文件句柄的首选方式。这是一个例子:

sub mysub {
    open my $fh, '>>', $outfile or die "Problem opening $outfile\n";
    print $fh "Some string\n";
    anotherSub($fh);
    close $fh;
}

sub anotherSub {
    my $fh = shift;
    print $fh "A second string\n";
}
于 2012-12-05T02:24:38.303 回答