| 1 | package Business::ISRC; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use Locale::Country qw/code2country/; |
|---|
| 7 | use base 'Class::Accessor'; |
|---|
| 8 | |
|---|
| 9 | use overload '""' => 'as_default_string'; |
|---|
| 10 | |
|---|
| 11 | $Business::ISRC::VERSION = '0.01'; |
|---|
| 12 | $Business::ISRC::VERBOSE = 1; |
|---|
| 13 | |
|---|
| 14 | __PACKAGE__->mk_ro_accessors(qw/ |
|---|
| 15 | raw_string |
|---|
| 16 | country_code |
|---|
| 17 | country_name |
|---|
| 18 | registrant_code |
|---|
| 19 | year |
|---|
| 20 | designation_code |
|---|
| 21 | /); |
|---|
| 22 | |
|---|
| 23 | sub _parse { |
|---|
| 24 | my ($isrc_string) = @_; |
|---|
| 25 | return if !defined $isrc_string; |
|---|
| 26 | |
|---|
| 27 | my $str = uc($isrc_string); |
|---|
| 28 | $str =~ s/\s//g; |
|---|
| 29 | |
|---|
| 30 | # it should be like "US-L4Q-07-02458" |
|---|
| 31 | if ($str !~ /^ |
|---|
| 32 | (?:ISRC)?\-? # prefix |
|---|
| 33 | ([a-zA-Z]{2})\-? # country code |
|---|
| 34 | ([a-zA-Z0-9]{3})\-? # registrant code |
|---|
| 35 | (\d{2})\-? # year |
|---|
| 36 | (\d{5}) # designation code |
|---|
| 37 | $/x) { |
|---|
| 38 | return; |
|---|
| 39 | } |
|---|
| 40 | return { |
|---|
| 41 | raw_string => $isrc_string, |
|---|
| 42 | country_code => uc($1), |
|---|
| 43 | registrant_code => $2, |
|---|
| 44 | year => $3, |
|---|
| 45 | designation_code => $4, |
|---|
| 46 | }; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | sub new { |
|---|
| 50 | my ($class, $isrc_string) = @_; |
|---|
| 51 | |
|---|
| 52 | my $self = _parse($isrc_string); |
|---|
| 53 | if (!defined $self) { |
|---|
| 54 | warn "invalid ISRC string [$isrc_string]." |
|---|
| 55 | . "see perldoc Business::ISRC and check format." |
|---|
| 56 | if $Business::ISRC::VERBOSE; |
|---|
| 57 | return; |
|---|
| 58 | } |
|---|
| 59 | bless $self, $class; |
|---|
| 60 | |
|---|
| 61 | my $country_name = code2country(lc($self->country_code)); |
|---|
| 62 | if (!defined $country_name) { |
|---|
| 63 | warn "unknown country code: " . $self->country_code |
|---|
| 64 | if $Business::ISRC::VERBOSE; |
|---|
| 65 | return; |
|---|
| 66 | } |
|---|
| 67 | $self->{country_name} = $country_name; |
|---|
| 68 | |
|---|
| 69 | return $self; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | sub as_default_string { |
|---|
| 73 | my $self = shift; |
|---|
| 74 | return $self->as_string(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | sub as_string { |
|---|
| 78 | my ($self, %opt) = @_; |
|---|
| 79 | |
|---|
| 80 | my $delim = exists $opt{no_dash} ? '' : '-'; |
|---|
| 81 | my (@ret); |
|---|
| 82 | push @ret, 'ISRC' if exists $opt{add_prefix}; |
|---|
| 83 | push @ret |
|---|
| 84 | , $self->country_code |
|---|
| 85 | , $self->registrant_code |
|---|
| 86 | , $self->year |
|---|
| 87 | , $self->designation_code |
|---|
| 88 | ; |
|---|
| 89 | return join $delim, @ret; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | 1; |
|---|
| 93 | |
|---|
| 94 | __END__ |
|---|
| 95 | |
|---|
| 96 | =head1 NAME |
|---|
| 97 | |
|---|
| 98 | Business::ISRC - Perl extension for manipulating International Standard Recording Code (ISRC) |
|---|
| 99 | |
|---|
| 100 | =head1 SYNOPSIS |
|---|
| 101 | |
|---|
| 102 | use Business::ISRC; |
|---|
| 103 | |
|---|
| 104 | # create object (validate string) |
|---|
| 105 | my $isrc = Business::ISRC->new("usl4q0702458"); |
|---|
| 106 | if (!defined $isrc) { |
|---|
| 107 | die "invalid isrc format"; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | $isrc->country_code; # US |
|---|
| 111 | $isrc->country_name; # Unites States |
|---|
| 112 | $isrc->registrant_code; # L4Q |
|---|
| 113 | $isrc->year; # 07 |
|---|
| 114 | $isrc->designation_code; # 02458 |
|---|
| 115 | |
|---|
| 116 | # get normalized string |
|---|
| 117 | print $isrc; # US-L4Q-07-02458 |
|---|
| 118 | |
|---|
| 119 | # or this is the same as above |
|---|
| 120 | print $isrc->as_string(); |
|---|
| 121 | |
|---|
| 122 | =head1 DESCRIPTION |
|---|
| 123 | |
|---|
| 124 | This module provides data container for ISRC. ISRC is the unique code |
|---|
| 125 | for identifying sound recordings and music videos internationally. |
|---|
| 126 | You can use this to validate or normalize ISRC strings. |
|---|
| 127 | |
|---|
| 128 | visit ifpi site for details of ISRC definition: |
|---|
| 129 | |
|---|
| 130 | ISRC Handbook(HTML) |
|---|
| 131 | http://www.ifpi.org/content/section_resources/isrc_handbook.html |
|---|
| 132 | |
|---|
| 133 | =head2 ISRC FORMAT |
|---|
| 134 | |
|---|
| 135 | ISRC consists of five parts: |
|---|
| 136 | |
|---|
| 137 | sample: US-L4Q-07-02458 |
|---|
| 138 | |
|---|
| 139 | ISRC (4) - prefix (omittable) |
|---|
| 140 | US (2) - country code (ISO 3166-1-Alpha-2) |
|---|
| 141 | L4Q (3) - registrant code |
|---|
| 142 | 07 (2) - year (1980=>80, 2012=>12) |
|---|
| 143 | 02458 (5) - designation code (track serial number) |
|---|
| 144 | |
|---|
| 145 | =head1 ACCESSOR |
|---|
| 146 | |
|---|
| 147 | all fields are read-only. |
|---|
| 148 | |
|---|
| 149 | raw_string |
|---|
| 150 | country_code |
|---|
| 151 | country_name |
|---|
| 152 | registrant_code |
|---|
| 153 | year |
|---|
| 154 | designation_code |
|---|
| 155 | |
|---|
| 156 | =head1 METHOD |
|---|
| 157 | |
|---|
| 158 | =head2 new() |
|---|
| 159 | |
|---|
| 160 | Construct Business::ISRC object. return if failed to parse given |
|---|
| 161 | ISRC string. if the name for the given country code in ISRC was |
|---|
| 162 | not found in Locale::Country module, new() also return (nothing). |
|---|
| 163 | |
|---|
| 164 | =head2 as_string() |
|---|
| 165 | |
|---|
| 166 | return normalized string. you can specify these options: |
|---|
| 167 | |
|---|
| 168 | my $str = $isrc->as_string( |
|---|
| 169 | add_prefix => 1, # add "ISRC-" prefix |
|---|
| 170 | no_dash => 1, # delete dash from string |
|---|
| 171 | ); |
|---|
| 172 | |
|---|
| 173 | =head1 SEE ALSO |
|---|
| 174 | |
|---|
| 175 | L<Business::UPC> |
|---|
| 176 | |
|---|
| 177 | =head1 AUTHOR |
|---|
| 178 | |
|---|
| 179 | Nakano Kyohei (bonar) E<lt>bonar@me.comE<gt> |
|---|
| 180 | |
|---|
| 181 | =head1 COPYRIGHT AND LICENSE |
|---|
| 182 | |
|---|
| 183 | Copyright (C) 2008 by nakano kyohei (bonar) |
|---|
| 184 | |
|---|
| 185 | This library is free software; you can redistribute it and/or modify |
|---|
| 186 | it under the same terms as Perl itself, either Perl version 5.8.8 or, |
|---|
| 187 | at your option, any later version of Perl 5 you may have available. |
|---|
| 188 | |
|---|
| 189 | =cut |
|---|