Skip to content

四火的唠叨

一个纯正程序员的啰嗦

Menu
  • 所有文章
  • About Me
  • 关于四火
  • 旅行映像
  • 独立游戏
  • 资源链接
Menu

不妨学习一下 Perl

Posted on 03/13/201206/23/2019 by 四火

perl Perl 是什么?用 Larry Wall 自己的话说,Perl 是借取了 C、sed、awk、shell scripting 以及很多其他程序语言的特性的一种语言。你可以说它是 “实用报表提取语言”(Practical Extraction and Report Language),也可以叫它 “病态折中垃圾列表器”(Pathologically Eclectic Rubbish Lister),Larry 说:“OK,我都承认”。

最近机缘巧合,有幸接触了一把 Perl,以及它的模板引擎 Mason(我是这么理解的)。我用过 Java、C#、C++等等传统高级语言,也研究过 Groovy、JavaScript 之类的动态语言,甚至自虐过 VB(Don Box 还说了一段很好玩的话,其中的 “Dim me As VBProgrammer” 流传甚广),我很懒,并不打算仔细研究 Perl——有的东西我有兴趣一查到底,有的东西觉得点到即止,够用就好,于是只想粗略地把它过一遍,不过 Perl 还是立马给我留下了深刻印象。

Martin Fowler 提出了两种 API 风格:最小接口和人本接口,前者简约、干净、独立,接口覆盖功能没有交集,而后者则提供了一大堆人性化的方法,方法之间互有冗余。Perl 就像人本接口,用 Perl 爱好者们自己的话说,叫做 “There’s More Than One Way To Do It.”,虽然很多动态语言也提供了丰富的方法,但是 Perl 把这一点做到了极致。

事实上,我在几个月以前就已经写出了一些简单的 perl 脚本,但是它和 Linux 的亲和力让它非常好用。那个时候我还几乎不了解 Perl,但是我就照着同事的一点 Perl 代码,加上我自己的理解,去完成了一个在 Linux 上用的小工具,Perl 就是如此具有易上手的特性,连 Google 的时间都省了。

如果你就这样认为 Perl 是一个语法简单的语言,那就错了。Perl 是一个可以写出极其简练代码的语言(这往往意味着也可以写出极其混乱的代码来,比如这个),但是功能上并不含糊(有一个很著名的 Perl 项目 ppt,用纯 perl 实现了所有 Unix 下的常用命令)。另外,Perl 大概是最好的对字符串进行处理的语言。

给以文件操作的例子,如果用 Java,需要写:

BufferedReader br = null;  
try {  
    br = new BufferedReader(new FileReader("test.txt"));  
    String line = br.readLine();  
while (null != line) {  
if (line.startsWith("http:"))  
            System.out.print(line);  
        line = br.readLine();  
    }  
} catch (FileNotFoundException e) {  
    System.out.println("Can't open file:" + e.getMessage());  
} catch (IOException e) {  
    e.printStackTrace();  
} finally {  
if (null != br) {  
try {  
            br.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  

如果用 perl,只需要:

open(FILE, "test.txt") or die "Can't open file: $!\n";  
while (<FILE>) {  
    chomp;  
    print if /^http:/;  
}  

谈到 Perl 不能不说其独有的诗歌文化,因为语言的包容性、丰富的表达方式和灵活性,让 Perl 可以写出诗歌一样的代码。比如这首名为《Black Perl》的 Perl 诗歌,更多的请到 perlmonks 去找:

BEFOREHAND: close door, each window & exit;  wait until time.  
    open spellbook, study, read (scan, select, tell us);  
write it, print the hex while each watches,  
    reverse its length, write again;  
        kill spiders, pop them, chop, split, kill them.  
            unlink arms, shift, wait & listen (listening, wait),  
sort the flock (then, warn the "goats" & kill the "sheep");  
    kill them, dump qualms, shift moralities,  
         values aside, each one;  
             die sheep! die to reverse the system  
                  you accept (reject, respect);  
next step,  
    kill the next sacrifice, each sacrifice,  
         wait, redo ritual until "all the spirits are pleased";  
    do it ("as they say").  
do it(*everyone***must***participate***in***forbidden**s*e*x*).  
return last victim; package body;  
    exit crypt (time, times & "half a time") & close it,  
         select (quickly) & warn your next victim;  
AFTERWORDS: tell nobody.  
             wait, wait until time;  
                  wait until next year, next decade;  
                      sleep, sleep, die yourself,  
                           die at last  

如果一门语言能给你带来对设计和编码很多新的理解和体验,那么,不妨尝试一下它。

——————————————————————————————————————————–

2012-5-23:

Perl 有一个有名的框架是 Mason,perl 可以做很多事,脚本、胶水语言等等,Mason 则是一个用 perl 来实现页面模板的好例子。建议对 perl 有兴趣的同学,关注一下它。另外,不妨再有对比地了解一下 Embperl 这个框架(它能够对 HTML 标签具备语义识别的能力,有些奇葩)。

文章未经特殊标明皆为本人原创,未经许可不得用于任何商业用途,转载请保持完整性并注明来源链接 《四火的唠叨》

×Scan to share with WeChat

你可能也喜欢看:

  1. 寻找组成字母相同的单词
  2. 分析运行中的 Python 进程
  3. 青蛙跳台阶问题的三种解法
  4. 关于 if (someobject != null) 的问题
  5. Study Notes Of Mason

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

订阅·联系

四火,啰嗦的程序员一枚,现居西雅图

Amazon Google Groovy Hadoop Haskell Java JavaScript LeetCode Oracle Spark 互联网 亚马逊 前端 华为 历史 同步 团队 图解笔记 基础设施 工作 工作流 工具 工程师 应用系统 异步 微博 思考 技术 数据库 曼联 测试 生活 眼界 程序员 管理 系统设计 缓存 编程范型 美股 英语 西雅图 设计 问题 面向对象 面试

分类

  • Algorithm and Data Structure (30)
  • Concurrency and Asynchronization (6)
  • System Architecture and Design (43)
  • Distributed System (18)
  • Tools Frameworks and Libs (13)
  • Storage and Data Access (8)
  • Front-end Development (33)
  • Programming Languages and Paradigms (55)
  • Testing and Quality Assurance (4)
  • Network and Communication (6)
  • Authentication and Authorization (6)
  • Automation and Operation Excellence (13)
  • Machine Learning and Artificial Intelligence (6)
  • Product Design (7)
  • Hiring and Interviews (14)
  • Project and Team Management (14)
  • Engineering Culture (17)
  • Critical Thinking (25)
  • Career Growth (57)
  • Life Experience and Thoughts (45)

推荐文章

  • 聊一聊分布式系统中的时间
  • 谈谈分布式锁
  • 常见分布式系统设计图解(汇总)
  • 系统设计中的快速估算技巧
  • 从链表存在环的问题说起
  • 技术面试中,什么样的问题才是好问题?
  • 从物理时钟到逻辑时钟
  • 近期面试观摩的一些思考
  • RSA 背后的算法
  • 谈谈 Ops(汇总 + 最终篇):工具和实践
  • 不要让业务牵着鼻子走
  • 倔强的程序员
  • 谈谈微信的信息流
  • 评审的艺术——谈谈现实中的代码评审
  • Blog 安全问题小记
  • 求第 K 个数的问题
  • 一些前端框架的比较(下)——Ember.js 和 React
  • 一些前端框架的比较(上)——GWT、AngularJS 和 Backbone.js
  • 工作流系统的设计
  • Spark 的性能调优
  • “残酷” 的事实
  • 七年工作,几个故事
  • 从 Java 和 JavaScript 来学习 Haskell 和 Groovy(汇总)
  • 一道随机数题目的求解
  • 层次
  • Dynamo 的实现技术和去中心化
  • 也谈谈全栈工程师
  • 多重继承的演变
  • 编程范型:工具的选择
  • GWT 初体验
  • java.util.concurrent 并发包诸类概览
  • 从 DCL 的对象安全发布谈起
  • 不同团队的困惑
  • 不适合 Hadoop 解决的问题
  • 留心那些潜在的系统设计问题
  • 再谈大楼扔鸡蛋的问题
  • 几种华丽无比的开发方式
  • 我眼中的工程师文化
  • 观点的碰撞
  • 谈谈盗版软件问题
  • 对几个软件开发传统观点的质疑和反驳
  • MVC 框架的映射和解耦
  • 编程的未来
  • DAO 的演进
  • 致那些自嘲码农的苦逼程序员
  • Java 多线程发展简史
  • 珍爱生命,远离微博
  • 网站性能优化的三重境界
  • OSCache 框架源码解析
  • “ 你不适合做程序员”
  • 画圆画方的故事

近期评论

  • Ticket: TRANSACTION 1.922915 BTC. Go to withdrawal >> https://yandex.com/poll/enter/BXidu5Ewa8hnAFoFznqSi9?hs=20bd550f65c6e03103876b28cabc4da6& on 倔强的程序员
  • panshenlian.com on 初涉 ML Workflow 系统:Kubeflow Pipelines、Flyte 和 Metaflow
  • panzhixiang on 关于近期求职的近况和思考
  • Anonymous on 闲聊投资:亲自体验和护城河
  • 四火 on 关于近期求职的近况和思考
  • YC on 关于近期求职的近况和思考
  • mafulong on 常见分布式基础设施系统设计图解(四):分布式工作流系统
  • 四火 on 常见分布式基础设施系统设计图解(八):分布式键值存储系统
  • Anonymous on 我裸辞了
  • https://umlcn.com on 资源链接
© 2025 四火的唠叨 | Powered by Minimalist Blog WordPress Theme