# 標量
my $var = "wangchao";
my $var_ref = \$var;
print ${$var_ref} . "\n"; # wangchao
${$var_ref} = "zhansan";
print ${$var_ref} . "\n"; # zhansan
print $var . "\n"; # zhansan
# 列表
my @list;
push @list, "wangchao";
push @list, "mahan";
print "@list" . "\n"; # wangchao mahan
my $list_ref = \@list;
print "@{$list_ref}" . "\n"; # wangchao mahan
push @{$list_ref}, "zhanglong";
print "@list" . "\n"; # wangchao mahan zhanglong
print "@{$list_ref}" . "\n"; # wangchao mahan zhanglong
# 哈希
my %hash;
$hash{1} = "wangchao";
$hash{2} = "mahan";
# 以下輸出wangchaomahan
foreach (keys %hash) {
print $hash{$_};
}
print "\n";
my $hash_ref = \%hash;
$hash_ref->{3} = "zhanglong";
# 以下輸出wangchaozhanglongmahan
foreach (keys %hash) {
print $hash{$_};
}
print "\n";
# 以下輸出wangchaozhanglongmahan
foreach (keys %{$hash_ref}) {
print $hash_ref->{$_};
}
print "\n";
# 標量、列表、哈希引用作為參數
sub foo
{
$foo_var_ref = $_[0];
$foo_list_ref = $_[1];
$foo_hash_ref = $_[2];
print ${$foo_var_ref} . "\n"; # zhansan
print "@{$foo_list_ref}" . "\n"; # wangchao mahan zhanglong
# wangchaozhanglongmahan
foreach (keys %{$foo_hash_ref}) {
print $foo_hash_ref->{$_};
}
}
# 調用函數
&foo(\$var, \@list, \%hash);
阅读(1574) | 评论(1) | 转发(0) |