我正在運行一個古老版本的 Movable Type,它對我來說很好用。但是,我開始收到以下服務器錯誤:
defined(@array) is deprecated at /home/public_html/cgi-bin/mt/extlib/Locale/Maketext.pm line 623.
defined(%hash) is deprecated at /home/public_html/cgi-bin/mt/extlib/Locale/Maketext.pm line 623.
我找到了有問題的行:
if defined(%{$module . "::Lexicon"}) or defined(@{$module . "::ISA"});
以下是重構這條線的正確方法嗎?
if %{$module . "::Lexicon"} or @{$module . "::ISA"};
如果是這樣,為什么?如果沒有,為什么不呢?我想更好地了解defined(@array)和發生了什么defined(%hash)。
更新:我也在第 367 行發現了一個類似的問題CGI.pm:
if (defined(@QUERY_PARAM) && !defined($initializer)) {
我將其重寫如下,但我仍然不確定它是否正確:
if (@QUERY_PARAM && $initializer) {
我可以看到@QUERY_PARAM可以證實它的存在,但我可能不會設定第二個條件$initializer也不會存在,我不太清楚如何做到這一點。
uj5u.com熱心網友回復:
$ perl -w -Mdiagnostics -e 'print defined(@array)'
Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at -e
line 1 (#1)
(F) defined() is not 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.
Uncaught exception from user code:
Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at -e line 1.
$ [perldoc -f defined]( http://metacpan.org/pod/perlfunc#defined )
...
Use of "defined" on aggregates (hashes and arrays) is no longer
supported. It used to report whether memory for that aggregate
had ever been allocated. You should instead use a simple test
for size:
if (@an_array) { print "has array elements\n" }
if (%a_hash) { print "has hash members\n" }
該defined(@array)構造從未執行用戶期望的操作,而且它實際執行的操作并沒有那么有用,因此它已從語言中洗掉(并且在您使用的 Perl 版本中已棄用)。正如診斷和檔案所建議的那樣,修復它的方法是僅使用@array而不是defined(@array)在布爾背景關系中。
uj5u.com熱心網友回復:
正如 mob 所說,解決方案就是 do if (@array)...or if (%hash)...。
但是,對于任何好奇的人,您可以檢查符號表以查看該變數是否曾經被自動激活。然而,更多的是,(幾乎)沒有理由這樣做。它只會告訴您您是否是使用該變數的第一行代碼。這是無用的 5 個 9。
但是對于第 6 位有效數字,您可以按照以下方法進行操作。我什至知道的唯一用例是在Carp呼叫命名空間中檢查某些變數而不在程序中污染它。
這僅適用于全域的、非詞法的變數。全域未宣告use vars qw//, 和our()。詞法是my()和state()。符號表是包含包中所有全域符號名稱的特殊散列。
use Data::Dump qw/pp/;
pp \ %Z::;
{}
$Z::foo = "foo scalar";
pp \ %Z::;
do {
my $a = { foo => *Z::foo };
$a->{foo} = \"foo scalar";
$a;
}
print "symbol exists\n" if exists $Z::{foo};
symbol exists
print "symbol and array exists\n" if $Z::{foo} and defined *{$Z::{foo}}{ARRAY};
pp \ %Z::;
do {
my $a = { foo => *Z::foo };
$a->{foo} = \"foo scalar";
$a;
}
print "symbol and array does not exist\n" unless $Z::{foo} and defined *{$Z::{foo}}{ARRAY};
symbol and array does not exist
pp \ %Z::;
do {
my $a = { foo => *Z::foo };
$a->{foo} = \"foo scalar";
$a;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/362218.html
