有太屡次写完一个perl程序,须要另外新建一个文件来测试,每次以为很繁琐,但又不得不这么作。没想到原来perl已经提供了解决方案,这就是DATA。php
使用很简单,见下面这个例子:html
1 #!/usr/bin/perl
2
3 while (<DATA>) {
4 print;
5 }
6
7 __DATA__
8 hello perl
输出结果: hello perlide
这个用法太方便太perl了,之后不再须要使用新建文件的笨方法了。post
下面是解释:测试
<IN>能够从打开的句柄IN中得到数据,<STDIN>能够从标准输入接收数据,相似地,<DATA> 文件句柄能够直接从执行它的脚本中获取数据,而不是从命令行或者从另外一个文件里获取。<DATA> 所读取的数据保存在每一个脚本末尾的特殊常量__DATA__以后,同时这个常量也标志着脚本的逻辑结束。url
注意:脚本中有两个while(<DATA>)将只有前一个起做用,由于前一个已经读到了文件结尾。好比如下示例:spa
1 #!/usr/bin/perl -w
2 use strict;
3
4 while (<DATA>) {
5 print "$_";
6 }
7
8 while (<DATA>) {
9 print;
10 }
11
12 __DATA__
13 hello world
输出结果:.net
hello world命令行
而不是:code
hello world
hello world
__DATA__
A Virtual File In Perl
Steps to follow:1) At the end of the code use __DATA__ marker and copy paste or write the contents of the file you wish to test exactly from the next line of _DATA_.2) Use while (){ -- your code here -- }.#!/usr/bin/perluse strict;use warnings;while () {chomp $_; print "Found" if $_ =~ /(t\w+)/; print $1;}__DATA__hello there how r uOutput of the above program will be “there”.