Getting Organised Subroutines, modules and the wonder of CPAN.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Painless Perl Modules Fernando J. Pineda Biostat computing club 2013/2/21.
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
The Basics Getting started with Perl for Bioinformatics programming.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Practical Extraction & Report Language Picture taken from
Databases and Perl Using Perl to talk to databases.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
– Intermediate Perl 8/27/ Intermediate Perl - modules Intermediate Perl – Session 5 · modules · CPAN.
subroutines and references
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
E0001 Computers in Engineering Procedures: subprograms and functions.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
1 Using Modules. 2 Modules A Perl module is a collection of subroutines and variables that typically implements some common functionality – and "packaged"
Introduction to the Windows API n API - Application Programming Interface n an API is the software interface for things such as the OS n an API is the.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Box 1Box 4Box 3Box 2 Box 5Box 8Box 7Box 6 My Shadow Box.
Perl (Tutorial II) NLP Course 07 Examples. EXAMPLE 1 #!/usr/bin/perl #Opens and reads the file "sentences" (input file) line by line. #For the i-th sentence.
Perl Grabbag Some useful bits'n'pieces that every Perl programmer should know.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
96-Summer 生物資訊程式設計實習 ( 二 ) Bioinformatics with Perl 8/13~8/22 蘇中才 8/24~8/29 張天豪 8/31 曾宇鳯.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
How to write & use Perl Modules. What is a Module? A separate Namespace in a separate file with related functions/variables.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Perl II Part III: Motifs and Loops. Objectives Search for motifs in DNA or Proteins Interact with users at the keyboard Write data to files Use loops.
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
An Overview of Perl A language for Systems and Network Administration and Management: An overview of the language.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Function Basics. Function In this chapter, we will move on to explore a set of additional statements that create functions of our own function (subroutine,
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
Intro to Object Oriented Perl Packages, Modules, Classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
Introducing Bioperl Toward the Bioinformatics Perl programmer's nirvana.
Advanced Perl For Bioinformatics Part 1 2/23/06 1-4pm Module structure Module path Module export Object oriented programming Part 2 2/24/06 1-4pm Bioperl.
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.
Perl Subroutines User Input Perl on linux Forks and Pipes.
Java Programming Language Lecture27- An Introduction.
Lecture 6.11
Lecture 7 Methods (functions and subroutines) Parameter Passing
Lecture 9: Basic concepts of Perl Modules. Functions (Subs) In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements.
Introduction to Bioinformatic Computation. Lecture
Functions and Procedures
Input, Output and Other Things
Introduction to Bioinformatic Computation. Lecture
Extending Languages.
Chapter 8 - Functions and Functionality
Presentation transcript:

Getting Organised Subroutines, modules and the wonder of CPAN

print '-' x LINE_LENGTH, "\n"; print "=" x LINE_LENGTH, "\n"; print "-o0o-" x 12, "\n"; print "- " x 30, "\n"; print ">>==<<==" x 8, "\n"; ============================================================ -o0o--o0o--o0o--o0o--o0o--o0o--o0o--o0o--o0o--o0o--o0o--o0o >>== >== >== >== >== >== >== >==<<== Named Blocks

Introducing Subroutines

Maxim 5.1 Whenever you think you will reuse some code, create a subroutine

drawline "=", REPEAT_COUNT; drawline( "=", REPEAT_COUNT ); drawline; drawline(); Calling Subroutines

drawline find_a_sequence convert_data my_subroutine sub1 tempsub Naming Subroutines

sub drawline { } sub drawline { } sub drawline { } sub drawline { } Creating Subroutines

sub drawline { print "-" x REPEAT_COUNT, "\n"; } The drawline Subroutine

#! /usr/bin/perl -w # first_drawline - the first demonstration program for "drawline". use constant REPEAT_COUNT => 60; sub drawline { print "-" x REPEAT_COUNT, "\n"; } print "This is the first_drawline program.\n"; drawline; print "Its purpose is to demonstrate the first version of drawline.\n"; drawline; print "Sorry, but it is not very exciting.\n"; Using drawline

This is the first_drawline program Its purpose is to demonstrate the first version of drawline Sorry, but it is not very exciting. Results from first_drawline...

print "$_[0]"; # The first parameter. print "$_[1]"; # The second parameter. print "$_[2]"; # The third parameter, and so on. sub drawline { print $_[0] x $_[1], "\n"; } drawline; drawline "-", REPEAT_COUNT; drawline( "-", REPEAT_COUNT ); drawline "=", REPEAT_COUNT; drawline( "-o0o-", 12 ); drawline "- ", 30; drawline( ">>==<<==", 8 ); Processing parameters

#! /usr/bin/perl -w # second_drawline - the second demonstration program for "drawline". use constant REPEAT_COUNT => 60; sub drawline { print $_[0] x $_[1], "\n"; } print "This is the second_drawline program.\n"; drawline "-", REPEAT_COUNT; print "Its purpose is to demonstrate the second version of drawline.\n"; Using the new drawline

drawline "-", REPEAT_COUNT; print "Sorry, but it is still not exciting. However, it is more useful.\n"; drawline "=", REPEAT_COUNT; drawline "-o0o-", 12; drawline "- ", 30; drawline ">>==<<==", 8; The second_drawline program, cont.

This is the second_drawline program Its purpose is to demonstrate the second version of drawline Sorry, but it is still not exciting. However, it is more useful. ============================================================ -o0o--o0o--o0o--o0o--o0o--o0o--o0o--o0o--o0o--o0o--o0o--o0o >>== >== >== >== >== >== >== >==<<== Results from second_drawline...

Better processing of parameters shift; # Or like this: shift(); sub drawline { print shift() x shift(), "\n"; } sub drawline { $chars = shift || "-"; $count = shift || REPEAT_COUNT; print $chars x $count, "\n"; }

drawline "=== ", 10; drawline; drawline "="; # Prints sixty equal signs. drawline "*"; # Prints sixty stars. drawline "$"; # Prints sixty dollars. drawline 40; # Does NOT print forty dashes! drawline 20, "-"; # Does NOT print twenty dashes! Using the new drawline...

#! /usr/bin/perl -w # fourth_drawline - the fourth demonstration program for "drawline". use constant REPEAT_COUNT => 60; sub drawline { $chars = shift || "-"; $count = shift || REPEAT_COUNT; print $chars x $count, "\n"; } The fourth_drawline program

print "This is the fourth_drawline program.\n"; drawline; print "Its purpose is to demonstrate the fourth version of drawline.\n"; drawline; print "Sorry, but it is still not exciting. However, it is more useful.\n"; drawline "=", REPEAT_COUNT; drawline "-o0o-", 12; drawline "- ", 30; drawline ">>==<<==", 8; The fourth_drawline program, cont.

drawline; drawline( Pattern => "*" ); drawline( Count => 20 ); drawline( Count => 5, Pattern => " -oOo- " ); drawline( Pattern => "===", Count => 10 ); Even better processing of parameters

The Default With Assigned Values insert array here

The %arguments Hash, With Assigned Values insert hash here

%arguments $chars = $arguments{ Pattern } || "-"; $count = $arguments{ Count } || REPEAT_COUNT; sub drawline { $chars = $arguments{ Pattern } || "-"; $count = $arguments{ Count } || REPEAT_COUNT; print $chars x $count, "\n"; } Processing Named Parameters

#! /usr/bin/perl -w # fifth_drawline - the fifth demonstration program for "drawline". use constant REPEAT_COUNT => 60; sub drawline { %arguments $chars = $arguments{ Pattern } || "-"; $count = $arguments{ Count } || REPEAT_COUNT; print $chars x $count, "\n"; } The fifth_drawline program

print "This is the fifth_drawline program.\n"; drawline; print "Its purpose is to demonstrate the fifth version of drawline.\n"; drawline; print "Things are getting a little more interesting.\n"; drawline( Pattern => "*" ); drawline( Count => 20 ); drawline( Count => 5, Pattern => " -oOo- " ); drawline( Pattern => "===", Count => 10 ); drawline; The fifth_drawline program, cont.

This is the fifth_drawline program Its purpose is to demonstrate the fifth version of drawline Things are getting a little more interesting. ************************************************************ oOo- -oOo- -oOo- -oOo- -oOo- ============================== Results from fifth_drawline...

| print "+"; drawline( Count => 15 ); print "+"; A more flexible drawline subroutine

print "+"; drawline( Count => 15 ); print "+\n"; print "+", drawline( Count => 15 ), "+\n"; Not Quite Right!

Returning results sub drawline { %arguments $chars = $arguments{ Pattern } || "-"; $count = $arguments{ Count } || REPEAT_COUNT; return $chars x $count; }

#! /usr/bin/perl -w # boxes - the box drawing demonstration program for "drawline". use constant REPEAT_COUNT => 15; sub drawline { %arguments $chars = $arguments{ Pattern } || "-"; $count = $arguments{ Count } || REPEAT_COUNT; return $chars x $count; } The boxes program

print "+", drawline, "+\n"; print "|", drawline( Pattern => " " ), "|\n"; print "+", drawline, "+\n"; The boxes program, cont.

Maxim 5.2 When determining the scope of a variable, think about its visibility

#! /usr/bin/perl -w # global_scope - the effect of "global" variables. sub adjust_up { $other_count = 1; print "count at start of adjust_up: $count\n"; $count++; print "count at end of adjust_up: $count\n"; } $count = 10; print "count in main: $count\n"; adjust_up; print "count in main: $count\n"; print "other_count in main: $other_count\n"; Visibility and Scope

count in main: 10 count at start of adjust_up: 10 count at end of adjust_up: 11 count in main: 11 other_count in main: 1 Results from global_scope...

#! /usr/bin/perl # private_scope - the effect of "my" variables. sub adjust_up { my $other_count = 1; print "count at start of adjust_up: $count\n"; $count++; print "other_count within adjust_up: $other_count\n"; print "count at end of adjust_up: $count\n"; } my $count = 10; print "count in main: $count\n"; adjust_up; print "count in main: $count\n"; print "other_count in main: $other_count\n"; Using private variables

count in main: 10 count at start of adjust_up: other_count within adjust_up: 1 count at end of adjust_up: 1 count in main: 10 other_count in main: Results from private_scope...

Maxim 5.3 Unless you have a really good reason not to, always declare your variables with my

#! /usr/bin/perl # hybrid_scope - the effect of "our" variables. sub adjust_up { my $other_count = 1; print "count at start of adjust_up: $count\n"; $count++; print "other_count within adjust_up: $other_count\n"; print "count at end of adjust_up: $count\n"; } our $count = 10; print "count in main: $count\n"; adjust_up; print "count in main: $count\n"; print "other_count in main: $other_count\n"; Using global variables properly

count in main: 10 count at start of adjust_up: 10 other_count within adjust_up: 1 count at end of adjust_up: 11 count in main: 11 other_count in main: Results from hybrid_scope...

Maxim 5.4 If you must use a global variable, declare it with our

sub drawline { my %arguments my $chars = $arguments{ Pattern } || "-"; my $count = $arguments{ Count } || REPEAT_COUNT; return $chars x $count; } The final version of drawline

$ man perlfunc $ perldoc -f sleep In-Built Subroutines

Grouping and Reusing Subroutines

Maxim 5.5 When you think you will reuse a subroutine, create a custom module

#! /usr/bin/perl -w use lib "$ENV{'HOME'}/bbp/"; use UsefulUtils; drawline; Modules

package; require Exporter; = qw( Exporter ); = qw(); = qw(); our %EXPORT_TAGS = (); our $VERSION = 0.01; 1; Modules – Example Template

package UsefulUtils; # UsefulUtils.pm - the useful utilities module from # "Bioinformatics, Biocomputing and Perl". require Exporter; = qw( Exporter ); = qw(); = qw( drawline ); our %EXPORT_TAGS = (); our $VERSION = 0.01; use constant REPEAT_COUNT => 60; The UsefulUtils Module

sub drawline { # Given: a character string and a repeat count. # Return: a string that contains the character string # "repeat count" number of times. # # Notes: For maximum flexibility, this routine does NOT # include a newline ("\n") at the end of the line. my %arguments my $chars = $arguments{ Pattern } || "-"; my $count = $arguments{ Count } || REPEAT_COUNT; return $chars x $count; } 1; The UsefulUtils Module, cont.

$ mkdir ~/bbp/ $ cp UsefulUtils.pm ~/bbp/ Installing UsefulUtils

#! /usr/bin/perl -w # boxes2 - the box drawing demonstration program for "drawline". use lib "$ENV{'HOME'}/bbp/"; use UsefulUtils qw( drawline ); print "+", drawline( Count => 15 ), "+\n"; print "|", drawline( Pattern => " ", Count => 15 ), "|\n"; print "+", drawline( Count => 15 ), "+\n"; Using UsefulUtils

$ man perlmodlib The Standard Modules

Maxim 5.6 Don't reinvent the wheel, use or extend a standard module whenever possible

CPAN: The Module Repository

Maxim 5.7 Don't reinvent the wheel, use or extend a CPAN module whenever possible

Maxim 5.8 When you think others might benefit from a custom module you have written, upload it to CPAN

Searching CPAN

$ tar zxvf ExampleModule-0.03.tar.gz $ cd ExampleModule-0.03 $ perl Makefile.PL $ make $ make test $ su $ make install $ Installing a CPAN module manually

$ man ExampleModule $ perl -e 'use ExampleModule' # Can't locate ExampleModule.pm # BEGIN failed--compilation aborted at -e line 1. Testing the installation

$ perl -MCPAN -e "install 'ExampleModule'" Installing a CPAN module automatically

Maxim 5.9 Always take the time to test downloaded CPAN modules for compliance to specific requirements

Where To From Here