Perl的DATA文件句柄
有太多次写完一个perl程序,需要另外新建一个文件来测试,每次觉得很繁琐,但又不得不这么做。没想到原来perl已经提供了解决方案,这就是DATA。
使用很简单,见下面这个例子:
#!/usr/bin/perl while (<DATA>) {
print;
} __DATA__
hello perl
输出结果: hello perl
这个用法太方便太perl了,以后再也不需要使用新建文件的笨方法了。
下面是解释:
<IN>可以从打开的句柄IN中获得数据,<STDIN>可以从标准输入接收数据,类似地,<DATA> 文件句柄可以直接从执行它的脚本中获取数据,而不是从命令行或者从另一个文件里获取。<DATA> 所读取的数据保存在每个脚本末尾的特殊常量__DATA__之后,同时这个常量也标志着脚本的逻辑结束。
注意:脚本中有两个while(<DATA>)将只有前一个起作用,因为前一个已经读到了文件结尾。比如以下示例:
#!/usr/bin/perl -w
use strict; while (<DATA>) {
print "$_";
} while (<DATA>) {
print;
} __DATA__
hello world
输出结果:
hello world
而不是:
hello world
hello world
__DATA__
A Virtual File In Perl
It has been occurred a lot of times that one has to test a code using data from a file. It can be anything like checking for a pattern in a file to taking some input. If you want to check the code without opening the file and reading it. One can use the __DATA__ marker provided by perl as a pseudo-datafile.
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/perl
use strict;
use warnings;
while () {
chomp $_;
print "Found" if $_ =~ /(t\w+)/;
print $1;
}
__DATA__
hello there how r u
Output of the above program will be “there”.
Perl的DATA文件句柄的更多相关文章
- perl 通过<<和文件句柄将数据写入到文件中去
可以通过文件句柄和<<运算符将文件内容写入到文件中去 #!usr/bin/perl -W use strict; use Spreadsheet::ParseExcel; use utf8 ...
- perl学习之文件句柄filehandle
句柄handle 分为文件句柄和目录句柄,文件句柄实际上包含文件,进程和套接字的读写. 文件句柄的操作步骤 open(FD,"filename"); @lines=<FD ...
- Perl的IO操作(1):文件句柄
文件句柄 文件句柄用来对应要操作的文件系统中的文件,这么说不太严谨,但比较容易理解.首先为要打开的文件绑定文件句柄(称为打开文件句柄),然后在后续的操作中都通过文件句柄来操作对应的文件,最后关闭文件句 ...
- Perl文件句柄相关常量变量
文件句柄相关变量 对应的官方手册:http://perldoc.perl.org/perlvar.html#Variables-related-to-filehandles 默认情况下: $/:输入行 ...
- Perl输出复杂数据结构:Data::Dumper,Data::Dump,Data::Printer
输出复杂结构 Data::Dumper.Data::Dump.Data::Printer都可以用来输出复杂的数据结构.本文只介绍简单的几个输出形式,以后再需要的地方再详细介绍. 前两者建议传递数据结构 ...
- perl学习(5) 输入和输出
1.1. 从标准输入设备输入 <STDIN> 行输入操作在到达文件的结尾时将返回undef,在while循环的条件中不能使用chomp: while (defined($line = &l ...
- perl常用代码
字符串联结和重复操作符 联接: . 重复:x 联接且赋值(类似+=): .=例: $newstring = "potato" . "head"; $ ...
- perl 学习笔记
一:基础 1:安装perl centos: yum -y install perl 官网:https://www.perl.org/ 升级到5.22:先下载,执行./i ...
- Perl IO:简介和常用IO模块
三篇Perl IO基础类文章: Perl的IO操作(1):文件句柄 Perl的IO操作(2):更多文件句柄的模式 Perl文件句柄相关的常见变量 IO对象和IO::Module家族模块 无论是哪种高级 ...
随机推荐
- 【2013微软面试题】输出节点数为n的二叉树的所有形态
转自:http://blog.csdn.net/monsterxd/article/details/8449005 /* * 题意,求节点数为n的二叉树的所有形态,先要想个方式来唯一标示一棵二叉树 ...
- 捣蛋phpwind控制器注入
在PwBaseController 里面,会有这个方法的存在 /** * action Hook 注册 * * @param string $registerKey 扩展点别名 * @param Pw ...
- 手势冲突UIPanGestureRecognizer 和UIPinchGestureRecognizer
当同时使用pan和pin手势时假如冲突,需要加入下面方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shou ...
- 轻松学Shell之认识正规表达式
离线下载观看:http://down.51cto.com/data/148117 650) this.width=650;" onclick='window.open("htt ...
- MVC中过虑特殊字符检测
[ValidateInput(false)] [HttpPost] public ActionResult Modify(Models.BlogArticle model) { //...... } ...
- HDU 1525 Euclid's Game (博弈)
Euclid's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Chocolatey的安装与使用
@(编程) 前言 在 Linux 下,大家喜欢用 apt-get 来安装应用程序,如今在 windows 下,大家可以使用 Chocolatey 来快速下载搭建一个开发环境. Chocolatey 的 ...
- 使用aspose.word两句代码将word转换为pdf
//Load Document Document document = new Document(@"C:\Users\Administrator\Desktop\人事---新员工转正总结( ...
- Linux下安装Android Studio (Centos 7)
首先去下载一个android studio的包. http://www.android-studio.org/ (友情一个) http://www.cnblogs.com/gssl/p/4963668 ...
- 图片 文字 input等垂直居中对齐
span::before { width:100%; height:1px; font-size:1em; content:''; background:#fff; position:absolute ...