Intermediate perl 总结提高

本文档总结了Perl编程的中级知识,包括重要的语法特性、模块使用和实战技巧,旨在提升Perl编程技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一章

简介

第二章

File::Spec 模块还提供了许多其它的方法来用可移植的方式处理路径。你可以通过perlport 文档了解更多有关移植方面的专题。
grep 操作符取一个列表和一个"测试表达式". 它一个一个地从列表中把元素取出来放到$_ 变量中, 并在标量环境中, 用"测试表达式"来检验这个值.如果检验出来是个"真"值, grep 会把$_ 变量送到输出列表中。
grep 中的测试表达式在map 中变成了映射表达式.map 操作符在列表环
境中为表达式求值(而不是像grep 那样在标量环境下求值).
Math::BigInt , 它用来处理超出Perl内置精度的整数值.[ *] [*]


一个Perl 标量变量保存一个单个值。一个数组保存一个或多个标量的次序列
表。一个散列保存一个标量作为键值, 另一个标量作为值。尽管一个标量可以
是任意字串, 可以被复杂数据结构用来编入一个数组或一个散列, 但是三种数据类型中没有一个适合用来做复杂数据关系。这就是引用的工作。我们由一个例
子来探查一下引用的重要性。
标量合适的操作对于引用都一样合适.它可以是数组或散列中的一个元素, 或简
单就是一个标量变量。


第三章
1 为了保证平台兼容性,多使用Cwd取当前目录。
use Cwd;
my $dir = getcwd;
或者
my $dir = cwd;
my $dir = fastgetcwd;
它们都返回程序运行的当前路径.
use Cwd 'abs_path';
my $abs_path = abs_path($file);
或者$abs_path = realpath($file);
或者$abs_path= fast_abs_path($file);
2 glob()又叫做文件名聚焦操作符,是一个强大的文本处理器。
定义和用法
返回相匹配的文件EXPR的列表,因为他们将扩大标准的Bourne shell。如果expr不指定路径,则使用当前目录。如果EXPR被忽略,那么使用$_的值。
从Perl5.6扩展内部完成,而不是使用外部脚本。 扩展如csh(及任何衍生工具,包括tcsh和bash的)风格的扩展,该转换,如下所示:
开始带一个单一的文件EXPR被忽略,除非明确地匹配。
* 字符匹配零个或多个字符的任何类型的。
? 任何类型的字符匹配一个字符。
[..] 结构相匹配的字符,包括范围,按正则表达式。
〜 字符相匹配的主目录;〜名相匹配的主目录的用户名。
{..} 括在括号内用逗号分隔的词对任何匹配。
返回值
在标量上下文错误返回undef,否则第一个文件的扩展名列表
在列表上下文中的空列表上的错误,否则列出扩展文件名。
Grep()函数
首先, grep 很直观的告诉读者正在进行的操作是从一串值中选出想要的。其次,grep 比循环简洁。(用软件工程的说法就是 grep 比循环更具有内聚力)。
Map()函数
定义和用法:计算EXPR或LIST块的每个元素。对于每次迭代,$_持有当前元素的值,这也可以被分配以被更新的元素的允许值。
简单地说,Perl中的map()函数运行在每个数组元素的表达,一个新的数组,并返回结果。
返回值:在标量上下文中,返回的元素产生总数量;在列表上下文中,返回的值列表。
第四章
 这里有一个小戏法:我们可以用在外面套上大括号的指向数组的引用, 来替换数
组的名字, 其结果就是访问原始的数组。我们做的仅仅就是把@_( 供应清单的拷贝)替换成@{$items} , 对一个引用的还原操作来取得原始的供应清单数组.现在我们调用子例程次数相比以前少多了。
引用的一个最重要的用法之一:把代码同其操作的具体数据结构分离开, 这样我们可以重用代码并使其更可读。
如果一个引用所指向的数组中还包含着另外一个指向数组的引用会是什么情况?那就成了非常有用的所谓复杂数据结构。
就像我们可以取到一个指向一个数组的引用一样, 我们也可以用反斜杠取到一
个指向散列的引用。我们也能还原一个对散列的引用, 得到它原来的数据.其方法与还原一个数组引用相同.就是当作没有引用这回事一样, 在散列引用变量的名字外面再加一对花括号.
第五章 
非单向引用的数据结构就是:这个数据结构中的一些部份引用与其指向的数据存
在循环引用.比如:这两块数据结构中都有互相指向对方的引用。
我们可以用缩小不同数组名的范围的方法来降低命名空间的繁杂性.代替在子程
序范围内声明变量, 我们可以建立一个临时块:
方括号把内中(相当一在一个列表环境)元素取出来;为这些元素建立一个新的,
匿名的数组;并且(这里很重要)返回一个对这个匿名数组的引用.
:自生成.任何一个不存在的变量, 或个存有undef 的变量, 当被用来查找一个变量的时候(技术上被称为左值环境), 会自动被适当的生成空项引用, Perl 然后再让处理继续。
"消防水带"式的报表生成策略.简单地建立一个散列引用的散列(还可以嵌套更深, 后面你就可以看到), 运用自生成功能, 根据需要填充数据结构, 然后遍历结果数据, 打印输出结果。
#!usr/bin/perl
Use warning
Use strict
my $all = "**all machines**";
my %total_bytes;
while (<>) {
next if /^#/;
my ($source, $destination, $bytes) = split;
$total_bytes{$source}{$destination} += $bytes;
$total_bytes{$source}{$all} += $bytes;
}
my @sources =
sort { $total_bytes{$b}{$all} <=> $total_bytes{$a}{$all} }
keys %total_bytes;
for my $source (@sources) {
my @destinations =
sort { $total_bytes{$source}{$b} <=> $total_bytes{$source}{$a} }
keys %{ $total_bytes{$source} };
print "$source: $total_bytes{$source}{$all} total bytes sent\n";
for my $destination (@destinations) {
next if $destination eq $all;
print " $source => $destination:",
" $total_bytes{$source}{$destination} bytes\n";
}
print "\n";
}
消防水袋模型
第六章 
Data::Dumper模块主要用途是:给出一个或多个变量,包括引用,以PERL语法的方式返回这个变量的内容。
比方说,这里有个很复杂的hash,数据结构很复杂,我想看看这个hash里面的内容。除了常见的方式(直接用print或者编历keys然后打印), 我们也可以使用Data::Daumper->Dump([\%hash])的形式。同时,模块中定义了很多的配置参数,让用户可以调整打印格式。
在Perl中,怎麼簡單的儲存資料。如果你只想單純的把一些資料記下,又不想勞師動眾的安裝巨大的資料庫,還要煩惱資料庫的規劃。隨手使用Storable也許是個不錯的解決方式。
与我们用指向数组的引用来实现用同一代码块在不同时间处理不同数组相同, 我们也可以使用指向子程序的引用实现在不同时间调用不同的子程序。一个变量或者一个复杂数据结构是一个程序中的数据的仓库。一个对子程序的引用可以被想像成为一个程序动作(方法)的仓库。
第七章
一个回调定义了在一个算法中当子程序运行到了一个特定地点时, 程序应该做什么。回调子程序不能有参数, 而调用者也忽略回调子程序返回的结果.但这没有关系.在还原后, 一个子程序引用可以看到所有指向子程序的引用的可见词法变量。
那种能访问所有我们声明的在运行时存在的词法变量的子程序, 我们叫它闭包.在Perl 术语中, 一个闭包就是指一种能引用在程序范围之外的词法变量的子程序。闭包中变量如何被修改的, 而闭包变量还可以被用来初始化变量或给匿名子程序提供参数输入(类似静态局部变量).
第8章
用一个含有空值的标量变量代表句柄名字
第9章
施瓦茨变换
第10章
require 操作符同样有以下两个功能:
任何语法错误会导致程序终止; 所以许多die $@ if $@ 语句在这里是不需要
的。文件最后一个求值必须返回真值。
不能(想)在预先配置在@INC 中的目录中安装模块。但我们可以在require 之前先改变数组@INC 自己。我们要保证添加搜索目录一定要放在其它事之前, 我们可以把它放进BEGIN 块.Perl 会在编译阶段执行在BEGIN 块中的语句, 而在运行时执行require 语句。
一个词法变量通常是临时的或只在程序的局部可访问。如果我们声明一个词法变量, 然后不带包前缀地使用它, 就获得一个词法变量。
第十一章
实现向面向对象重生的只用了一个附加的语法。
第十二章及第二十一章
perl.modules archive - nntp.perl.org
http://www.nntp.perl.org/group/perl.modules/
第十三章
方法箭头可以用在实例上, 就像用在包名上(类)一样。
类方法和实例方法的区别是第一个参数是否为实例(一个被祝福过的引用)或一个类名(一个字串)
第十四章
perl中文件测试的操作符及意义


测试文件的权限:


-r 文件或者目录对目前(有效的)用户是可读的


-w 文件或者目录对目前(有效的)用户是可写的


-x 文件或者目录对目前(有效的)用户是可执行的


-o 文件或者目录有对目前(有效的)用户拥有


-R 文件或者目录,对实际的用户或组来说是可读的


-W 文件或者目录,对实际的用户或者组来说是可写的


-X 文件或者目录,对实际的用户或者组来说是可执行的


-O 文件或者目录,有实际的用户拥有


测试文件的存在:


-e 文件或者目录是存在的


-z 文件存在但没有内容


-s 文件或者目录存在


测试文件的类型:


-f 是普通文件


-d 是目录


-l 是符号链接


-S 是socket类型文件


-p 是命名管道


-b 是块设备文件


-c 是字符设备文件


-u 文件或目录设置了setuid位


-g 文件或目录设置了setguid位


-k 文件或者目录设置了sticky位


-t 文件句柄是TTY设备


-T 文本文件


-B 二进制文件


测试文件的时间:


-M 最后一次被修改至今的天数


-A 最后一次被访问至今的天数


-C 最后一次文件节点编号被变更后至今的天数


下面的perl程序可以查看一个目录下的文件是否是可读可写可执行的,目录名由命令行传入。


第十五章
第十六章


http://www.cbi.pku.edu.cn/chinese/documents/perl/perl13.htm


第十七章
Island::Plotting::Maps
Animal::Utils
Navigate::SeatOfPants


第十八章
对象消亡
第十九章
认识驼鹿
第二十章
高级测试
Over a decade ago (nearly eternity in Internet Time), Randal Schwartz wrote the first edition of Learning Perl. In the intervening years, Perl itself has grown substantially from a "cool" scripting language used primarily by Unix system administrators to a robust object-oriented programming (OOP) language that runs on practically every computing platform known to mankind. Throughout its four editions, Learning Perl remained the same size (about 300 pages) and continued to cover much of the same material to remain compact and accessible to the beginning programmer. But there is much more to learn about Perl now than when that first book was written. Randal called the first edition of this book Learning Perl Objects, References, and Modules, and now it's Intermediate Perl, but we like to think of it as just Learning More Perl. [*] This is the book that picks up where Learning Perl leaves off. We show you how to use Perl to write larger programs. [*] Don't ask why it isn't called that. We must have had 300 emails on the subject. Okay, ask, since we know you're going to anyway. You never really stop learning Perl, so Learning More Perl doesn't really tell you much about the book. Our editor chose the name, which tells you what to expect. As in Learning Perl, we designed each chapter to be small enough to read in just an hour or so. Each chapter ends with a series of exercises to help you practice what you've just learned, and the answers are in the appendix for your reference. And like Learning Perl, we've developed the material in this book for a teaching environment and used it in that setting, including for our own use at Stonehenge Consulting Services, as we conduct on-site and open-enrollment trainings. You don't have to be a Unix guru, or even a Unix user, to benefit from this book. Unless otherwise noted, everything in this book applies equally well to Windows ActivePerl from ActiveState and all other modern implementations of Perl. To use this book, you just need to be familiar with the material in Learning Perl and have the ambition to go further. Structure of This Book You should read this book from front to back, stopping to do the exercises. Each chapter builds on preceding chapters, and we'll assume that you know the material from those chapters as we discuss new topics. Chapter 1, Introduction - 2 - Intermediate Perl An introduction to the material. Chapter 2, Intermediate Foundations Pick up some intermediate Perl skills you'll need for the rest of the book. Chapter 3, Using Modules Use Perl's core modules, as well as modules from other people. We're going to show you how to create your own modules later in the book, but until we do, you can still use modules you already have. Chapter 4, Introduction to References Introduce a level of redirection to allow the same code to operate on different sets of data. Chapter 5, References and Scoping Learn how Perl manages to keep track of pointers to data, and an introduction to anonymous data structures and autovivification. Chapter 6, Manipulating Complex Data Structures Create, access, and print arbitrarily deep and nested data structures, including arrays of arrays and hashes of hashes. Chapter 7, Subroutine References Capture behavior as an anonymous subroutine that you create dynamically and execute later. Chapter 8, Filehandle References Store filehandles in scalar variables that you can easily pass around your program or store in data structures. Chapter 9, Practical Reference Tricks Sorting complex operations, the Schwartzian Transform, and working with recursively defined data. Chapter 10, Building Larger Programs Build larger programs by separating code into separate files and namespaces. Chapter 11, Introduction to Objects - 3 - Intermediate Perl Work with classes, method calls, inheritance, and overriding. Chapter 12, Objects with Data Add per-instance data, including constructors, getters, and setters. Chapter 13, Object Destruction Add behavior to an object that is going away, including object persistence. Chapter 14, Some Advanced Object Topics Use multiple inheritance, automatic methods, and references to filehandles. Chapter 15, Exporter How use works, how we can decide what to export, and how we can create our own import routines. Chapter 16, Writing a Distribution Package a module for sharing, including portable installation instructions. Chapter 17, Essential Testing Test your code to ensure it does what you want it to do. Chapter 18, Advanced Testing Test complex aspects of code and meta-code things such as documentation and test coverage. Chapter 19, Contributing to CPAN Share your work with the world by uploading it to CPAN. Appendix, Answers to Exercises Where to go to get answers. Conventions Used in This Book The following typographic conventions are used in this book: Constant width - 4 - Intermediate Perl Used for function names, module names, filenames, environment variables, code snippets, and other literal text Italics Used for emphasis and for new terms where they are defined
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值