You are on page 1of 11

CPAN Modules

CPAN: Comprehensive Perl Archive


Network

 CPAN, is an archive of software


modules written in Perl, also
includes documentation for them.
 The archive has been online
[http://www.cpan.org/] since
October 1995 and is constantly
growing.
 Most of the generic functionality is
already available through various
CPAN modules.
 Exercises will demonstrate the
usage of some CPAN modules.

for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 2
Using Getopt::Long
 Parses the command line from @ARGV, recognizing and removing
specified options and their possible values.
 Basic Options:
 The most simple options are the ones that take no values.
 For options that take values: specify whether the option value is
required or not, and what kind of value the option expects.
¬ Required values: “=“
¬ Optional values: “:”
¬ Possible value types:
 s : String value
 i : Integer value
 f : Float value

for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 3
Exercise 1: Using Getopt CPAN module
use Getopt::Long;
my $in_file = "";
my $out_file = "";
my $count = '';
my $help = '';

GetOptions("-i=s" => \$in_file, # Value required, of type String


"-o:s" => \$out_file, # Optional value, of type String
"-count=i" => \$count, # Value required, of type Int
"-h" => \$help); # No value required

print "Input File: $in_file\nOutput File: $out_file\nCount:


$count\nHelp: $help\n";

Exercises\Chapter_08_CPAN_Modules\Exercise1.pl
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 4
Using File::Basename
 fileparse : Used to parse file paths into their directory,
filename and suffix.
 Suffix can be given as a list of strings, or as RegEx as well.

my @exts = qw(.doc .txt);


my $mypath="/home/user/dir1/dir2/file3.txt";
my($base, $path, $ext) = fileparse($mypath, @exts);
print ("Path: $path\nBase: $base\nExt: $ext\n\n");
($base, $path, $ext) = fileparse($mypath, '\..*');
print ("Path: $path\nBase: $base\nExt: $ext\n\n");

# Output
Path: /home/user/dir1/dir2/
Base: file3
Ext: .txt

for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 5
Using File::Basename [cntd…]

 basename : returns the last level of a file path.


 provided for compatibility with the Unix shell command
basename().
my $mypath="/home/user/dir1/dir2/file3.txt";
Print “File:”, basename($mypath); # Output
$mypath="/home/user/dir1/dir2/"; File:file3.txt
Print “File:”, basename($mypath); File:dir2

 Note the output when the input path does not include filename.
 It does NOT always return the file name portion of a path as you
might expect, just returns the “last level of a file path”.
 To be safe, if you want the file name portion of a path use
fileparse().

for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 6
Using File::Basename [cntd…]

 dirname : returns all but the last level of a file path.


 provided for compatibility with the Unix shell command
dirname().
my $mypath="/home/user/dir1/dir2/file3.txt";
Print “Dir:”, basename($mypath); # Output
$mypath="/home/user/dir1/dir2/"; Dir: =/home/user/dir1/dir2
Print “Dir:”, basename($mypath); Dir: =/home/user/dir1

 Note the output when the input path does not include filename.
 In spite of its name, it does NOT always return the directory
name as you might expect.
 To be safe, if you want the directory name portion of a path
use fileparse().

for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 7
Exercise 2: Using File::Basename module

Write a perl script to parse the file path


"/home/user/dir1/dir2/file3.txt“ into Directory
path, Base filename, and File extn using ‘fileparse’.

Again parse the same file path using ‘basename’ &


‘dirname’ to get the Directory path & File name
separately.

for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 8
Exercise 3: Writing Excel file using
Spreadsheet::WriteExcel

# Create a new Excel workbook


my $workbook = Spreadsheet::WriteExcel-
>new('perl.xls');

# Add a worksheet
my $worksheet = $workbook->add_worksheet();

# Write into a cell, using row & column notation


$worksheet->write(1, $col, 'Hi Excel!');

# Write a number and a formula using A1 notation


$worksheet->write('A3', 12.345);
Exercises/Chapter_08_CPAN_Modules/Exercise3_write_excel.pl
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 9
Exercise 4: Reading Excel file using
Spreadsheet::ParseExcel
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse('./perl.xls');
for my $worksheet ( $workbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
for my $row ( $row_min .. $row_max ) {
for my $col ( $col_min .. $col_max ) {
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
print "Value = ", $cell->value(),
}
}
}

Exercises/Chapter_08_CPAN_Modules/Exercise4_read_excel.pl
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 10

You might also like