Perl 函式引用

2021-06-27 16:28:07 字數 867 閱讀 3863

函式的引用:       $ref=\&func;   (func的定義在其他位置),不要(),當\&func()時為執行函式,返回值再引用。

當\&func()時為執行函式,返回值再引用。

[oracle@jhoa 3]$ cat 3.pl

sub generate_greeting ; }

##引用

$rs = generate_greeting();

print "\$rs is $rs\n";

##使用函式

&$rs;

[oracle@jhoa 3]$ perl 3.pl

$rs is code(0x61c52a0)

hello worldgoodbye[oracle@jhoa 3]$

引用的使用;

簡單變數:$$ref $

陣列:@$ref @; 元素:$$ref[0] $ref->[0];

雜湊:%$ref %; 元素:$$ref $ref->;

函式:&$ref(a,b); $ref->(a,b)

檔案:$ref

函式的引用: $ref=\&func; (func的定義在其他位置),不要(),當\&func()時為執行函式,返回值再引用。

[oracle@jhoa 3]$ cat 3.pl

sub generate_greeting ; } $rs = generate_greeting();

&$rs();

&$rs;

[oracle@jhoa 3]$ perl 3.pl

hello worldhello world[oracle@jhoa 3]$

perl陣列硬引用 perl中的引用

為什麼使用引用?在perl4中,hash表中的value欄位只能是scalar,而不能是list,這對於有些情況是很不方便的,比如有下面的資料 chicago,usa frankfurt,germany berlin,germany washington,usa helsinki,finland n...

perl 引用備忘

ref是乙個匿名陣列引用,同時巢狀著匿名陣列引用,匿名雜湊引用 my ref 1,2,3,a b c 訪問其中的乙個元素1 printf d n ref 0 printf d n ref 0 printf d n 0 訪問其中的乙個巢狀的匿名陣列引用的值a printf s n ref 3 0 pr...

PERL 引用與解引用

perl 語言中的引用類似於c c 中的指標。標量 scalar 列表 list 雜湊 hash 函式 sub 檔案控制代碼 handle 都可以使用引用來標記。引用本身又是乙個標量。scal ref scal 標量引用 arr ref arr 列表引用 hash ref hashe 雜湊引用 ha...