版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Writing Perl 5 Modules:An IntroductionKeith Arner1OutlineWhat do we mean by Modules and Classes?Module BasicsUsing Modules, Packages, Special Variables, Exporting FunctionsAdvanced ConceptsAutoloading, More Exporting, VersioningAsk questions at any time2Topics Not CoveredObjectsModules available on
2、CPANWriting modules for CPAN distributionEach of these would make a good topic for a future talk (Any volunteers?)3Modules and ClassesThere are two types of modularization under Perl 5Modules : library functionsClasses : object-oriented programming4What is a Module?A module is a .pm file that define
3、s a library of related functionsModules are conceptually similar to old-fashioned Perl libraries (.pl files), but have a cleaner implementationselective namespace clutteringsimpler function invocation5How to use a Module Time:Localuse Time:Local;$time = timelocal($sec, $min, $hours, $mday, $mon, $ye
4、ar);print Seconds since epoch: $timen;6Module BasicsLearn by Example: MyRand.pmRefer to attached printout of MyRand.pmNote that line numbers are for reference purposes only7Package Names and FilenamesPackage name is declared on line 1This should be the same as the filename, without the .pm extension
5、If it is different, your functions will not be exported correctlyShould begin with a capital letter to avoid possible conflict with pragmas8More on Package Namespackage MyRandis file MyRand.pmpackage Timeis file Time.pmpackage Time:Localis file Time/Local.pmThey have nothing to do with one another9T
6、he Package: Your Own Private NamespaceEach package is a separate namespace for functions and variablesThere is no collision of names between namespaces$seed in the package main and $seed in the package MyRand are distinct variables10Magic VariablesEXPORT, ISA and $VERSION have special meaningsEXPORT
7、 tells Perl what functions you want to make available to the publicISA tells Perl how to export the functions (I wont go into the details)$VERSION is used to indicate compatibility11Exporting FunctionsLine 11 sets EXPORT to contain a list of the names of functions to be exportedNote that we use qw()
8、 to generate the listThis is just syntactic sugar for:EXPORT = (srand, rand);12What does it mean to Export?Your functions are defined in your own packageExporting makes the functions available in another package as if they were defined in the other packageThis is done by fiddling with symbol tables
9、(which is a topic for another talk)13A Function is a Functionis a FunctionOn lines 19 and 23, functions are declared and written exactly as they would be anywhere else14Module Specific StateLine 15 declares a variable local to the module that can maintain its state between function calls$seed is vis
10、ible to all functions in this file$seed does not conflict with $seed in any other module, nor in a mainline program15Declaring VictoryAll Perl modules must return a true value to indicate that they completed successfullyLine 35 shows the canonical returnAny true value will do:true3.1415926535$O & $/
11、(3.99)16How do you use it?Make sure your module is in your include path, INCThis can be augmented with -I or your PERL5LIB environment variableuse MyRand;srand(5);print rand;17Advanced TopicsAutoloadingSelective ExportingVersioningMuch, much more18AutoloadingThe autoloader is used to improve perform
12、ance of large modules (such as POSIX.pm)Functions are split into separate files and compiled only as neededAutoSplit.pm is used to split modules19More ExportingEXPORT can export variables in addition to functions:EXPORT = qw(rand srand $seed)EXPORT_OK and %EXPORT_TAGS can be used to export selective
13、ly20Selective ExportingIt is not always desirable to export everything, so EXPORT_OK is usedEXPORT = qw (rand srand);EXPORT_OK = qw ($seed);use MyRand qw(rand $seed);21Fancy ExportingTyping all the names of symbols to import can become a hassle, so %EXPORT_TAGS is usedEXPORT = qw (rand srand);EXPORT
14、_OK = qw ($seed);%EXPORT_TAGS = (funcs = qw(rand srand),vars = qw($seed) );use MyRand qw(:funcs :vars);22Versioning$VERSION is used to indicate the version of the moduleuse MyRand 2.0;will fail if $MyRand:VERSION 2.0Can be used with RCS Keywords:$VERSION =($REVISION 1.0$ =m/Revision ($+)/);23Much, much moreOverriding builtin functionsCascading inheritance with ISAAutomated CleanupObject Oriented Programming24ReferencesProgramming Perl, Second Edition. Wall, Christiansen & SchwartzChapters 5 and 7Man pagesperlmod
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 20870.7-2026半导体器件第16-7部分:微波集成电路衰减器
- T/ZEA 002-2024电子商务交易产品质量监测抽样规范
- T/CAAMTB 176-2023汽车发动机 活塞组件 产品质量特性及重要度分级
- T/JALNCP 5701-2024井冈山香芹
- T/SZBX 141-2024“苏州大米”质量分级规范
- T/CEPPC 16-2023企业碳效分析及碳信用评价规范
- T/HZBX 062-2023曲安奈德苯海拉明水杨酸酊主要成分的含量测定 高效液相色谱法
- T/CI 220-2023高精度三维测头
- T/JSJTQX 60-2024港口散杂货企业应急能力评估指南
- T/HEVCA 4-2024电动汽车充电桩僵尸桩及故障桩评判与处理方法
- 2026年高级考评员职业技能等级认定考试题库含答案
- 2026新教材数学 小学数学人教版三年级上2.4混合运算解决问题 教学课件
- 2025年湖南能源集团招聘考试笔试试题(含答案)
- 冀少版八年级上册生物第五单元第一章第三节《芽的发育》教学课件(新教材)
- 2026年云南省政府采购评审专家考试卷附答案
- GB/T 1685.1-2026硫化橡胶或热塑性橡胶压缩应力松弛的测定第1部分:恒定温度下试验
- 职业卫生技术服务专业技术人员考试(职业卫生评价)例题及答案(北京市)
- 2026-2030中国高纯碲化锌市场深度调查与前景预测分析研究报告
- 2026四川成都市公共交通集团有限公司招聘储备人才等岗位备考题库附答案详解(培优)
- 2026年大数据智慧环保监测平台建设可行性方案
- 2026年福建社区工作者考试真题及答案
评论
0/150
提交评论