在下面的代碼中,我得到一個uninitialized value
警告,但只在第二個given/when
示例。爲什麼是這樣?給定/當未定義值
#!/usr/bin/env perl
use warnings;
use 5.12.0;
my $aw;
given ($aw) {
when ('string') {
say "string";
}
when (not defined) {
say "aw not defined";
}
default {
say "something wrong";
}
}
given ($aw) {
when (/^\w+$/) {
say "word: $aw";
}
when (not defined) {
say "aw not defined";
}
default {
say "something wrong";
}
}
我得到的輸出是:
aw not defined
Use of uninitialized value $_ in pattern match (m//) at ./perl.pl line 20.
aw not defined
所以他應該把'定義'檢查在頂部來解決這個問題? – simbabque
@simbabque,是的,這將刪除警告。 – Qtax