Intro Modules

Embed Size (px)

Citation preview

  • 7/30/2019 Intro Modules

    1/30

    Introduction to Perl modules

    Simon Whitaker

    ([email protected])

    May 2003

  • 7/30/2019 Intro Modules

    2/30

    Introduction to Perl modules (SW) 2

    Evolving towards modularity

    In its simplest form, programming in a

    language like Perl involves writing lines of codethat execute sequentially:

    $name = "Alice";

    print "Hello $name!\n";

    # prints Hello Alice!

  • 7/30/2019 Intro Modules

    3/30

    Introduction to Perl modules (SW) 3

    Evolving towards modularity

    Writing line-by-line code works for simple tasks,

    but it can get unnecessarily repetitive:

    $name = "Alice";

    print "Hello $name!\n";

    $name2 = "Bob";

    print "Hello $name2!\n";

    $name3 = "Charlie";

    print "Hello $name3!\n";

  • 7/30/2019 Intro Modules

    4/30

    Introduction to Perl modules (SW) 4

    Evolving towards modularity

    Problems arise when I need to revise my code.

    For example, imagine that I need to translatemy program into French. I have to alter 3 linesof code:

    $name = "Alice";

    print "Bonjour $name!\n";

    $name2 = "Bob";

    print "Bonjour $name2!\n";$name3 = "Charlie";

    print "Bonjour $name3!\n";

  • 7/30/2019 Intro Modules

    5/30

    Introduction to Perl modules (SW) 5

    Evolving towards modularity

    Better to encapsulate the repetitive stuff in a

    subroutine, which I only have to amend once:

    sub greet {

    print "Hello $_[0]\n";

    }

    $name = "Alice";

    greet($name);$name2 = "Bob";

    greet($name2);

  • 7/30/2019 Intro Modules

    6/30

    Introduction to Perl modules (SW) 6

    Evolving towards modularity

    And in French...

    sub greet {

    print "Bonjour $_[0]\n";

    }

    $name = "Alice";

    greet($name);

    $name2 = "Bob";greet($name2);

  • 7/30/2019 Intro Modules

    7/30

    Introduction to Perl modules (SW) 7

    Evolving towards modularityI can now use greet() anywhere in my

    program, making my code easier to maintain.

    However, what if I want to use greet() in

    other programs? I can copy it into eachprogram, but then I have the same problem ofhaving to maintain multiple instances of thesame code.

    This is where modules come in.

  • 7/30/2019 Intro Modules

    8/30

    Introduction to Perl modules (SW) 8

    What is a module?

    A module is a library of Perl code that can be

    included in your Perl program.

    When you include a Perl module in a program,

    the functionality of that module is available foryou to use inside your own program.

  • 7/30/2019 Intro Modules

    9/30

    Introduction to Perl modules (SW) 9

    Where do modules come from?

    Perl modules come from a variety of sources:

    Standard modules: modules that are installedwhen you install Perl

    CPAN: the Comprehensive Perl Archive

    Network a global network of Perl modulerepositories

    Third parties: e.g. in-house code libraries,Bioperl, etc

  • 7/30/2019 Intro Modules

    10/30

    Introduction to Perl modules (SW) 10

    How to use a module

    To include a module in your program, use Perl's

    use keyword. The general form is:

    use Modu l e ;

    Typically, the module will export its mostpopular subroutines and variables into yourprogram. You can then use these subroutines

    and variables just as if they were declared inyour program.

  • 7/30/2019 Intro Modules

    11/30

    Introduction to Perl modules (SW) 11

    How to use a module

    For example:

    use Hello; # use the Hello module

    greet("Alice"); # greet() is exported

    # by the Hello module

  • 7/30/2019 Intro Modules

    12/30

    Introduction to Perl modules (SW) 12

    How to use a module

    To use subroutines or variables that aren't

    exported by default, precede their name withthe module name followed by a double colon:

    use Hello;

    Hello::greet("Alice");

    # calls greet() from the Hello module

  • 7/30/2019 Intro Modules

    13/30

    Introduction to Perl modules (SW) 13

    How to use a module

    Alternatively, you can state explicitly which

    variables and subroutines you want to exportwhen you load the module, using the generalform:

    us e Modul e qw( $v ar i ab l e s ubr out i ne ) ;

    where $variable and subroutine are the

    things you want to export. This form overridesthe default form only the variables andsubroutines you specify are imported.

  • 7/30/2019 Intro Modules

    14/30

    Introduction to Perl modules (SW) 14

    Example 1: Text::WrapText::Wrap is a simple but very useful module.

    It's part of the standard module library, whichmeans that it's installed when you install Perl.

    Text::Wrap contains a subroutine called

    wrap() (exported by default), which wrapstext to form neat paragraphs.

    wrap() takes three arguments: the indentstring for the first line of a paragraph, theindent string for subsequent lines, and the textto be wrapped.

  • 7/30/2019 Intro Modules

    15/30

    Introduction to Perl modules (SW) 15

    Example 1: Text::Wrapuse Text::Wrap;

    $indent_first = " ";

    $indent_subsq = "";

    $text = "For the life of me I couldnever understand why Mr Perkins didn'tenjoy his job. He had everything a mancould want: a place on the board, anabsent boss and a cat named Henry.";

    print wrap($indent_first,$indent_subsq, $text);

  • 7/30/2019 Intro Modules

    16/30

    Introduction to Perl modules (SW) 16

    Example 1: Text::Wrap

    Output:

    For the life of me I could

    never understand why Mr Perkins

    didn't enjoy his job. He had

    everything a man could want: a

    place on the board, an absent

    boss and a cat named Henry.

  • 7/30/2019 Intro Modules

    17/30

    Introduction to Perl modules (SW) 17

    Example 1: Text::Wrap

    Text::Wrap also has a variable called

    $columns, which determines how manycolumns my text wraps to. Unlikewrap(),$columns isn't exported from Text::Wrap by

    default, so I refer to it here using its fully

    qualified name:

    # start of program as before

    $Text::Wrap::columns = 20;print wrap($indent_first,

    $indent_subsq, $text);

  • 7/30/2019 Intro Modules

    18/30

    Introduction to Perl modules (SW) 18

    Example 1: Text::Wrap

    Output:

    For the life ofme I could never

    understand why Mr

    Perkins didn't

    enjoy his job. He

    had everything a

    man could want: a

    place on the board,an absent boss and

    a cat named Henry.

  • 7/30/2019 Intro Modules

    19/30

    Introduction to Perl modules (SW) 19

    3 benefits of using modules

    1. Saves time and effort

    2. Greater portability3. Modularity

  • 7/30/2019 Intro Modules

    20/30

    Introduction to Perl modules (SW) 20

    Benefit 1: Saves time and effort

    There are thousands of Perl modules out there,

    covering a huge range of common (and not socommon) tasks. Don't re-invent the wheel!

    If you need to grab data from a web page,create PDF files on the fly or connect to adatabase from your Perl program, I have greatnews for you someone else has already done

    the hard work. Better still, they'll share it withyou!

  • 7/30/2019 Intro Modules

    21/30

    Introduction to Perl modules (SW) 21

    Benefit 2: Greater portability

    Perl modules are generally written with

    portability in mind. Again let someone else dothe hard work!

    For example: imagine that you want to move afile from within your Perl program. Perl doesn'thave a built-in move function, so you might betempted to write:

    `mv oldfile newfile`;

  • 7/30/2019 Intro Modules

    22/30

    Introduction to Perl modules (SW) 22

    Benefit 2: Greater portability

    This works fine on UNIX. But if you try to run

    the program on Windows, or another operatingsystem that doesn't have amv command, it

    fails.

    This, however, is portable across OSs:

    use File::Copy;

    move("oldfile", "newfile");

  • 7/30/2019 Intro Modules

    23/30

    Introduction to Perl modules (SW) 23

    Benefit 3: Modularity

    As the name suggests, modules are modular!

    With modules you can parcel bits of code thatdo specific tasks (e.g. wrapping text) into

    discrete bundles, which can be re-usedelsewhere.

    If the code needs to be updated you only needto change the module, not dozens of Perlprograms.

    b f d b b

  • 7/30/2019 Intro Modules

    24/30

    Introduction to Perl modules (SW) 24

    A brief word about objects

    From time to time you'll come across object-

    oriented (OO) Perl modules.

    It's not important at this stage to understand

    much about OO programming or how it works,but it helps to be familiar with the syntax ituses so that you feel comfortable using OO Perlmodules.

    b f d b b

  • 7/30/2019 Intro Modules

    25/30

    Introduction to Perl modules (SW) 25

    A brief word about objects

    Objects represent complex entities, such as

    people, books or DNA sequences. Generally,you create objects using new:

    $alice = new Student();

    and interact with them using methods, whichwork just like subroutines:

    print $alice->gradeAverage();

    E l 2 N FTP

  • 7/30/2019 Intro Modules

    26/30

    Introduction to Perl modules (SW) 26

    Example 2: Net::FTP

    Net::FTP is an object-oriented Perl module that

    allows you to access FTP servers. As usual, youstart by importing the module:

    use Net::FTP;

    Then you create a new Net::FTP object:

    $ftp = new Net::FTP("ftp.mysite.com");

    E l 2 N FTP

  • 7/30/2019 Intro Modules

    27/30

    Introduction to Perl modules (SW) 27

    Example 2: Net::FTP

    Now you can interact with your new object:

    $ftp->login("username", "password");

    $ftp->cwd("/pub");

    $ftp->get("mrperkins.doc");

    $ftp->quit();

    M d l d t ti

  • 7/30/2019 Intro Modules

    28/30

    Introduction to Perl modules (SW) 28

    Module documentationWhen you install Perl modules, you get access

    to documentation for that module.

    To read the documentation for a module, usethe perldoc command:

    $ perldoc File::Copy

    Alternatively, on Unix you can use the mancommand. On Windows you will have HTMLdocumentation available there's a link in theStart menu.

    F th di

  • 7/30/2019 Intro Modules

    29/30

    Introduction to Perl modules (SW) 29

    Further reading

    For the official low-down on modules:

    $ perldoc perlmod

    For a list of standard modules:

    $ perldoc perlmodlib

    To read in the bath: Learning Perl Objects,References & Modules by Randal L. Schwartzwith Tom Phoenix. June 2003 (est.)

    S

  • 7/30/2019 Intro Modules

    30/30

    Introduction to Perl modules (SW) 30

    Summary

    Modules are libraries of code that you can

    insert into your Perl programs Modules save you time and effort, by

    providing easy access to common tasks

    Use the perldoc command to getdocumentation on a Perl module

    Search CPAN for general-purpose modules