0

嗨所以我创建了以下脚本用于输出到 wiki 页面。问题是我不确定如何格式化我的输出。这是我的代码:

    #!/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;
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(mysubinput())
{

    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
{
    &mysubinput;
    print "Go back to [[MD3000i-Perf]] \n";
    print "{| border=1 cellspacing=5 cellpadding=2 style=text-align: center \n";
    print "|-\n";
    print "! [[DataStore-1]]\n";
    print "! [[DataStore-2]]\n";
    print "! [[DataStore-3]]\n";
    print "! [[DataStore-4]]\n";
    print "! [[DataStore-5]]\n";
    print "! [[DataStore-6]]\n";
    print "! [[DataStore-7]]\n";
    print "! [[DataStore-8]]\n";
    print "! [[DataStore-9]]\n";
    print "|-\n";
    print "| [[File:disk.png |60px]]\n";
    print "| [[File:disk.png |60px]]\n";
    print "| [[File:disk.png |60px]]\n";
    print "| [[File:disk.png |60px]]\n";
    print "| [[File:disk.png |60px]]\n";
    print "| [[File:disk.png |60px]]\n";
    print "| [[File:disk.png |60px]]\n";
    print "| [[File:disk.png |60px]]\n";
    print "| [[File:disk.png |60px]]\n";
    print "|}\n";
    print "{| border=1 cellspacing=5 cellpadding=2 style=text-align: center\n";
    print "|-\n";
    &writedate;
    print " |-\n";
    &writesections;
    print " |-}\n";
}
#&mysubinput;
&writepage;
#&mysubinput;
#&writesections;

使用这种方法,事情似乎奏效了。但是,我的输入不是 100% 正确的。我必须输入所有数据两次,然后程序才能吐出一些东西......

Enter the number for the datastore:
1
Enter the name for the datastore e.g: DataStore or ServerStore:
DataStore
Enter the data store image prefix e.g. datastore or serverstore:
datastore
Enter the month MM:
12
Enter the day DD:
03
Enter the Year YYYY:
2012
Enter the number for the datastore:
1
Enter the name for the datastore e.g: DataStore or ServerStore:
DataStore
Enter the data store image prefix e.g. datastore or serverstore:
datastore
Enter the month MM:
12
Enter the day DD:
03
Enter the Year YYYY:
2012
Go back to [[MD3000i-Perf]]
{| border=1 cellspacing=5 cellpadding=2 style=text-align: center
|-
! [[DataStore-1]]
! [[DataStore-2]]
! [[DataStore-3]]
! [[DataStore-4]]
! [[DataStore-5]]
! [[DataStore-6]]
! [[DataStore-7]]
! [[DataStore-8]]
! [[DataStore-9]]
|-
| [[File:disk.png |60px]]
| [[File:disk.png |60px]]
| [[File:disk.png |60px]]
| [[File:disk.png |60px]]
| [[File:disk.png |60px]]
| [[File:disk.png |60px]]
| [[File:disk.png |60px]]
| [[File:disk.png |60px]]
| [[File:disk.png |60px]]
|}
{| border=1 cellspacing=5 cellpadding=2 style=text-align: center
|-
! 12/04/12
        |-
| [[File:DataStore-1-Tios-12-03-2012.png| 100px]]
| [[File:DataStore-1-readp-12-03-2012.png| 100px]]
| [[File:DataStore-1-cachep-12-03-2012.png| 100px]]
| [[File:DataStore-1-ckbsec-12-03-2012.png| 100px]]
| [[File:DataStore-1-mkbsec-12-03-2012.png| 100px]]
| [[File:DataStore-1-ciosec-12-03-2012.png| 100px]]
| [[File:DataStore-1-miosec-12-03-2012.png| 100px]]
        |-}

将此变量添加到“我的”后,现在似乎一切正常

my ($STORENUM, $STORENAME, $IMGPREFIX, $MONTH, $DAY, $YEAR) = &mysubinput;

第二部分是添加 return ($STORENUM, $STORENAME, $IMGPREFIX, $MONTH, $DAY, $YEAR);

现在我的问题是如何将子写入部分的输出放入文件中,例如 datastore.wk?

4

2 回答 2

2

如果您已启用use strictuse warnings启用,您会看到如下消息

Global symbol "$STORENAME" requires explicit package name

可以帮助您调试程序。

问题是您在其中声明的变量mysubinput是该子例程的本地变量,并且在子例程返回时将被丢弃。就writesections这些变量而言,这些变量从未被声明过,程序也无法编译。

取消注释这些use语句,并在使用它们的第一个子例程之前添加 、 、 、 和 的声明,并从子例程中的$DAY这些$MONTH变量$STORENAME$STORENUM删除。然后它们将可以在文件的其余部分中访问,并且您的程序应该可以工作。$YEARmy

顺便说一句,调用子程序&mysubinput确实是一种非常古老的风格。你应该mysubinput()自始至终使用。

于 2012-12-04T19:31:40.387 回答
1

永远不要评论这一use strict行:)

它会提醒您,您的writesectionssub 无权访问在mysubinput!范围内声明的变量。

我建议你先了解私有变量。要共享您的变量,您应该共享return它们。按照你的逻辑:

sub mysubinput
{
    ...
    return ($STORENUM, $STORENAME, $IMGPREFIX, $MONTH, $DAY, $YEAR);
}

my ($STORENUM, $STORENAME, $IMGPREFIX, $MONTH, $DAY, $YEAR) = mysubinput();
writesections($STORENAME, $STORENUM, $MONTH, $DAY, $YEAR);

但是,如果您希望扩展您的 Perl 脚本,那么迟早您将不得不学习面向对象编程。那么,Moose 框架就是你的朋友。

于 2012-12-04T19:34:26.223 回答