Home > Articles > Web Development > Perl

Modules in Perl

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

6.4 Some example modules

The Perl distribution comes with many modules, and CPAN (see Section 1.6) offers thousands of others. This section describes a few of these modules and how to use them.

6.4.1 Math::Trig

The Math::Trig module, which was written by Jarkko Hietaniemi and Raphael Manfredi, defines subroutines that fill in some of Perl's missing trigonometric functions. All of its subroutines are exported by default to the caller's package.

Among the subroutines defined by Math::Trig are tan, which returns the tangent of an angle in radians, and deg2rad, which converts degrees to radians. Here is an example of how to use this module:


use strict; 
use warnings; 

use Math::Trig; 

my $angle_degrees = 30; 
my $angle_radians = deg2rad $angle_degrees; 
my $tangent = tan $angle_radians; 

print "Tangent of $angle_degrees degrees is $tangent.\n"; 

6.4.2 File::Compare

File::Compare, written by Nick Ing-Simmons, compares two files. It returns 0 if the files are equal, 1 if the files are different, or -1 if there was an error.

By default, File::Compare exports the compare subroutine to the caller's package. The following program uses File::Compare to compare two files:

# filename: compare-files.pl 

use strict; 
use warnings; 
use File::Compare; 

# What files should we compare? 
my $file1 = "/home/reuven/.bashrc"; 
my $file2 = "/home/reuven/.bash_profile"; 

# Compare the files, and get a result 
my $result = compare($file1, $file2); 

if ($result == 0) 
   print "'$file1' and '$file2' are identical.\n"; 
elsif ($result == 1) 
   print "'$file1' and '$file2' are different.\n"; 
elsif ($result == -1) 
   die "Problem running File::Compare::compare: $! "; 
   die "Error! Result of '$result' from File::Compare: $! "; 

6.4.3 Getopt::Long

Getopt::Long, written by Johan Vromans and available on CPAN (see Section 1.6), makes it easy for Perl programs to accept command-line arguments. Unix programs often allow an option to be invoked with a single-letter argument or an equivalent, one-word argument. For example, GNU cat sees the -n and –number options as equivalent.

Command-line arguments often take values. For example:

program --number=5 

Getopt::Long automatically exports the subroutine GetOptions. takes a hash as an argument, in which the keys describe the data types and the values are references to variables that should be assigned the argument's value. For instance, a program can automatically place the integer argument to the –number flag in the $number variable with:

GetOpt("number=i" => \$number); 

To make –number and -n aliases for one another, separate the possibilities with a vertical bar (|). The following program demonstrates how to integrate this feature into a program:

# filename: test-getopt.pl 

use strict; 
use warnings; 
use Getopt::Long; 

# Declare the variable, giving a default value 
my $number = 0; 

# Get the options 
my $success = GetOptions("number|n=i" => \$number); 

# Use the variable 
print "Option was $number\n" if $success; 

If this program is invoked without –number or -n, GetOptions does not modify $number. GetOptions also tests the type of data passed in –number, exiting with a fatal error if the user passes a string or float.

The manual for Getopt::Long, available after installation with perldoc Getopt::Long, describes the module's type checking in detail.

6.4.4 Data::Dumper

Data::Dumper, written by Gurusamy Sarathy and available from CPAN (see Section 1.6), makes it possible to store, pass, and retrieve complex data structures. By default, Data::Dumper exports its Dumper subroutine.

The following program demonstrates how Data::Dumper stores an array of arrays:

# filename: demo-dumper.pl 

use strict; 
use warnings; 
use Data::Dumper; 

# Translate 0-1-2-3 (English, Spanish, Hebrew) 
my @array0 = qw(zero cero efes); 
my @array1 = qw(one uno achat); 
my @array2 = qw(two dos shtayim); 
my @array3 = qw(three tres shalosh); 

# Create a reference to our array of arrays 
my $numbers_ref = [\@array0, \@array1, \@array2, \@array3]; 

print Dumper($numbers_ref), "\n"; 

The output from demo-dumper.pl looks like this:

$VAR1 = [ 

Data::Dumper is particularly useful for saving data structures to disk and then reading them back again. The following program reads this structure from the file /tmp/output-file, retrieves it into a lexical with eval, and then displays its contents:

# filename: read-dumped-data.pl 

use strict; 
use warnings; 

my $VAR1; 

# What file is it? 
my $file = "/tmp/output-file"; 

# Open a filehandle 
open FILE, $file or die "Cannot read '$file': $! "; 

# Grab the whole file at once 
undef $/; 

# Read the contents 
my $contents = (<FILE>); 

# Close the filehandle 
close FILE; 

# Evaluate the contents into a new array ref 
eval $contents; 

foreach my $number (@$VAR1) 
      print "English: $number->[0]\n"; 
      print "Spanish: $number->[1]\n"; 
      print "Hebrew: $number->[2]\n"; 
      print "\n"; 
  • + Share This
  • 🔖 Save To Your Account