Using Libraries - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Using Libraries

Description:

Using Libraries – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 26
Provided by: tabr1
Category:

less

Transcript and Presenter's Notes

Title: Using Libraries


1
Using Libraries
2
To or not to Function(perldoc perlsub)
  • A subroutine may be called using an explicit ""
    prefix. The "" is
  • optional in modern Perl, as are
    parentheses if the subroutine has been
  • predeclared. The "" is not optional when
    just naming the subroutine,
  • such as when its used as an argument to
    defined() or undef(). Nor is
  • it optional when you want to do an
    indirect subroutine call with a
  • subroutine name or reference using the
    "subref()" or "subref()"
  • constructs, although the "subref-gt()"
    notation solves that problem.
  • See perlref for more about all that.
  • Subroutines may be called recursively. If
    a subroutine is called using
  • the "" form, the argument list is
    optional, and if omitted, no _at__
  • array is set up for the subroutine the _at__
    array at the time of the
  • call is visible to subroutine instead.
    This is an efficiency mechanism
  • that new users may wish to avoid.

3
-- continued
  • foo(1,2,3) pass three
    arguments
  • foo(1,2,3) the same
  • foo() pass a null list
  • foo() the same
  • foo foo() get
    current args, like foo(_at__) !!
  • foo like foo() IFF
    sub foo predeclared, else "foo"
  • Not only does the "" form make the
    argument list optional, it also
  • disables any prototype checking on
    arguments you do provide. This is
  • partly for historical reasons, and partly
    for having a convenient way
  • to cheat if you know what youre doing.
    See Prototypes below.

4
Sharing Code
  • Another advantage is that programmers can reuse
    my subroutines, and vice-versa.
  • "Bob" writes a subroutine called
    "reverse_complement", and some other subroutines,
    and stores these in a file called
    "BseqProcess.pl"
  • I can use "reverse_complement" with
  • do "BseqProcess.pl"
  • Code is brought in from separate files
  • 1) easier to maintain
  • 2) permits inter-programmer cooperation

5
Using "do"
  • Suppose that "Bob" used my "print_formatted_sequen
    ce" by using "do sequence_pro.pl" in
    "BseqProcess.pl"
  • !/usr/bin/perl
  • BseqProcess.pl
  • do sequence_pro.pl
  • sub reverse_complement
  • Then I also use a "do sequence_pro.pl" -- these
    subroutines will be pulled in twice (and generate
    a warning with w)
  • !/usr/bin/perl
  • my_program
  • do BseqProcess.pl
  • do sequence_pro.pl done twice, since done in
    BseqProcess.pl too

6
require
  • "require" is a mechanism that tracks what files
    have been brought in and brings them in only
    once.
  • require "sequence_pro.pl"
  • require "BseqProcess.pl"
  • Note last expression evaluated in the file must
    return a true value thus most files evaluated
    for "require" have a cryptic "1" as the last
    line. This is simply an annoying relic that was
    never removed from the language.
  • IS this in the module book????
  • SHOW formated_seq.pl

7
formatted_seq.pl
  • !/usr/bin/perl
  • formatted_seq.pl
  • require 'sequence_pro.pl'
  • seq "ATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTAccc
    ccccccccccccccccccccccccccccccccccccccccccccccccgg
    gggggggggggggggggggggggggggggggggggggggggggggggggg
    ggggggggggggggggggggggggggggggggggggggggggggggggg"
  • print_formatted_sequence(seq,50,10)

8
sequence_pro.pl
  • sequence_pro.pl
  • sub print_formatted_sequence
  • if(_!2)
  • die("Error in Seqprint_formatted_sequence.
    Expecting 3 parameters\n")
  • my (seq,line_length,seq_length) _at__
  • seq_length block length -- bad choice of
    variable name
  • example line_length 20, seq_length 5
  • AAAAA AAAAA AAAAA AAAAA
  • my front 1
  • my back line_length
  • remainder seq
  • while(length(remainder)gtline_length)
  • _ remainder
  • (format,remainder) m/(.line_length)(.)/
    match a line, then the rest
  • format 1, and remainder 2
  • format s/(.seq_length)/1 /g match for
    10, then insert a space
  • print "front format back\n"
  • front front line_length
  • back back line_length
  • format remainder
  • format s/(.seq_length)/1 /g
  • back front length(remainder)-1
  • print "front format back\n"
  • 1

9
Libraries and Programs
It just works
A better way to organize code.
/home/tabraun/proj
/home/tabraun/proj
tlate.pl
sequence_pro.pl tlate.pl Seq.pm SeqIO.pm BPlite.p
m
/home/tabraun/perl/local/lib sequence_pro.pl
/home/tabraun/perl/bioperl-0.7.1/ Seq.pm SeqIO.pm
BPlite.pm
How will perl know to look in /local/lib and
bioperl-0.7.1 ????
10
_at_INC
  • Terminology
  • main code (program) the program that calls
    subroutines
  • included files (library) collection of shared
    subroutines, software library
  • What is the directory structure of the "main
    code" and the "included files"?
  • It "just works" for the simplest case, in which
    the program and the "libraries" are in the same
    directory, and you run the program from that
    directory.
  • Its not practical to have all shared libraries in
    the "current working directory" ideally these
    would be organized into specific subdirectories.
  • Perl, searches for libraries along a "library
    search path", similar to what UNIX "shells" do
    with the PATH environment variable.
  • The current directory is included in this "search
    path", so as long as your libraries are in the
    current working directory, it "just works."

11
_at_INC
  • the "search path" is given in a special variable
    called _at_INC
  • perl V
  • (lots of stuff)
  • _at_INC
  • /home/tabraun/perl/bioperl-live//ensembl/modul
    es
  • /home/tabraun/perl/bioperl-live//bioperl-0.7
  • /home/tabraun/perl/bioperl-live//ensembl-exter
    nal/modules
  • /home/tabraun/perl/bioperl-live//ensembl-lite/
    modules
  • /usr/lib/perl5/5.8.0/i386-linux-thread-multi
  • /usr/lib/perl5/5.8.0
  • /usr/lib/perl5/site_perl/5.8.0/i386-linux-thre
    ad-multi
  • /usr/lib/perl5/site_perl/5.8.0
  • /usr/lib/perl5/site_perl
  • /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-th
    read-multi
  • /usr/lib/perl5/vendor_perl/5.8.0
  • /usr/lib/perl5/vendor_perl
  • /usr/lib/perl5/5.8.0/i386-linux-thread-multi
  • /usr/lib/perl5/5.8.0

12
Extending _at_INC Programatically
  • typically, users cannot alter the content of
    default directories in _at_INC
  • _at_INC is an ordinary array
  • may add libraries by modifying _at_INC
  • unshift _at_INC, "/home/tabraun/perl/local/lib"
  • For this program, perl will search (first) in
    this directory -- Why?

13
Extending _at_INC on the System
  • Recall my "100" programs that used
    "sequence_pro.pl"
  • To programatically add "/home/tabraun/perl/local/l
    ib" to each one would be 100 different edits
  • Alternatively, a system variable may be set to
    tell perl where to look (so 100 programs don't
    have to be changed).
  • env (tells me what my shell is)
  • csh
  • setenv PERL5LIB /home/tabraun/perl/local/lib
  • BASH
  • PERL5LIB/home/tabraun/perl/local/lib export
    PERL5LIB
  • Advantage set it once, and forget it
  • Disadvantage
  • someone else wanting to use your library would
    need the same variable set

14
-I (Include)
  • Yet another way
  • perl I/home/tabraun/perl/local/lib
    /home/bob/perl-program

15
Namespace Collisions
  • Terry has created sequence_pro.pl
  • Bob has created BseqProcess.pl that has
    incorporated sequence_pro.pl
  • !/usr/bin/perl
  • BseqProcess.pl
  • require "sequence_pro.pl"
  • sub reverse_complement
  • print_formatted_sequence(seq,50) Terry's sub
  • Everything works fine.
  • Now I modify sequence_pro.pl, and add a
    subroutine called reverse_complement (which is
    already in BseqProcess.pl Bob's)

16
Namespace Collision Solutions
  • One particularly BAD solution, is for Terry to
    add a prefix to every scalar, subroutine, array,
    and hash, such as "sequence_pro_"
  • Every reference to a subroutine/variable in
    "sequence_pro" would have this prefix
    "sequence_pro_
  • Now Bob's looks like
  • !/usr/bin/perl
  • BseqProcess.pl
  • require "sequence_pro.pl"
  • sub reverse_complement
  • sequence_pro_print_formatted_sequence(seq,50)
    Terry's sub
  • Awkward for Terry

17
Solution in Perl is Packages
  • The problem with the previous solution is that
    "name prefix" had to be spelled out in every
    instance.
  • Alternative is "Package"

18
  • package Sequence_pro declaring a package
  • sub print_formatted_sequence
  • sub reverse_complement
  • 1
  • The package declaration tells Perl to insert
    "Sequence_pro in front of most names within the
    file.
  • Essentially, the code behaves like this
  • sub Sequence_proprint_formatted_sequence
  • sub Sequence_proreverse_complement
  • 1

19
Require'ing a Package
  • Now Bob may use the Sequence_pro library, and
    simply adds Sequence_pro to subroutines defined
    in the library.
  • !/usr/bin/perl
  • require 'sequence_pro2.pl'
  • sub reverse_complement
  • Sequence_proprint_formatted_sequence(seq,50)
  • The Sequence_pro prefix is left off for
    subroutines he wants to define on his own.
  • (Look at formatted_seq2.pl and sequence_pro2.pl)

20
formatted_seq.pl
  • !/usr/bin/perl
  • require 'sequence_pro2.pl'
  • seq "ATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTAccc
    ccccccccccccccccccccccccccccccccccccccccccccccccgg
    gggggggggggggggggggggggggggggggggggggggggggggggggg
    ggggggggggggggggggggggggggggggggggggggggggggggggg"
  • print_formatted_sequence(seq,50,10)
  • print_sequence(seq,50,10)
  • sub print_sequence
  • Sequence_proprint_formatted_sequence(_0,_
    1,_2)

21
sequence_pro2.pl
  • package Sequence_pro
  • sub print_formatted_sequence
  • if(_!2)
  • die("Error in Seqprint_formatted_sequence.
    Expecting 3 parameters\n")
  • my (seq,line_length,seq_length) _at__
  • my front 1
  • my back line_length
  • remainder seq
  • while(length(remainder)gtline_length)
  • _ remainder
  • (format,remainder) m/(.line_length)(.)/
    match a line, then the res
  • t
  • format s/(.seq_length)/1 /g match for
    10, then insert a space
  • print "front format back\n"
  • front front line_length
  • back back line_length
  • format remainder
  • format s/(.seq_length)/1 /g
  • back front length(remainder)-1
  • print "front format back\n"
  • 1

22
Package Names
  • similar to perl variable names
  • alphanumerics and underscores
  • no first digit
  • convention
  • to use capital letter as first letter
  • do not use same name as existing modules (core
    and CPAN) comes with experience
  • may contain multiple names/double colons
  • UtilsSequenceAlignment
  • Reference
  • perldoc perlmodlib (description of included
    modules)
  • man perlmodlib

23
Packages and Variables
  • may access variables in a package
  • package Sequence_pro
  • sequence "ATG"
  • sub print_formatted_sequence
  • 1
  • Then, in "main" program
  • seq Sequence_prosequence

24
Perldoc perlmodlib
  • Perl does not enforce private and public parts of
    its modules as you
  • may have been used to in other languages
    like C, Ada, or Modula-17.
  • Perl doesnt have an infatuation with
    enforced privacy. It would
  • prefer that you stayed out of its living
    room because you werent
  • invited, not because it has a shotgun.
  • The module and its user have a contract,
    part of which is common law,
  • and part of which is "written". Part of
    the common law contract is
  • that a module doesnt pollute any
    namespace it wasnt asked to. The
  • written contract for the module (A.K.A.
    documentation) may make other
  • provisions. But then you know when you
    "use RedefineTheWorld" that
  • youre redefining the world and willing to
    take the consequences.

25
End
Write a Comment
User Comments (0)
About PowerShow.com