root/lang/perl/Business-ISRC/trunk/lib/Business/ISRC.pm @ 16900

Revision 16900, 4.1 kB (checked in by bonar, 5 years ago)

module for International Standard Recording Code (ISRC)

Line 
1package Business::ISRC;
2
3use strict;
4use warnings;
5
6use Locale::Country qw/code2country/;
7use base 'Class::Accessor';
8
9use 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
23sub _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
49sub 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
72sub as_default_string {
73    my $self = shift;
74    return $self->as_string();
75}
76
77sub 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
921;
93
94__END__
95
96=head1 NAME
97
98Business::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
124This module provides data container for ISRC. ISRC is the unique code
125for identifying sound recordings and music videos internationally.
126You can use this to validate or normalize ISRC strings.
127
128visit ifpi site for details of ISRC definition:
129
130ISRC Handbook(HTML)
131http://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
147all 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
160Construct Business::ISRC object. return if failed to parse given
161ISRC string. if the name for the given country code in ISRC was
162not found in Locale::Country module, new() also return (nothing).
163
164=head2 as_string()
165
166return 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
175L<Business::UPC>
176
177=head1 AUTHOR
178
179Nakano Kyohei (bonar) E<lt>bonar@me.comE<gt>
180
181=head1 COPYRIGHT AND LICENSE
182
183Copyright (C) 2008 by nakano kyohei (bonar)
184
185This library is free software; you can redistribute it and/or modify
186it under the same terms as Perl itself, either Perl version 5.8.8 or,
187at your option, any later version of Perl 5 you may have available.
188
189=cut
Note: See TracBrowser for help on using the browser.