1 Writing Perl 5 Modules: An Introduction Keith Arner.

Slides:



Advertisements
Similar presentations
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Programming Logic and Design Fourth Edition, Introductory
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Guide To UNIX Using Linux Third Edition
General Algorithms for Common Business Problems
Chapter 1 Program Design
Introduction to C Programming
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Neal Stublen Overview of.NET Windows Applications Microsoft Windows OS / Intel Platform Windows Application File SystemNetworkDisplay.
Neal Stublen Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning.
Lecture 9 Polymorphism Richard Gesick.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Topic 11: Object-oriented Perl CSE3395 Perl Programming Camel3 chapter 12, pages perlobj, perltoot, perlbot, perlmod manpages.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
How to write & use Perl Modules. What is a Module? A separate Namespace in a separate file with related functions/variables.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CS212: Object Oriented Analysis and Design
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSE S. Tanimoto Perl-Intro - 1 Perl Practical Extraction and Report Language Shell-level scripting language: Glue that ties together programs written.
1 JavaScript 2.0: Evolving a Language for Evolving Systems Waldemar Horwat Netscape.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Lecture 7 Conditional Scripting and Importing/Exporting.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Introduction to Object Oriented Programming Chapter 10.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 Using Perl Modules. 2 What are Perl modules?  Modules are collections of subroutines  Encapsulate code for a related set of processes  End in.pm.
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
CS 360 Perl Part 2. Remember  Assignments are Due Today  Make a web page for all of your assignments  You get to design it so it works well for you.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Character (String) Data
Perl Modules.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Chapter 6 - Subroutines and Functions
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Week 4 Lecture-2 Chapter 6 (Methods).
Presentation transcript:

1 Writing Perl 5 Modules: An Introduction Keith Arner

2 Outline What do we mean by Modules and Classes? Module Basics –Using Modules, Packages, Special Variables, Exporting Functions Advanced Concepts –Autoloading, More Exporting, Versioning Ask questions at any time

3 Topics Not Covered Objects Modules available on CPAN Writing modules for CPAN distribution Each of these would make a good topic for a future talk (Any volunteers?)

4 Modules and Classes There are two types of modularization under Perl 5 –Modules : library functions –Classes : object-oriented programming

5 What is a Module? A module is a.pm file that defines a library of related functions Modules are conceptually similar to old- fashioned Perl libraries (.pl files), but have a cleaner implementation –selective namespace cluttering –simpler function invocation

6 How to use a Module Time::Local use Time::Local; $time = timelocal($sec, $min, $hours, $mday, $mon, $year); print "Seconds since epoch: $time\n";

7 Module Basics Learn by Example: MyRand.pm Refer to attached printout of MyRand.pm Note that line numbers are for reference purposes only

8 Package Names and Filenames Package name is declared on line 1 This should be the same as the filename, without the.pm extension If it is different, your functions will not be exported correctly Should begin with a capital letter to avoid possible conflict with pragmas

9 More on Package Names package MyRand is file MyRand.pm package Time is file Time.pm package Time::Local is file Time/Local.pm They have nothing to do with one another

10 The Package: Your Own Private Namespace Each package is a separate namespace for functions and variables There is no collision of names between namespaces $seed in the package main and $seed in the package MyRand are distinct variables

11 and $VERSION have special tells Perl what functions you want to make available to the tells Perl how to export the functions (I won't go into the details) $VERSION is used to indicate compatibility

12 Exporting Functions Line 11 to contain a list of the names of functions to be exported Note that we use qw() to generate the list –This is just syntactic sugar = ('srand', 'rand');

13 What does it mean to Export? Your functions are defined in your own package Exporting makes the functions available in another package as if they were defined in the other package This is done by fiddling with symbol tables (which is a topic for another talk)

14 A Function is a Function is a Function On lines 19 and 23, functions are declared and written exactly as they would be anywhere else

15 Module Specific State Line 15 declares a variable local to the module that can maintain it's state between function calls $seed is visible to all functions in this file $seed does not conflict with $seed in any other module, nor in a mainline program

16 Declaring Victory All Perl modules must return a true value to indicate that they completed successfully Line 35 shows the canonical return Any true value will do: –'true' – –$^O & $/ –(3..99)

17 How do you use it? Make sure your module is in your include This can be augmented with -I or your PERL5LIB environment variable use MyRand; srand(5); print rand;

18 Advanced Topics Autoloading Selective Exporting Versioning Much, much more

19 Autoloading The autoloader is used to improve performance of large modules (such as POSIX.pm ) Functions are split into separate files and compiled only as needed AutoSplit.pm is used to split modules

20 More can export variables in addition to functions: = qw(rand srand and %EXPORT_TAGS can be used to export selectively

21 Selective Exporting It is not always desirable to export everything, is = qw (rand = qw ($seed); use MyRand qw(rand $seed);

22 Fancy Exporting Typing all the names of symbols to import can become a hassle, so %EXPORT_TAGS is = qw (rand = qw ($seed); %EXPORT_TAGS = ( funcs => [qw(rand srand)], vars => [qw($seed)] ); use MyRand qw(:funcs :vars);

23 Versioning $VERSION is used to indicate the version of the module use MyRand 2.0; –will fail if $MyRand::VERSION < 2.0 Can be used with RCS Keywords: –$VERSION =('$REVISION 1.0$' =~ m/Revision ([^\$]+)/);

24 Much, much more Overriding builtin functions Cascading inheritance Automated Cleanup Object Oriented Programming

25 References Programming Perl, Second Edition. Wall, Christiansen & Schwartz –Chapters 5 and 7 Man pages –perlmod(1) Perl Cookbook. Christiansen & Torkington –Chapter 12

26 Summary Modules are libraries of functions A simple module just exports a set of functions Perl modules can be expanded in many directions for arbitrarily sophisticated libraries