__END__和__DATA__

__END__和__DATA__

__END__ 表示脚本的末尾位置,其后内容将被忽略
__DATA__ 特殊文件句柄

看到perl by example里一个示例说可以用_END_代替_DATA_,实验一下确实如此但是不明白为什么?

比如
----------------------------------------------------
...
print <DATA>;

__DATA__
The quick brown fox jumps over the lazy dog
----------------------------------------------------


----------------------------------------------------
...
print <DATA>;

__END__
The quick brown fox jumps over the lazy dog
----------------------------------------------------

都是输出:
The quick brown fox jumps over the lazy dog

这本破书的例子有问题。。。。这个地方我也花了很多时间来看看 ,--DATA-- 这个只是指输入的文件句柄之类的意思,不是实体的词....换成_--AAAA--也行,只是指这种类型来输入的数据


QUOTE:
原帖由 iakuf 于 2008-12-27 23:35 发表
这本破书的例子有问题。。。。这个地方我也花了很多时间来看看 ,--DATA-- 这个只是指输入的文件句柄之类的意思,不是实体的词....换成_--AAAA--也行,只是指这种类型来输入的数据

我倒!


QUOTE:
原帖由 iakuf 于 2008-12-27 23:35 发表
这本破书的例子有问题。。。。这个地方我也花了很多时间来看看 ,--DATA-- 这个只是指输入的文件句柄之类的意思,不是实体的词....换成_--AAAA--也行,只是指这种类型来输入的数据

是 __DATA__,不是 --DATA--


QUOTE:
原帖由 MMMIX 于 2008-12-28 10:24 发表

是 __DATA__,不是 --DATA--

我一直认为,这些细微之处,反应了一个人是否适合搞编程。

我相信那位朋友一定清楚自己想要表达的其实就是 __DATA__,
只不过:
1,键盘不好
or
2,懒得按 shift
or
3,不小心打错了,懒得纠正
罢了。


QUOTE:
原帖由 flw 于 2008-12-28 10:30 发表

我一直认为,这些细微之处,反应了一个人是否适合搞编程。

我相信那位朋友一定清楚自己想要表达的其实就是 __DATA__,
只不过:
1,键盘不好
or
2,懒得按 shift
or
3,不小心打错了,懒得纠正
罢了。

深表赞同。

有些人知道code标签懒得用、也懒的缩进和排版,像下面这样粘上一堆乱糟糟的代码就跑来问人:
#include <stdio.h>

int main() {
      int i;
   if (i < 1)
  printf("hello\n");
    else
     printf("world\n");
  }



QUOTE:
原帖由 iakuf 于 2008-12-27 23:35 发表
这本破书的例子有问题。。。。这个地方我也花了很多时间来看看 ,--DATA-- 这个只是指输入的文件句柄之类的意思,不是实体的词....换成_--AAAA--也行,只是指这种类型来输入的数据

刚刚实验了下,好像事实和这位兄弟说的有些出入啊,不知道是不是我哪里理解错了

#!/usr/bin/perl

use strict;
use warnings;

while (<DATA>) {
        chomp;
        print "Found:\n$`<$&>$'\n" if /\bfox\b/;
}

__DATA__
The quick brown fox jumps over the lazy dog




把__DATA__换成__END__倒是一样的结果:
Found:
The quick brown <fox> jumps over the lazy dog

但是换成别的任意值就不对了。 这个__DATA__和__END__究竟什么关系啊?

一定要学会查文档。

[Copy to clipboard] [ - ]
CODE:
    The two control characters ^D and ^Z, and the tokens __END__ and
    __DATA__ may be used to indicate the logical end of the script before
    the actual end of file. Any following text is ignored.

    Text after __DATA__ but may be read via the filehandle "PACKNAME::DATA",
    where "PACKNAME" is the package that was current when the __DATA__ token
    was encountered. The filehandle is left open pointing to the contents
    after __DATA__. It is the program's responsibility to "close DATA" when
    it is done reading from it. For compatibility with older scripts written
    before __DATA__ was introduced, __END__ behaves like __DATA__ in the
    toplevel script (but not in files loaded with "require" or "do") and
    leaves the remaining contents of the file accessible via "main::DATA".

    See SelfLoader for more description of __DATA__, and an example of its
    use. Note that you cannot read from the DATA filehandle in a BEGIN
    block: the BEGIN block is executed as soon as it is seen (during
    compilation), at which point the corresponding __DATA__ (or __END__)
    token has not yet been seen.

有点弄明白了,__DATA__和__END__同样都是表示脚本结束,不过__END__只能在main package中使用,而__DATA__在任意package里都可以使用。

__DATA__会打开文件句柄DATA, 看样子__END__也会打开DATA文件句柄。

如果有理解错的地方麻烦指点。


[Copy to clipboard] [ - ]
CODE:
perldoc perldata

看special literals一节

[Copy to clipboard] [ - ]
CODE:
The two control characters ^D and ^Z, and the tokens __END__ and __DATA__ may be used to indicate the logical end of the script before the actual end of file. Any following text is ignored.

Text after __DATA__ but may be read via the filehandle PACKNAME::DATA , where PACKNAME is the package that was current when the __DATA__ token was encountered. The filehandle is left open pointing to the contents after __DATA__. It is the program's responsibility to close DATA when it is done reading from it. For compatibility with older scripts written before __DATA__ was introduced, __END__ behaves like __DATA__ in the top level script (but not in files loaded with require or do) and leaves the remaining contents of the file accessible via main::DATA .