我剛剛設定了一個新的開發環境并從 Centos 7 (perl 5.16 v16) 轉到 RHEL 8.5 (perl 5.26.3)。話雖如此,我無法弄清楚這個舊子中定義陳述句的新語法。最后一行的任何幫助將不勝感激。我只是無法讓它作業:
my $flags_loop = db()->retrieveSet(table => "flags",order=>["flag"],dir=>'DESC');
my $outflags;
foreach my $flag (@$flags_loop) {
my $flagsize = db()->count(
table => "songflag",
where => { flagid => $flag->{flagid} } );
$flag->{flagsize} = $flagsize;
push (@$outflags, $flag);
}
# in case no flags yet defined
# 5.26 incompatible
if (!defined(@$outflags)) { $outflags=[]; }
uj5u.com熱心網友回復:
非標量的defined東西從來沒有像任何人的想法那樣作業過,或者至少人們 DWIM-ed 的含義與defined最初的意圖不同。
definedon arrays(和其他一些東西)在 Perl 5.005 中已被棄用,但與許多棄用一樣,它實際上并沒有消失。最終,v5.16(您之前使用的版本)認真對待它(不,這次是真的!)并添加了一個棄用警告,即使沒有啟用警告也會得到。警告告訴您要執行以下操作:
$ perl5.16.3 -e 'defined(@ARGV)'
defined(@array) is deprecated at -e line 1.
(Maybe you should just omit the defined()?)
沒有新語法:只是你根本不使用的舊語法。Perl 現在在多個版本的棄用警告方面做得非常好,因為開發人員非常認真地洗掉了幾十年來已經列出的所有要洗掉的東西。
Usingdiagnostics給出了錯誤的更長解釋,向您展示了如果要檢查陣列是否為空該怎么辦。
defined(@array) is deprecated at -e line 1 (#1)
(D deprecated) defined() is not usually useful on arrays because it
checks for an undefined scalar value. If you want to see if the
array is empty, just use if (@array) { # not empty } for example.
(Maybe you should just omit the defined()?)
即便如此,這個特性在 Perl 中直到 v5.22。警告告訴您該怎么做:
$ perl5.32.0 -e 'defined(@ARGV)'
Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at -e line 1.
uj5u.com熱心網友回復:
我收到類似于以下內容的錯誤訊息:
Can't use 'defined(@array)' (Maybe you should just omit the defined()?)
當我改變時:
if (!defined(@$outflags)) { $outflags=[]; }
到:
if (!(@$outflags)) { $outflags=[]; }
錯誤消失。
另見:定義
uj5u.com熱心網友回復:
洗掉該行并替換
my $outflags;
和
my $outflags = [ ];
解釋如下。
檢查是否定義了陣列是沒有意義的。
通常,當有人使用該構造時,他們想檢查陣列是否為空。(這不是它的作用。)如果是這種情況,你會替換
if (!defined(@$outflags)) { ... }
和
if (!@$outflags)) { ... }
錯誤訊息甚至表明可能是這種情況。但事實并非如此。@$outflags永遠不會為空,因此您不會嘗試檢查它是否為空。
您實際上是在嘗試確保它$outflags始終是對陣列的參考。如果沒有該行,則$outflags如果undef沒有找到標志。
因此,具體來說,您正在嘗試檢查 if $outflagsis undef。因此,您應該更換
if (!defined(@$outflags)) { ... }
和
if (!defined($outflags)) { ... }
也就是說,有一種更簡單的方法。由于$outflags要么 是參考 要么undef,我們也可以檢查真實性而不是定義性。這意味著
if (!defined($outflags)) { $outflags=[]; }
可以替換為
if (!$outflags) { $outflags=[]; }
但我們可以簡單地使用
$outflags //= [];
But that's not the simplest approach! We could avoid that line entirely by initializing $outflags from the start!
my $outflags = [];
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425554.html
標籤:perl
上一篇:將hashref轉換為kv對陣列
