Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Using Modules. 2 Modules A Perl module is a collection of subroutines and variables that typically implements some common functionality – and "packaged"

Similar presentations


Presentation on theme: "1 Using Modules. 2 Modules A Perl module is a collection of subroutines and variables that typically implements some common functionality – and "packaged""— Presentation transcript:

1 1 Using Modules

2 2 Modules A Perl module is a collection of subroutines and variables that typically implements some common functionality – and "packaged" to facilitate reusability Reusable package that is typically defined in a library file with the same name as the package with.pm (Perl Module) Example from normal perl distribution: File::Basename – parses file specifications use File::Basename; introduces 3 subroutines into the current package fileparse, basename, and dirname Reference: perldoc File::Basename

3 3 File::Basename After the "use", within this package, you may use the "basename" and "dirname" subroutines as if you had written them yourself use File::Basename; my $basename = basename($some_full_path); my $dir = dirname($some_full_path);

4 4 #!/usr/bin/perl # File_Basename/example.pl use File::Basename; # Note, we don't use File::Basename.pm $path = "/home/tabraun/BME/test/program.pl"; my $base = basename $path; ### How is this different than before with require??? print "base=$base\n"; ### require needed File::Basename::basename $path my $dir = dirname $path; print "dir=$dir\n"; my $base = &basename($path); print "base=$base\n"; my $dir = &dirname($path); print "dir=$dir\n"; my ($base,$dir,$suffix) = &fileparse($path,".pl"); print "base=$base dir=$dir suffice=$suffix\n";

5 5 Look at File::Basename Linux: more /usr/lib/perl5/5.8.0/File/Basename.pm Mac: more /System/Library/Perl/5.8.6/File/Basename.pm locate Basename.pm

6 6 Text here used for documenation use 5.006; use warnings; our(@ISA, @EXPORT, $VERSION, $Fileparse_fstype, $Fileparse_igncase); require Exporter; @ISA = qw(Exporter); @EXPORT = qw(fileparse fileparse_set_fstype basename dirname); $VERSION = "2.71"; # fileparse_set_fstype() - specify OS-based rules used in future # calls to routines in this package # # Currently recognized values: VMS, MSDOS, MacOS, AmigaOS, os2, RISCOS # Any other name uses Unix-style rules and is case-sensitive sub fileparse_set_fstype { my @old = ($Fileparse_fstype, $Fileparse_igncase); if (@_) { $Fileparse_fstype = $_[0]; $Fileparse_igncase = ($_[0] =~ /^(?:MacOS|VMS|AmigaOS|os2|RISCOS|MSWin32|MSDOS)/i); } wantarray ? @old : $old[0]; } # fileparse() - parse file specification # # Version 2.4 27-Sep-1996 Charles Bailey bailey@genetics.upenn.edu

7 7 sub fileparse { my($fullname,@suffices) = @_; unless (defined $fullname) { require Carp; Carp::croak("fileparse(): need a valid pathname"); } my($fstype,$igncase) = ($Fileparse_fstype, $Fileparse_igncase); my($dirpath,$tail,$suffix,$basename); my($taint) = substr($fullname,0,0); # Is $fullname tainted? if ($fstype =~ /^VMS/i) { if ($fullname =~ m#/#) { $fstype = '' } # We're doing Unix emulation else { ($dirpath,$basename) = ($fullname =~ /^(.*[:>\]])?(.*)/s); $dirpath ||= ''; # should always be defined } if ($fstype =~ /^MS(DOS|Win32)|epoc/i) { ($dirpath,$basename) = ($fullname =~ /^((?:.*[:\\\/])?)(.*)/s); $dirpath.= '.\\' unless $dirpath =~ /[\\\/]\z/; } elsif ($fstype =~ /^os2/i) { ($dirpath,$basename) = ($fullname =~ m#^((?:.*[:\\/])?)(.*)#s); $dirpath = './' unless $dirpath; # Can't be 0 $dirpath.= '/' unless $dirpath =~ m#[\\/]\z#;

8 8 } elsif ($fstype =~ /^MacOS/si) { ($dirpath,$basename) = ($fullname =~ /^(.*:)?(.*)/s); $dirpath = ':' unless $dirpath; } elsif ($fstype =~ /^AmigaOS/i) { ($dirpath,$basename) = ($fullname =~ /(.*[:\/])?(.*)/s); $dirpath = './' unless $dirpath; } elsif ($fstype !~ /^VMS/i) { # default to Unix ($dirpath,$basename) = ($fullname =~ m#^(.*/)?(.*)#s); if ($^O eq 'VMS' and $fullname =~ m:^(/[^/]+/000000(/|$))(.*):) { # dev:[000000] is top of VMS tree, similar to Unix '/' # so strip it off and treat the rest as "normal" my $devspec = $1; my $remainder = $3; ($dirpath,$basename) = ($remainder =~ m#^(.*/)?(.*)#s); $dirpath ||= ''; # should always be defined $dirpath = $devspec.$dirpath; } $dirpath = './' unless $dirpath; } if (@suffices) { $tail = ''; foreach $suffix (@suffices) { my $pat = ($igncase ? '(?i)' : ''). "($suffix)\$"; if ($basename =~ s/$pat//s) { $taint.= substr($suffix,0,0); $tail = $1. $tail; } $tail.= $taint if defined $tail; # avoid warning if $tail == undef wantarray ? ($basename.= $taint, $dirpath.= $taint, $tail) : ($basename.= $taint); }

9 9 # basename() - returns first element of list returned by fileparse() sub basename { my($name) = shift; (fileparse($name, map("\Q$_\E",@_)))[0]; } # more code….. (this is about 80% of package) Take home messages –source code is available to use, examine, and re-use –we've had enough perl now to be able to examine other code and partially comprehend (or even modify if that suits our needs) –you may actually modify/change modules as needed!!!

10 10 Primary Differences Between require/use and lib/module See pages 285-287 in "Programming Perl" A module may export symbols (subroutine names, variables, etc.,) and these become available to the main program as if they were scoped in the main program. This is opposed to library/require that has to have an explicit reference to the package. use/module: &print_formatted_sequence($seq,50,10); vs require/lib: MySeq::print_formatted_sequence($seq,50,10);

11 11 Primary Differences use Module List# List of symbols to export use Module –used for loading modules only –no quotes –you don't include.pm require EXPR –may be used for loading libraries or modules –if you load a module with "require", you must include the package name when referencing symbols (subroutines/variables, etc) Cannot "pollute" the name space with "require" "use" allows you to control how the name space is "polluted"

12 12 Documentation perldoc perlmod -- general description of modules perldoc perlmodlib -- list of general modules In Eclipse: Help->perldoc -> (enter whatever you want).


Download ppt "1 Using Modules. 2 Modules A Perl module is a collection of subroutines and variables that typically implements some common functionality – and "packaged""

Similar presentations


Ads by Google