Numeric ModulesMany 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:
Let's take a look at one of these modules, Number::Format, and how you might use it to format Oracle database data. Number::FormatNumber::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: You can install the Win32 ActivePerl package as follows: C:\>ppm PPM> install Number-Format Mathematics ModulesThere 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.
|