版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 老年人日常护理技巧
- 隆鼻手术恢复期护理细节
- 护理精神科护理特殊性与挑战
- 护理中的疼痛管理与缓解策略
- 大丰市小海中学高中化学检测期末串讲(下)
- 2025年保险合作协议书
- 垂直市场广告竞争策略研究
- 2026 年中职康复治疗技术(柔韧性训练)试题及答案
- 英语b级的试题及答案
- 基于“大单元”视角的高中思政课教学策略研究
- 跨区域文化协作-洞察及研究
- 2025 易凯资本中国健康产业白皮书 -生物制造篇(与茅台基金联合发布)
- 产业经济学(苏东坡版)课后习题及答案
- T/CECS 10227-2022绿色建材评价屋面绿化材料
- 区域医学检验中心项目建设方案
- 小学四年级安全教育上册教学计划小学四年级安全教育教案
- 个人优势与劣势分析
- VCR接头锁紧工作程序
- 2025阀门装配工艺规程
- 非计划拔管风险评估及护理
- 小学数学教学中融入中国传统文化的实践研究
评论
0/150
提交评论