如何在子例程中打印被调用的子例程

时间:2012-12-05 02:06:56

标签: perl subroutine output

我有这个perl脚本很好地工作,输出显示在控制台中。但是,我的想法是重定向到一个文件....到目前为止,我能够完成一半的任务。我可以PRINT FILE除了在“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();

3 个答案:

答案 0 :(得分:0)

使用select

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

答案 1 :(得分: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;
}

答案 2 :(得分:-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";
}