差異處
這裏顯示兩個版本的差異處。
tech:perl_clear_hash [2008/12/24 13:26] – 建立 jonathan | tech:perl_clear_hash [2008/12/25 10:21] (目前版本) – jonathan | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== Perl 的 hash 變數相關實務驗證 ====== | ||
+ | 這個問題所造成的 Bug 真的會很難抓.. 所以仔細了解一下 hash 的變數與實際驗證的結果整理如下: | ||
+ | |||
+ | * 清除 hash 變數的方式 | ||
+ | <code perl> | ||
+ | # | ||
+ | |||
+ | for ($i=0; $i<10; $i++) { | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | $hash_test={}; | ||
+ | print $i.' | ||
+ | $hash_test-> | ||
+ | sleep(1); | ||
+ | print $i.' | ||
+ | } | ||
+ | </ | ||
+ | 必須要用 undef($hash_test), | ||
+ | <note warning> | ||
+ | **清除 array 的語法剛好相反** | ||
+ | * @arr_t=(); 可以正確清除掉 arr_t 的陣列內容 | ||
+ | * $arr_t=(); 無法清除掉 arr_t 的陣列內容 | ||
+ | </ | ||
+ | |||
+ | |||
+ | * hash 變數並不會因為在 sub 內的 local 宣告就回重新產生一份在 sub 內專屬的變數, | ||
+ | <code perl> | ||
+ | |||
+ | # | ||
+ | |||
+ | $hash_x-> | ||
+ | print(" | ||
+ | change_hash(' | ||
+ | print(" | ||
+ | |||
+ | sub change_hash { | ||
+ | local($p_value) = @_; | ||
+ | local(%hash_x); | ||
+ | |||
+ | $hash_x-> | ||
+ | return; | ||
+ | } | ||
+ | </ | ||
+ | ** 結果如下 ** | ||
+ | < | ||
+ | [apache@rdtest01 develop]$ perl /tmp/t4.pl | ||
+ | 1:[abc] | ||
+ | 2:[xyz] | ||
+ | </ | ||
+ | |||
+ | {{tag> |