| 1 | =for gpg | ||||||||||||||||||||||||||||
| 2 | -----BEGIN PGP SIGNED MESSAGE----- | ||||||||||||||||||||||||||||
| 3 | Hash: SHA1 | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Tie::Comma - A simple and easy way to format numbers with commas, | ||||||||||||||||||||||||||||
| 8 | using a tied hash. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | This documentation describes version 0.04 of Tie::Comma, January 07, 2005 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | package Tie::Comma; | ||||||||||||||||||||||||||||
| 18 | 2 | $Tie::Comma::VERSION = 0.04; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use vars qw/@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS %comma/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | @ISA = qw/Exporter/; | |||||||||||||||||||||||||||
| 23 | 2 | @EXPORT = qw/%comma/; | |||||||||||||||||||||||||||
| 24 | 2 | @EXPORT_OK = qw/commify/; | |||||||||||||||||||||||||||
| 25 | 2 | %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Defaults | ||||||||||||||||||||||||||||
| 28 | 2 | our $thou_sep = ','; | |||||||||||||||||||||||||||
| 29 | 2 | our $deci_sep = '.'; | |||||||||||||||||||||||||||
| 30 | 2 | our $grouping = 3; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Configure for locale, if possible. | ||||||||||||||||||||||||||||
| 33 | eval | ||||||||||||||||||||||||||||
| 34 | 2 | { | |||||||||||||||||||||||||||
| 35 | 2 | require POSIX; | |||||||||||||||||||||||||||
| 36 | 2 | my $loc = POSIX::setlocale(POSIX::LC_NUMERIC()); | |||||||||||||||||||||||||||
| 37 | 2 | my $lc = POSIX::localeconv(); | |||||||||||||||||||||||||||
| 38 | 2 | ▶ | $thou_sep = $lc->{thousands_sep} || $thou_sep; | ||||||||||||||||||||||||||
|
Condition33%1/3
$lc->{'thousands_sep'} || $Tie::Comma::thou_sep
MC/DC0%0/2
$lc->{'thousands_sep'} || $Tie::Comma::thou_sep
$lc->{'thousands_sep'}
$Tie::Comma::thou_sep
Truth table33%1/3
A || B
A= $lc->{'thousands_sep'}B= $Tie::Comma::thou_sep
| |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | $deci_sep = $lc->{decimal_point} || $deci_sep; | ||||||||||||||||||||||||||
|
Condition33%1/3
$lc->{'decimal_point'} || $Tie::Comma::deci_sep
MC/DC0%0/2
$lc->{'decimal_point'} || $Tie::Comma::deci_sep
$lc->{'decimal_point'}
$Tie::Comma::deci_sep
Truth table33%1/3
A || B
A= $lc->{'decimal_point'}B= $Tie::Comma::deci_sep
| |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | $grouping = $lc->{grouping}? unpack('c', $lc->{grouping}) : $grouping; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | }; # Ignore any errors in this block -- just fall back to the defaults. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Substitution pattern | ||||||||||||||||||||||||||||
| 44 | 2 | my $num_pat = "(" . ("\\d" x $grouping) . ")(?=\\d)(?!\\d*\\$deci_sep)"; | |||||||||||||||||||||||||||
| 45 | 2 | my $num_re = qr/$num_pat/; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Here's the statement that makes it all happen. | ||||||||||||||||||||||||||||
| 48 | 2 | tie our %comma, 'Tie::Comma'; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Delay loading Carp.pm until needed. | ||||||||||||||||||||||||||||
| 51 | sub Tie::Comma::croak | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
croak: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 0 | require Carp; | |||||||||||||||||||||||||||
| 54 | 0 | goto &Carp::croak; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | #---> $string = commify $number; | ||||||||||||||||||||||||||||
| 58 | # commify : Formats a number with commas. | ||||||||||||||||||||||||||||
| 59 | # This version is taken from the Perl Cookbook. | ||||||||||||||||||||||||||||
| 60 | sub commify ($) | ||||||||||||||||||||||||||||
| 61 | 24 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
commify: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 24 | my $rev_num = reverse shift; # The number to be formatted, reversed. | |||||||||||||||||||||||||||
| 63 | 24 | $rev_num =~ s/$num_re/$1$thou_sep/g; | |||||||||||||||||||||||||||
| 64 | 24 | return scalar reverse $rev_num; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub TIEHASH | ||||||||||||||||||||||||||||
| 68 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 70 | 2 | my $dummy; # not used, but we need a reference. | |||||||||||||||||||||||||||
| 71 | 2 | bless \$dummy, $class; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub FETCH | ||||||||||||||||||||||||||||
| 75 | 26 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=14 SCAR=26.6
| |||||||||||||||||||||||||||||
| 76 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 77 | 26 | my $key = shift; | |||||||||||||||||||||||||||
| 78 | 26 | ▶ | return '' if !defined $key; # No args? or undef? return empty string. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 24 | my @args = split $;, $key, -1; | |||||||||||||||||||||||||||
| 81 | 24 | ▶ | @args > 3 and Tie::Comma::croak "Too many arguments to %comma"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 24 | my ($num, $dp, $min_fw) = @args; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 24 | for ($dp, $min_fw) | |||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | 48 | ▶ | next unless defined; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 24 | ▶ | next unless length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 22 | s/\Q$deci_sep\E.*//o; # remove any fractional part | |||||||||||||||||||||||||||
| 89 | 22 | ▶ | $_ = 0 unless /^-?\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 24 | ▶ | $min_fw = 0 if (!defined $min_fw or length $min_fw == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $min_fw or length $min_fw == 0
MC/DC50%1/2
not defined $min_fw or length $min_fw == 0
not defined $min_fw
length $min_fw == 0
Truth table66%2/3
not(A) or B
A= not defined $min_fwB= length $min_fw == 0
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Caller specified number of decimal places? | ||||||||||||||||||||||||||||
| 94 | 24 | ▶ | if (defined $dp && length $dp) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $dp and length $dp
MC/DC100%2/2
defined $dp and length $dp
defined $dp
length $dp
Truth table100%3/3
A and B
A= defined $dpB= length $dp
| |||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | 14 | ▶ | Tie::Comma::croak "Negative decimal places in %comma" if $dp < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 14 | $dp = abs($dp); | |||||||||||||||||||||||||||
| 98 | 14 | $num = sprintf "%.${dp}f", $num; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 24 | my $cnum = commify $num; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # Pad, if necessary. | ||||||||||||||||||||||||||||
| 104 | 24 | ▶ | return $cnum if length $cnum >= abs($min_fw); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 6 | my $spaces = ' ' x (abs($min_fw) - length $cnum); | |||||||||||||||||||||||||||
| 106 | 6 | ▶ | return $min_fw < 0? "$cnum$spaces" : "$spaces$cnum"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | use subs qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | 2 | STORE EXISTS CLEAR FIRSTKEY NEXTKEY ); | |||||||||||||||||||||||||||
| 111 | *STORE = *EXISTS = *CLEAR = *FIRSTKEY = *NEXTKEY = sub | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 113 | 0 | Tie::Comma::croak "Invalid call to Tie::Comma internal function"; | |||||||||||||||||||||||||||
| 114 | 2 | }; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | 1; | |||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||