The properties of Perl Modules:
- Modules either come with Perl or can be downloaded from CSPAN
- Modules can also be custom written
- Libraries also exist, but the difference is:
Libraries mostly contain similar subroutines
Modules is like a “smart library” – offers collection of subroutines that act as if they were build in functions
Modules are more commonly used in Perl than Libraries
- Some basic modules that already come with Perl are discussed below.
- File::Basename Module
Include the following at the top of file
- use File::Basename
- This works well in UNIX but need to be cautious when using in Windows
my $name = "/usr/local/bin/perl"; my $dirname = dirname $name; # set as bin my $basename = basename $name; # set as perl
- File::Spec Module
Used for manipulating file names, directories, and other stored file system information
Again this can work well in UNIX but needs to be cautioned on other OS
my $new_name = File::Spec -> catfile($dirname, $basename);
- Some other common modules:
Cwd Module
- This module tells you the current working directory
File::Copy
Fatal
- Replaces the do / die
Image::Size
Net::SMTP
- Be able to send email through an SMTP server
POSIX
- Contains POSIX functions
Sys::Hostname
Text::Wrap
Time::Local
Overload
- To overload existing functions (addition, subtraction, etc)
- CGI.pm
Common Gateway Interface
- Comes with Perl
- Easy way to form parameters and generate HTML responses
For webpage creation
Has many functions to print HTML
Examples:
#!/usr/bin/perl use CGI qw(:all); print header("text/plain"), # Creates the HTML header start_html("This is the page title"), # printing start of HTML h1("Header Description Here"); # h1 function makes an H1 header my $list_items; foreach my $param ( param() ) { # function to get param values $list_items .= li( "$param: " . param($param) ); # concating a list of param values } print ul($list_items); # printing the param list values as a list to HTML print end_HTML(); # ending the HTML document
- Databases and DBI
Doesn’t come with Perl but is the most popular module used
Uses same interface to connect to almost any database, from Oracle to whatever database (ODBC) driver exists
Download Perl DBI (module name)
Check http://dbi/perl.org for more information
- Pragma
These are special modules that come with Perl for the internal compiler
- For example: use strict # this is a pragma
Other examples are:
- Constant
- Tells the compiler that a given identifier has a constant value
- Diagnostics
- Compiler refers to the perldiag manpage to give more analysis on error messages
- Lib
- Tells Perl that the given directory is the first place to look for modules
- Vars
- Warnings
- Available in Perl Version 5.6 and up only