Chinaunix

标题: 帮忙解释一下这段代码内部原理 $SEEK = "123.txt";open(SEEK); [打印本页]

作者: hitsubunnu    时间: 2008-03-10 18:39
标题: 帮忙解释一下这段代码内部原理 $SEEK = "123.txt";open(SEEK);
今天看到一段代码 虽然知道运行结果 但是不明白里面的原理


  1. $SEEK = "123.txt";
  2. open(SEEK);
  3. while(<SEEK>){
  4.         print;
  5. }
  6. close SEEK;
复制代码


open(SEEK) 里面省略了什么?  怎么把"123.txt"带进去的
作者: churchmice    时间: 2008-03-10 18:45
你确认是SEEK而不是$SEEK  ??
作者: hitsubunnu    时间: 2008-03-10 18:46
不是   你可以运行一下   就是因为这个 我才不理解呢
作者: churchmice    时间: 2008-03-10 18:49
果然神奇
see
perldoc -f open
open FILEHANDLE,EXPR
       open FILEHANDLE,MODE,EXPR
       open FILEHANDLE,MODE,EXPR,LIST
       open FILEHANDLE,MODE,REFERENCE
       open FILEHANDLE
               Opens the file whose filename is given by EXPR, and associates
               it with FILEHANDLE.

               (The following is a comprehensive reference to open(): for a
               gentler introduction you may consider perlopentut.)

               If FILEHANDLE is an undefined scalar variable (or array or hash
               element) the variable is assigned a reference to a new anony-
               mous filehandle, otherwise if FILEHANDLE is an expression, its
               value is used as the name of the real filehandle wanted.  (This
               is considered a symbolic reference, so "use strict 'refs'"
               should not be in effect.)

            
  If EXPR is omitted, the scalar variable of the same name as the
               FILEHANDLE contains the filename.  (Note that lexical vari-
               ables--those declared with "my"--will not work for this pur-
               pose; so if you're using "my", specify EXPR in your call to
               open.)


就是说open(FILEHANDLE)相当与

  1. open(FILEHANDLE,$FILEHANDLE);
复制代码

[ 本帖最后由 churchmice 于 2008-3-10 18:51 编辑 ]
作者: hitsubunnu    时间: 2008-03-10 19:03
perl 有点儿太自由了  

我就因为这个  改了将近4个小时的程序  让我在将近2000行代码中 就找打开文件的位置

这么写程序 有些害人

在此对之前写程序的人亲属表示抱歉
作者: churchmice    时间: 2008-03-10 19:14
原帖由 hitsubunnu 于 2008-3-10 19:03 发表
perl 有点儿太自由了  

我就因为这个  改了将近4个小时的程序  让我在将近2000行代码中 就找打开文件的位置

这么写程序 有些害人

在此对之前写程序的人亲属表示抱歉

习惯了就好了
作者: start2008    时间: 2008-03-10 21:38
呵呵 懒惰是perler的美德
正在看的learning perl中提到的
又学了一招
作者: perljoker    时间: 2008-03-11 09:52

我倒,这招学了可不好……
作者: cobrawgl    时间: 2008-03-11 10:37
perl 给你自由

但自由是有代价的
作者: jcool    时间: 2008-06-19 09:54
呵呵,好有意思 ...
作者: 不死草    时间: 2008-06-19 15:29
是啊,自由是有代价的,特别怕别人以前的程序中的“bug”...上次因为一个"Sprintf"函数的问题查一半天。。。
作者: dlms    时间: 2008-06-20 10:05
看错了。免得被人BS

[ 本帖最后由 dlms 于 2008-6-20 10:26 编辑 ]
作者: hdksky    时间: 2008-06-23 14:20
自由好,写起来方便,其他人读起来就要麻烦点了
作者: redspider    时间: 2008-06-24 12:25
这样搞是在毁 perl
作者: bibi2008    时间: 2008-06-24 12:46
晕啊,还有这种省略的用法。
作者: socyno    时间: 2008-07-02 21:57
这种还是不要学的好
作者: xht314    时间: 2008-07-03 09:43
生命诚可贵,爱情价更高,若为自由故,两者皆可抛!
作者: converse    时间: 2008-07-03 09:49
太自由了确实不好.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2