Numeric Modules

   

Many Perl data- munging modules are available on CPAN that you can use to convert and otherwise manipulate numeric data, analogous to Oracle functions such as TO_NUMBER or TO_CHAR, but often going beyond these with increased specialization (an example is Number::Latin ). Those we consider to be the most useful in pre-handling Oracle database data are summarized in Table D-1.

You can obtain these modules and many others from both the CPAN (for Unix) and ActiveState (for Win32) archives. You can check for the latest status of PPM packages at:

http://aspn.activestate.com/ASPN/Downloads/ActivePerl/PPM/Packages
Table D-1. Numeric modules

CPAN module

Description/CPAN address

Number::Encode

Written by Luis Muoz; converts bit strings into numeric strings, in a similar manner to Oracle's BIN_TO_NUM.

http://www.cpan.org/authors/id/L/LU/LUISMUNOZ/

Number::Format

Written by William R. Ward; a popular number formatting package, which performs a variety of numeric operations and is described in the next section.

http://www.cpan.org/authors/id/WRW/

Number::Latin

Written by Sean M. Burke and going beyond Oracle function capabilities, this module converts numbers to and from the W3C Latin numbering system. This system uses the 'a'..'z' , 'aa'..'az' , 'ba'..'zz' notation, often seen in spreadsheets.

http://www.cpan.org/authors/id/S/SB/SBURKE/

Number::Phone::US

Written by Hugh Kennedy; validates US telephone numbers.

http://www.cpan.org/authors/id/K/KE/KENNEDYH/

Number::Spice

Written by Wim Verhaegen; reformats to the Spice notation for integrated circuit design [7] -- for example, 225 picofarads becomes 225p .

http://www.cpan.org/authors/id/W/WI/WIMV/

Number::Spell

Written by Les Howard; spells out integers in words for example, print(spell_number(777)) outputs seven hundred seventy seven .

http://www.cpan.org/authors/id/L/LH/LHOWARD/

[7] The Simulation Program for Integrated Circuits Emphasis. Check out the classic Berkeley Spice circuit design tool at http:// freshmeat .net/projects/berkeleyspice/

Let's take a look at one of these modules, Number::Format , and how you might use it to format Oracle database data.

Number::Format

Number::Format is a very useful Perl module that offers a variety of useful conversion methods , which produce results similar to Oracle's built-in TO_NUMBER and TO_CHAR functions. Number::Format also adds a few features that aren't available in the Oracle functions, such as the wide range of negative number formats you can adopt. We illustrate a typical usage of Number::Format 's format_number , format_price , and format_bytes in Example D-2. (This particular example deals with the Altairian Dollar currency favored by recent Galactic President, Zaphod Beeblebrox.)

The Number::Format module numberFormat.pl
 #!perl -w use strict;  use Number::Format;  # The neg_account key uses an "x" to represent the number, and then # whatever other formatting you require.    my $Altarian =     new Number::Format(-thousands_sep => ',',                        -decimal_point => '.',                        -int_curr_symbol => 'ALT',                        -decimal_digits => 4,                        -decimal_fill => 2,  -neg_format => '(x)',    # Accounting Style Negs  -kilo_suffix => ' KiloAlt',                        -mega_suffix => ' MegaAlt',                        -giga_suffix => ' PanGalacticGargle');    my $finiteProbability = 6666666666.66;  # We've used a negative currency amount for format_price() to   # demonstrate the regular collapses of the Altairian Dollar! :-)  print $Altarian->format_number($finiteProbability),    "\n",  $Altarian->format_price (-$finiteProbability, 3), "\n"  ,       $Altarian->format_bytes ($finiteProbability); 

Running the numberFormat.pl script produces the following output. Notice that the accounting-style neg_format method has enclosed our negative figure in brackets:

 $ perl numberFormat.pl 6,666,666,666.6600  (ALT 6,666,666,666.660)  6.2088 PanGalacticGargle 

You can download the Number::Format tarball from:

http://www.cpan.org/authors/id/WRW

You can install the Win32 ActivePerl package as follows :

 C:\>ppm PPM> install Number-Format 

Mathematics Modules

There are four mathematical modules bundled with Perl (summarized in Table D-2) that you can use to handle most of the mathematical data-munging operations you are likely to perform. For less common operations, check CPAN; you will find many unbundled modules there that provide mathematical support for data-munging operations on data ranging from Fibonaci [8] numbers through financial annuities.

[8] The Fibonaci numbers are an inductive sequence of numbers in which each term is generated by the two previous terms. The first two terms are both assigned the value of 1 and all other terms are created by adding the last two numbers together. The first few terms of the sequence is 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. This number pattern is often useful for mathematical and financial analysis, and is also used within Mother Nature to determine many different growth patterns. For instance, we may have five fingers, and vine leaves may have five fronds, because both are using the Fibonaci number sequence.

Table D-2. Mathematics modules bundled with Perl

Module

Description

Mark::BigFloat

Written by Mark Biggar; used for operations on arbitrary-length floating-point numbers.

Math::BigInt

Also written by Mark Biggar; a related module used for operations on integers of any length.

Math::Complex

Written by Raphael Manfredi and Jarkko Hietaniemi; used for operations on complex numbers.

Math::Trig

Also written by Manfredi and Hietaniemi; used to provide trigonometric support, including a definition of the pi constant ( figs/u03c0.gif ).

   


Perl for Oracle DBAs
Perl for Oracle Dbas
ISBN: 0596002106
EAN: 2147483647
Year: 2002
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net