perl scalar reference小問題

看板Perl作者 (瑤)時間5年前 (2018/05/25 15:54), 編輯推噓2(208)
留言10則, 3人參與, 5年前最新討論串1/1
請教大家一個關於scalar ref的小問題 my $project = "aaa"; . . . open(FH,<file); foreach (<FH>){ # 這行為 project : bbb my @tmp = split(":",$_); my $token = $tmp[0]; #值為 project my $value = $tmp[1]; #值為 bbb # chomp 以及空白字元處理就不打上來了 $$token = $value; } 預期 $$token這行應該等效下行 $project = $value 但實際上卻不是這樣 也分別印出 $project 與 $$token 的位址 確實不同 請問這是我對於 reference理解錯誤嗎? 謝謝幫忙! ----- Sent from JPTT on my Asus ASUS_Z01HDA. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 27.246.6.223 ※ 文章網址: https://www.ptt.cc/bbs/Perl/M.1527234840.A.C7E.html

05/25 17:04, 5年前 , 1F
把$project改成以our宣告就可以了
05/25 17:04, 1F

05/25 17:05, 5年前 , 2F
$$token那一行使用到的symbolic reference只能用來對
05/25 17:05, 2F

05/25 17:06, 5年前 , 3F
package variable起作用
05/25 17:06, 3F

05/25 17:08, 5年前 , 4F
以my宣告的變數不會出現在symbol table裡 所以symbolic ref
05/25 17:08, 4F

05/25 17:09, 5年前 , 5F
的方式找不到你要的那個變數
05/25 17:09, 5F

05/25 17:10, 5年前 , 6F
說到底還是加上 use strict; use warnings; 比較能避開
05/25 17:10, 6F

05/25 17:12, 5年前 , 7F
類似這種看似細微但是出錯時又不是能一眼看透的錯誤
05/25 17:12, 7F

05/25 17:21, 5年前 , 8F
可以參考perlref 文件, modern perl(非書名)
05/25 17:21, 8F

05/25 18:02, 5年前 , 9F
推 f 大解釋,長知識了。
05/25 18:02, 9F

05/26 06:25, 5年前 , 10F
感謝f大 謝謝 功力又更深一層了
05/26 06:25, 10F
文章代碼(AID): #1R1y4On- (Perl)
文章代碼(AID): #1R1y4On- (Perl)