Perl只打印一行,但打印到屏幕上

时间:2013-06-29 22:33:25

标签: perl perl-module mod-perl perl-data-structures

我正在尝试打印符合我特定条件的设备列表。当我将所有内容打印到屏幕上时,效果很好。但是,当我将其打印到文件时,它只打印一行。我是perl的新手,所以任何帮助都将不胜感激。感谢

$dbConnection = &openConnection();
# run the "list_device" command via the initial connection
my $device_list = $dbConnection->list_device();
foreach my $listDevices ($device_list->result()) {
    if (   ($listDevices->model !~ /PIX/)
        && ($listDevices->model !~ /ASA/)
        && ($listDevices->model !~ /ACE/)
        && ($listDevices->driverName !~ /Context/)
        && ($listDevices->hostName =~ /^ls1.*/i)
        && ($listDevices->vendor =~ /Cisco/)
    ) {
        #create device hash for LS
        $deviceHash{"deviceID"}         = $listDevices->deviceID;
        $deviceHash{"deviceType"}       = $listDevices->deviceType;
        $deviceHash{"vendor"}           = $listDevices->vendor;
        $deviceHash{"model"}            = $listDevices->model;
        $deviceHash{"primaryIPAddress"} = $listDevices > primaryIPAddress;
        $deviceHash{"hostName"}         = $listDevices->hostName;

        # mapping array
        my @returnData = (
            "deviceID",         "hostName",
            "primaryIPAddress", "deviceType",
            "vendor",           "model"
        );
        open OVERWRITE, ">overwrite.txt" or die $!            
        # loop through the hash and print out the device information
        foreach my $data (@returnData) {
            my $returnDataLength = @returnData;

            if (exists $deviceHash{$data}) {
                                print OVERWRITE $deviceHash{$data} . ",";
                                }
        }
        close OVERWRITE;
    }
    &closeConnection($dbConnection);
}

1 个答案:

答案 0 :(得分:6)

您正在打开overwrite.txt,每$data个项目写一次。它每次都被覆盖。将其移到循环外部。