+ -
当前位置:首页 → 问答吧 → 关于一个perl编程中的哈希表打印的奇怪问题

关于一个perl编程中的哈希表打印的奇怪问题

时间:2011-04-21

来源:互联网

例如下边的程序:#!/usr/bin/perl
%hash=("A"=>0,"C"=>0,"G"=>0,"U"=>0);
print %hash;
print "%hash";
上边的程序中,第一个print语句可以打印出哈希表的键/值,第二个print语句打印的结果却是%hash,不知道其中的原因,求高人指点。
#!/usr/bin/perl
$a=1;
print $a;
print "$a";
这两个print语句都可以打印出$a的值,但是上边的哈希表的print语句却不可以加引号。
求高人指点

作者: xdp719   发布时间: 2011-04-21

只有 $ 或 @ 开头的才能内插

作者: zhlong8   发布时间: 2011-04-21

你好,本人初学perl,请问内插是什么意思

作者: xdp719   发布时间: 2011-04-21

就是这个  "$a" 双引号中会插入取变量的值

作者: zhlong8   发布时间: 2011-04-21

回复 xdp719


   

QUOTE:
Perl's Interpolation Syntax
When Perl encounters a string that can be interpolated, it scans it for three significant characters, $, @
and \. If any of these are present and not escaped (prefixed with a backslash) they trigger interpolation
of the text immediately following. What actually happens depends on the character:

Character Action
\ Interpolate a metacharacter or character code
$ Interpolate a scalar variable or evaluate an expression in scalar context
@ Interpolate an array variable or evaluate an expression in list context
If a string does not contain any of these then there is nothing to interpolate and Perl will use the string
as it is. Furthermore, Perl first checks for strings that can be interpolated at compile-time, weeding out
all those that are either already constant and do not require interpolation, or can be interpolated to a
constant value. Consequently it does not matter much if we use double quotes for our constant strings or
not; Perl will detect and optimize them before execution starts.

作者: Perl_Er   发布时间: 2011-04-21

热门下载

更多