-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Format.pm
170 lines (114 loc) · 2.6 KB
/
Format.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package App::ISBN::Format;
use strict;
use warnings;
use Business::ISBN;
use Class::Utils qw(set_params);
use Error::Pure qw(err);
use Getopt::Std;
our $VERSION = 0.04;
# Constructor.
sub new {
my ($class, @params) = @_;
# Create object.
my $self = bless {}, $class;
# Process parameters.
set_params($self, @params);
# Object.
return $self;
}
# Run.
sub run {
my $self = shift;
# Process arguments.
$self->{'_opts'} = {
'h' => 0,
};
if (! getopts('h', $self->{'_opts'}) || @ARGV < 1
|| $self->{'_opts'}->{'h'}) {
print STDERR "Usage: $0 [-h] [--version] isbn_string\n";
print STDERR "\t-h\t\tPrint help.\n";
print STDERR "\t--version\tPrint version.\n";
print STDERR "\tisbn_string\tISBN string.\n";
return 1;
}
$self->{'_isbn_string'} = shift @ARGV;
my $isbn_obj = Business::ISBN->new($self->{'_isbn_string'});
if (! $isbn_obj) {
err "ISBN '$self->{'_isbn_string'}' is bad.";
return 1;
}
# Validation.
if (! $isbn_obj->is_valid) {
$isbn_obj->fix_checksum;
# Check again.
if (! $isbn_obj->is_valid) {
err "ISBN '$self->{'_isbn_string'}' is not valid.";
return 1;
}
}
# Construct output.
my $isbn_concrete;
my $isbn_without_dash = $self->{'_isbn_string'};
$isbn_without_dash =~ s/-//msg;
if (length $isbn_without_dash > 10) {
$isbn_concrete = $isbn_obj->as_isbn13;
} else {
$isbn_concrete = $isbn_obj->as_isbn10;
}
print $self->{'_isbn_string'}.' -> '.$isbn_concrete->as_string, "\n";
return 0;
}
1;
__END__
=pod
=encoding utf8
=head1 NAME
App::ISBN::Format - Base class for isbn-format script.
=head1 SYNOPSIS
use App::ISBN::Format;
my $app = App::ISBN::Format->new;
my $exit_code = $app->run;
=head1 METHODS
=head2 C<new>
my $app = App::ISBN::Format->new;
Constructor.
=head2 C<run>
my $exit_code = $app->run;
Run.
Returns 1 for error, 0 for success.
=head1 ERRORS
new():
From Class::Utils::set_params():
Unknown parameter '%s'.
run():
ISBN '%s' is bad.
ISBN '%s' is not valid.
=head1 EXAMPLE
=for comment filename=format_example_isbn.pl
use strict;
use warnings;
use App::ISBN::Format;
# Arguments.
@ARGV = (
'9788025343364',
);
# Run.
exit App::ISBN::Format->new->run;
# Output:
# 9788025343364 -> 978-80-253-4336-4
=head1 DEPENDENCIES
L<Business::ISBN>,
L<Class::Utils>,
L<Error::Pure>,
L<Getopt::Std>.
=head1 REPOSITORY
L<https://github.com/michal-josef-spacek/App-ISBN-Format>
=head1 AUTHOR
Michal Josef Špaček L<mailto:[email protected]>
L<http://skim.cz>
=head1 LICENSE AND COPYRIGHT
© 2022-2024 Michal Josef Špaček
BSD 2-Clause License
=head1 VERSION
0.04
=cut