免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5990 | 回复: 16
打印 上一主题 下一主题

__END__和__DATA__ [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-27 21:28 |只看该作者 |倒序浏览
__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

[ 本帖最后由 wertyu 于 2008-12-28 12:19 编辑 ]

论坛徽章:
1
辰龙
日期:2014-05-15 19:37:15
2 [报告]
发表于 2008-12-27 23:35 |只看该作者
这本破书的例子有问题。。。。这个地方我也花了很多时间来看看 ,--DATA-- 这个只是指输入的文件句柄之类的意思,不是实体的词....换成_--AAAA--也行,只是指这种类型来输入的数据

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


我倒!

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2008-12-28 10:24 |只看该作者
原帖由 iakuf 于 2008-12-27 23:35 发表
这本破书的例子有问题。。。。这个地方我也花了很多时间来看看 ,--DATA-- 这个只是指输入的文件句柄之类的意思,不是实体的词....换成_--AAAA--也行,只是指这种类型来输入的数据

是 __DATA__,不是 --DATA--

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2008-12-28 10:30 |只看该作者
原帖由 MMMIX 于 2008-12-28 10:24 发表

是 __DATA__,不是 --DATA--

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

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

论坛徽章:
0
6 [报告]
发表于 2008-12-28 11:41 |只看该作者
原帖由 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");
  }

[ 本帖最后由 pugs 于 2008-12-28 11:42 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2008-12-28 12:24 |只看该作者
原帖由 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__究竟什么关系啊?

[ 本帖最后由 wertyu 于 2008-12-28 13:01 编辑 ]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
8 [报告]
发表于 2008-12-28 13:03 |只看该作者
一定要学会查文档。
  1.     The two control characters ^D and ^Z, and the tokens __END__ and
  2.     __DATA__ may be used to indicate the logical end of the script before
  3.     the actual end of file. Any following text is ignored.

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

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

论坛徽章:
0
9 [报告]
发表于 2008-12-28 13:08 |只看该作者
有点弄明白了,__DATA__和__END__同样都是表示脚本结束,不过__END__只能在main package中使用,而__DATA__在任意package里都可以使用。

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

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

论坛徽章:
0
10 [报告]
发表于 2008-12-28 13:10 |只看该作者

  1. perldoc perldata
复制代码

看special literals一节

  1. 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.

  2. 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 .
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP