免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123
最近访问板块 发新帖
楼主: 思平
打印 上一主题 下一主题

和仙子等众高手一起探讨一个问题 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2005-12-06 20:14 |只看该作者
  1. our %hash = ( a=>1 );
  2. my $glob = *hash;
复制代码



这2句偶理解就没觉得有困难,可能偶没想太多吧,:P
*hash只是个符号入口,它指示符号位于哪个包,以及符号的名称,具体是个ref还是个pointer,没必要去深究呀。
*hash{LABEL}只有在加上这个LABEL后才有意义,表明它是指向一个hash,还是一个scalar,还是一个array.
所以$glob = *hash并不存在上下文的错误哦,*hash这里并不代表hash,也不代表任何hash的引用,它只是个符号入口。
因为*hash可以代表一切,包括$hash,%hash,@hash,&hash,还有HANDLE等,但又可以说什么具体意义都没有,所以确实容易让人糊涂。

好在偶不想那么多,所以也不必为这个问题头痛了,嘿嘿。

一花一世界,一叶一菩提。
无我无相,四大皆空。

读读禅经,或许有助于理解,:P

论坛徽章:
0
22 [报告]
发表于 2005-12-06 21:56 |只看该作者
原帖由 兰花仙子 于 2005-12-6 20:14 发表
无我无相,四大皆空。

读读禅经,或许有助于理解,

舍利子,是诸法空相,不生不灭,不垢不净不增不减。
仙子既然提到“禅”,禅宗由慧能始,又由慧能止,六祖曰:“菩提本无树,明镜亦非台”,何为“四大皆空”?
何谓“空”?
于真性情中,亦可见佛性,一味地追求空,其实却陷入了“色”,须知努力地修行并不能“增”,亦不能“净”,更不能“生”
古时有高僧由画入佛,我来个由 Perl 入佛,亦无不可……

[ 本帖最后由 思平 于 2005-12-6 22:01 编辑 ]

论坛徽章:
0
23 [报告]
发表于 2005-12-06 22:05 |只看该作者
http://www.fayland.org/books/effective_perl.pdf
这是一本 Royalty-free licenses 的书,可以自由下载和阅读的。
里面的一些图表 PErl Graphical Structures 对 引用这类东西讲的很好。我推荐诸位有空读一下。

论坛徽章:
0
24 [报告]
发表于 2005-12-07 10:49 |只看该作者
mlists.j@gmail.com 是哪位呢?
该不会是 ChinaUnix 上的朋友吧?

我今天从 maillist 上收到这份会话,看了一下时间是 12.5 号的,难道 mlists.j@gmail.com 是哪位 ChinaUnix 上的朋友?
仙子是你吗?

  1. A Strange Syntax
  2. 7 封邮件
  3. --------------------------------------------------------------------------------
  4. Jennifer Garner <mlists.j@gmail.com>  2005年12月5日 下午9:52  
  5. 收件人: beginners@perl.org
  6. Hi,lists,

  7. Seeing this code please:

  8. our %sym = (
  9.    name => 'flower',
  10.    age  => 23,
  11. );

  12. print ${*{$::{sym}}{HASH}}{name};

  13. The result of printing is : flower.

  14. How to analyse the last sentence of that code?Thanks.




  15. --------------------------------------------------------------------------------
  16. Adriano Ferreira <a.r.ferreira@gmail.com>  2005年12月5日 下午10:53  
  17. 收件人: Jennifer Garner <mlists.j@gmail.com>, beginners@perl.org
  18. On 12/5/05, Jennifer Garner <mlists.j@gmail.com> wrote:
  19. > print ${*{$::{sym}}{HASH}}{name};

  20. > How to analyse the last sentence of that code?Thanks.

  21. From "perldoc perlref"

  22.       7.  A reference can be created by using a special syntax, lovingly
  23.           known as the *foo{THING} syntax.  *foo{THING} returns a reference
  24.           to the THING slot in *foo (which is the symbol table entry which
  25.           holds everything known as foo).

  26.               $scalarref = *foo{SCALAR};
  27.               $arrayref  = *ARGV{ARRAY};
  28.               $hashref   = *ENV{HASH};
  29.               $coderef   = *handler{CODE};
  30.               $ioref     = *STDIN{IO};
  31.               $globref   = *foo{GLOB};

  32. So $::{sym} returns the glob symbol "sym" on the main package ($main::
  33. or $::), takes a reference to its HASH part, and returns what it got
  34. in the key "name". Piece of cake, ain't it?

  35. Regards,
  36. Adriano.

  37. --
  38. To unsubscribe, e-mail: beginners-unsubscribe@perl.org
  39. For additional commands, e-mail: beginners-help@perl.org
  40. <http://learn.perl.org/> <http://learn.perl.org/first-response>





  41. --------------------------------------------------------------------------------
  42. Wiggins d'Anconia <wiggins@danconia.org>  2005年12月6日 上午2:33  
  43. 收件人: Adriano Ferreira <a.r.ferreira@gmail.com>
  44. 抄送: Jennifer Garner <mlists.j@gmail.com>, beginners@perl.org
  45. Adriano Ferreira wrote:
  46. > On 12/5/05, Jennifer Garner <mlists.j@gmail.com> wrote:
  47. >
  48. >>print ${*{$::{sym}}{HASH}}{name};
  49. >
  50. >
  51. >>How to analyse the last sentence of that code?Thanks.
  52. >
  53. >
  54. >>From "perldoc perlref"
  55. >
  56. >        7.  A reference can be created by using a special syntax, lovingly
  57. >            known as the *foo{THING} syntax.  *foo{THING} returns a reference
  58. >            to the THING slot in *foo (which is the symbol table entry which
  59. >            holds everything known as foo).
  60. >
  61. >                $scalarref = *foo{SCALAR};
  62. >                $arrayref  = *ARGV{ARRAY};
  63. >                $hashref   = *ENV{HASH};
  64. >                $coderef   = *handler{CODE};
  65. >                $ioref     = *STDIN{IO};
  66. >                $globref   = *foo{GLOB};
  67. >
  68. > So $::{sym} returns the glob symbol "sym" on the main package ($main::
  69. > or $::), takes a reference to its HASH part, and returns what it got
  70. > in the key "name". Piece of cake, ain't it?
  71. >
  72. > Regards,
  73. > Adriano.
  74. >

  75. Now that you understand it, replace it with $sym->{name} so the next
  76. person doesn't have to ask. Unless you are using a really old Perl.

  77. http://danconia.org

  78. [引用文字已隐藏]


  79. --------------------------------------------------------------------------------
  80. Flemming Greve Skovengaard <dsl58893@vip.cybercity.dk>  2005年12月6日 上午3:05  
  81. 收件人: "beginners@perl.org" <beginners@perl.org>
  82. 抄送: Wiggins d'Anconia <wiggins@danconia.org>
  83. Wiggins d'Anconia wrote:
  84. >
  85. > Now that you understand it, replace it with $sym->{name} so the next
  86. > person doesn't have to ask. Unless you are using a really old Perl.
  87. >

  88. Actually that should be *sym->{name} instead of $sym->{name} ( or %sym->{name}
  89. but that's deprecated ).
  90. Else you get "Variable "$sym" is not imported at ..." when using 'use strict;'
  91. ( as you should ).

  92. --
  93. Flemming Greve Skovengaard                    The prophecy of the holy Norns
  94. a.k.a Greven, TuxPower                        The world is doomed to die
  95. <dsl58893@vip.cybercity.dk>                   Fire in the sky
  96. 4112.38 BogoMIPS                              The end is coming soon

  97. [引用文字已隐藏]


  98. --------------------------------------------------------------------------------
  99. Jennifer Garner <mlists.j@gmail.com>  2005年12月6日 上午9:10  
  100. 收件人: Flemming Greve Skovengaard <dsl58893@vip.cybercity.dk>
  101. 抄送: "beginners@perl.org" <beginners@perl.org>, Wiggins d'Anconia <wiggins@danconia.org>
  102. ${*{$::{sym}}{HASH}}{name};

  103. As we know, $::{sym} == *main::sym, it's a typeglob.
  104. but what is **main::sym? and the same,what is *{$glob}?thanks.

  105. [引用文字已隐藏]


  106. --------------------------------------------------------------------------------
  107. Adriano Ferreira <a.r.ferreira@gmail.com>  2005年12月7日 上午12:26  
  108. 收件人: Jennifer Garner <mlists.j@gmail.com>, beginners@perl.org
  109. On 12/5/05, Jennifer Garner <mlists.j@gmail.com> wrote:
  110. > As we know, $::{sym} == *main::sym, it's a typeglob.
  111. > but what is **main::sym? and the same,what is *{$glob}?thanks.

  112. **main::sym is a syntax error, but *{*main::sym}==*main::sym.

  113. But don't be fooled by the equality $::{sym} == *main::sym. It just
  114. means they numerically compare the same. Taking references you get
  115. that $::{sym} returns a scalar and *main::sym a glob.

  116. $ perl -e 'print \($::sym, *main::sym)'
  117. SCALAR(0x1002f094)GLOB(0x10010fa8)

  118. So *{$glob} is the way to tell Perl to go from the scalar to the glob,
  119. when we'll be able to access its HASH part.

  120. That's why

  121. $ perl -e 'our %sym = (name => "flower"); print ${*{$::{sym}}{HASH}}{name};"

  122. prints "flower", but

  123. $ perl -e 'our %sym = (name => "flower"); print ${$::{sym}{HASH}}{name};"

  124. prints nothing ($::{sym}{HASH} returns undef). As Wiggins wisely said,
  125. $sym->{name} is more sane.

  126. --

  127. [引用文字已隐藏]


  128. --------------------------------------------------------------------------------
  129. Jennifer Garner <mlists.j@gmail.com>  2005年12月7日 上午9:16  
  130. 收件人: "beginners@perl.org" <beginners@perl.org>
  131. Thanks for Adriano Ferreira.Your explaination is good for me.I have known
  132. that.

  133. [引用文字已隐藏]
复制代码

[ 本帖最后由 思平 于 2005-12-7 11:07 编辑 ]

论坛徽章:
0
25 [报告]
发表于 2005-12-07 11:24 |只看该作者
经过多方考证,
我现在已经基本上已经有了结论了。
等下有空了我再参照一下 Perl 的源码,争取对 GLOB 做个结论。

论坛徽章:
0
26 [报告]
发表于 2005-12-07 12:08 |只看该作者
**main::sym is a syntax error, but *{*main::sym}==*main::sym.

But don't be fooled by the equality $::{sym} == *main::sym. It just
means they numerically compare the same. Taking references you get
that $::{sym} returns a scalar and *main::sym a glob.

$ perl -e 'print \($::sym, *main::sym)'
SCALAR(0x1002f094)GLOB(0x10010fa8)

So *{$glob} is the way to tell Perl to go from the scalar to the glob,
when we'll be able to access its HASH part.

That's why

$ perl -e 'our %sym = (name => "flower"); print ${*{$::{sym}}{HASH}}{name};"

prints "flower", but

$ perl -e 'our %sym = (name => "flower"); print ${$::{sym}{HASH}}{name};"

prints nothing ($::{sym}{HASH} returns undef). As Wiggins wisely said,
$sym->{name} is more sane.


上述这段话足够解答你的疑惑了,嘿嘿。

论坛徽章:
0
27 [报告]
发表于 2005-12-07 12:59 |只看该作者
呵呵!
其实我什么疑惑都没有……
唉。
不说也罢。

论坛徽章:
0
28 [报告]
发表于 2005-12-07 13:18 |只看该作者
$glob = *sym;     # $glob 突然一下子就变成了 GLOB?不再是 SCALAR 了?再或者 GLOB 就像 REF 一样,本质上也是一个标量?

$glob还是个SCALAR的,*sym是个GLOB,为什么它们会相等?
也用不了想那么多的,Perl内部就定义了它们可以这样赋值的。
our %sym;
这样申明后,查看%::符号表,会找到:
$::{sym} == *main::sym
上述也明显的左边是SCALAR,右边是GLOB,它们就是这样赋值的。
GLOB具体是个什么东东?就等着各位去揭晓了。偶认为它只是一个符号,就是内存中的某个点,它存储了指向具体数据类型的指针。
"一粒微尘看世界",当年佛祖捻花一笑,感化世间万丈红尘;Larry Wall键盘一敲,蹦出了GLOB这个莫须有而又包囊无限的东东。

论坛徽章:
0
29 [报告]
发表于 2005-12-07 13:42 |只看该作者
如果从 Perl 解释器的代码这个层面上来看,GLOB 的确是一种 Scalar,这个只要看一下 sv.h 就知道了(注),不过这不是我想要的。
事实上,我非常不愿意做的,就是从语言的实现上去解释语言。
截止到目前为止,我还是没有放弃。
大家说的我都明白,我只是想,怎么解释可以让它更优美、更抽象……


注:
sv.h 中有如下代码:
  1. typedef enum {
  2.     SVt_NULL,   /* 0 */
  3.     SVt_IV,     /* 1 */
  4.     SVt_NV,     /* 2 */
  5.     SVt_RV,     /* 3 */
  6.     SVt_PV,     /* 4 */
  7.     SVt_PVIV,   /* 5 */
  8.     SVt_PVNV,   /* 6 */
  9.     SVt_PVMG,   /* 7 */
  10.     SVt_PVBM,   /* 8 */
  11.     SVt_PVLV,   /* 9 */
  12.     SVt_PVAV,   /* 10 */
  13.     SVt_PVHV,   /* 11 */
  14.     SVt_PVCV,   /* 12 */
  15.     SVt_PVGV,   /* 13 */
  16.     SVt_PVFM,   /* 14 */
  17.     SVt_PVIO    /* 15 */
  18. } svtype;
复制代码

[ 本帖最后由 思平 于 2005-12-7 14:16 编辑 ]

论坛徽章:
0
30 [报告]
发表于 2005-12-08 13:53 |只看该作者
楼上各位都说的是中文,我怎么就看不懂泥....
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP