变量在 perl 中包含 undef 值

Variable contain undef value in perl

我的文件包含:

AMQ8409: Display Queue details.
   QUEUE(SYSTEM.DATATYPE.REPLY.QUEUE)      TYPE(QLOCAL)
   ACCTQ(QMGR)                             ALTDATE(2016-08-01)
   ALTTIME(18.33.20)                       BOQNAME( )

AMQ8409: Display Queue details.
   QUEUE(SYSTEM.DATATYPE.REQUEST.QUEUE)    TYPE(QLOCAL)
   ACCTQ(QMGR)                             ALTDATE(2016-08-01)
   ALTTIME(18.32.37)                       BOQNAME( )

AMQ8409: Display Queue details.
   QUEUE(SYSTEM.MESSAGE.ASSOCIATION.QUEUE)
   TYPE(QLOCAL)                            ACCTQ(QMGR)
   ALTDATE(2016-08-01)                     ALTTIME(18.32.37)

AMQ8409: Display Queue details.
   QUEUE(SYSTEM.PENDING.DATATYPE.REPLY)        TYPE(QLOCAL)
   ACCTQ(QMGR)                             ALTDATE(2016-08-01)
   ALTTIME(18.33.20)                       BOQNAME( )

我想从QUEUE选项中提取数据,假设输出如下,

SYSTEM.DATATYPE.REPLY.QUEUE 
SYSTEM.DATATYPE.REQUEST.QUEUE 
SYSTEM.MESSAGE.ASSOCIATION.QUEUE
SYSTEM.PENDING.DATATYPE.REPLY

我写了一个代码来获得这个输出,

use warnings;
use strict;

open (CHS_OUT, "cat test.txt|");
while (<CHS_OUT>) {

        my ($QName) = /QUEUE\(([^()]+)\)/;
        chomp($QName);

        print "$QName \n";

}

但是,收到以下警告,

Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 1. SYSTEM.DATATYPE.REPLY.QUEUE Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 3. Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 4. Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 5. Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 6. SYSTEM.DATATYPE.REQUEST.QUEUE Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 8. Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 9. Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 10. Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 11.
SYSTEM.MESSAGE.ASSOCIATION.QUEUE Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 13. Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 14. Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 15. Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 16. SYSTEM.PENDING.DATATYPE.REPLY Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 18. Use of uninitialized value $QName in concatenation (.) or string at test_str.pl line 19, line 19.

如果我在我的代码中删除use strictuse warnings,它会成功,但是在该值下面有多个白色space,输出如下,

SYSTEM.DATATYPE.REPLY.QUEUE 




SYSTEM.DATATYPE.REQUEST.QUEUE 




SYSTEM.MESSAGE.ASSOCIATION.QUEUE 




SYSTEM.PENDING.DATATYPE.REPLY 

这个输出不是我想要的,因为它在值下有多个 white-space 行,这个 white-space perl 假定为 undef 值。

我想输出如下,每行没有白色space,

SYSTEM.DATATYPE.REPLY.QUEUE 
SYSTEM.DATATYPE.REQUEST.QUEUE 
SYSTEM.MESSAGE.ASSOCIATION.QUEUE
SYSTEM.PENDING.DATATYPE.REPLY

有没有可能我可以使用 next 让 perl 忽略处理 undef 值?

对于不包含 "Queue(" 的每一行,您都会收到此未定义的消息。 所以你必须检查它:

while (<CHS_OUT>) {
        my ($QName) = /QUEUE\(([^()]+)\)/;
        if ($QName){ 
           print "$QName \n";
        }
}