blib/lib/Business/CUSIP/Random.pm

filter: Statementstmt 100% 27 / 27 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 100% 41 / 41 CC 3 SCAR 3.4
CC
3
Cov
100%
rand_issue_number
2.0
_pick
2.0
BEGIN
1.0
CRAP 1.4
↑ Summary
1 package Business::CUSIP::Random;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Business::CUSIP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.01';
8  
9 =head1 NAME
10  
11 Business::CUSIP::Random - Generate random CUSIP numbers for testing
12  
13 =head1 SYNOPSIS
14  
15 use Business::CUSIP::Random;
16  
17 my $cusip;
18  
19 $cusip = Business::CUSIP::Random->generate; # returns a Business::CUSIP object
20  
21 # or...
22  
23 $cusip = Business::CUSIP::Random->generate_string; # returns a string
24  
25 =head1 DESCRIPTION
26  
27 Generates a random CUSIP (Committee on Uniform Security Identification Procedures) number for use in testing.
28  
29 =head1 METHODS
30  
31 =head2 generate
32  
33 =head2 generate_string
34  
35 Returns a randomly-generated, valid CUSIP number.
36  
37 generate() returns a Business::CUSIP object, while generate_string() returns a string.
38  
39 Takes the following optional parameter as a hash:
40  
41 =over 4
42  
43 =item * B<fixed_income>
44  
45 If true, the CUSIP generated will follow the format defined for fixed-income securities.
46  
47 =back
48  
49 =cut
50  
51 1001 ▶sub generate {
Subroutine generate: called CC=1 SCAR=0.0
Pod documented
52 1001 my ($class, %params) = @_;
53  
54 1001 my $cusip = $class->rand_issuer_number(%params) .
55 $class->rand_issue_number(%params);
56 1001 my $cusip_obj = Business::CUSIP->new($cusip, $params{fixed_income});
57 1001 $cusip .= $cusip_obj->check_digit;
58 1001 $cusip_obj->cusip($cusip);
59 1001 return $cusip_obj;
60 }
61  
62 1 ▶sub generate_string {
Subroutine generate_string: called CC=1 SCAR=0.0
Pod documented
63 1 my $class = shift;
64 1 return $class->generate(@_)->cusip;
65 }
66  
67 =head2 rand_issuer_number
68  
69 Returns a random CUSIP issuer number
70  
71 Takes the following optional parameter as a hash:
72  
73 =over 4
74  
75 =item * B<fixed_income>
76  
77 If true, the issuer number generated will follow the format defined for fixed-income securities.
78  
79 =back
80  
81 =cut
82  
83 1001 ▶sub rand_issuer_number {
Subroutine rand_issuer_number: called CC=1 SCAR=0.0
Pod documented
84 1001 my ($class, %params) = @_;
85 1001 return $class->_pick(3, 0..9) . $class->_pick(3, 0..9, 'A'..'Z');
86 }
87  
88 =head2 rand_issue_number
89  
90 Generates a random CUSIP issue number.
91  
92 Takes the following optional parameter as a hash:
93  
94 =over 4
95  
96 =item * B<fixed_income>
97  
98 If true, the issue number generated will follow the format defined for fixed-income securities.
99  
100 =back
101  
102 =cut
103  
104 1001 ▶sub rand_issue_number {
Subroutine rand_issue_number: called CC=2 SCAR=6.9
Pod documented
105 1001 my ($class, %params) = @_;
106 1001 ▶ return $params{fixed_income} ? $class->_pick(2, 'A'..'H', 'J'..'N', 'P'..'Z', 2..9)
Branch: 1 branch
BranchTrueFalseTotal
$params{'fixed_income'} ? : 471 530 1001
107 : $class->_pick(1, 10..88);
108 }
109  
110 3003 ▶sub _pick {
Subroutine _pick: called CC=2 SCAR=6.9
111 3003 my ($class, $count, @chars) = @_;
112 3003 my $res = '';
113 3003 $res .= $chars[rand @chars] for (1..$count);
114 3003 return $res;
115 }
116  
117 =head1 DEPENDENCIES
118  
119 Business::CUSIP
120  
121 =head1 AUTHORS
122  
123 Michael Aquilina <aquilina@cpan.org>
124  
125 Grant Street Group <developers@grantstreet.com>
126  
127 =head1 COPYRIGHT AND LICENSE
128  
129 Copyright (C) 2011 Michael Aquilina.
130  
131 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
132  
133 =cut
134  
135 1 1;
136  
↑ Summary