line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Printer::Label::Template::Processor;
|
2
|
1
|
|
|
1
|
|
38518
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
4
|
use Carp;
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
90
|
|
6
|
1
|
|
|
1
|
|
933
|
use File::Slurp;
|
|
1
|
|
|
|
|
15062
|
|
|
1
|
|
|
|
|
71
|
|
7
|
1
|
|
|
1
|
|
1051
|
use Net::FTP;
|
|
1
|
|
|
|
|
55186
|
|
|
1
|
|
|
|
|
65
|
|
8
|
1
|
|
|
1
|
|
905
|
use Net::Printer;
|
|
1
|
|
|
|
|
25056
|
|
|
1
|
|
|
|
|
68
|
|
9
|
1
|
|
|
1
|
|
762
|
use Template;
|
|
1
|
|
|
|
|
60351
|
|
|
1
|
|
|
|
|
42
|
|
10
|
1
|
|
|
1
|
|
1040
|
use Params::Validate qw/validate SCALAR UNDEF OBJECT HASHREF ARRAYREF CODEREF/;
|
|
1
|
|
|
|
|
11297
|
|
|
1
|
|
|
|
|
1175
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '1.01';
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
15
|
|
|
|
|
|
|
# Constants
|
16
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# lookup table used to link file extensions and _build_output_from_* methods
|
19
|
|
|
|
|
|
|
my $H_TEMPLATES = {
|
20
|
|
|
|
|
|
|
pl => 'perl',
|
21
|
|
|
|
|
|
|
perl => 'perl',
|
22
|
|
|
|
|
|
|
tt => 'tkit',
|
23
|
|
|
|
|
|
|
tt2 => 'tkit',
|
24
|
|
|
|
|
|
|
};
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
27
|
|
|
|
|
|
|
# Public methods
|
28
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
31
|
|
|
|
|
|
|
# new
|
32
|
|
|
|
|
|
|
# Creates a label object
|
33
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub new {
|
36
|
1
|
|
|
1
|
1
|
193678
|
my $class = shift;
|
37
|
1
|
|
|
|
|
9
|
my %params = @_;
|
38
|
1
|
50
|
|
|
|
9
|
my $print_mode = defined($params{print_mode}) ? uc($params{print_mode}) : 'CON';
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# params validation
|
41
|
|
|
|
|
|
|
%params = validate(@_, {
|
42
|
|
|
|
|
|
|
script_file => {
|
43
|
|
|
|
|
|
|
type => SCALAR,
|
44
|
|
|
|
|
|
|
},
|
45
|
|
|
|
|
|
|
print_mode => {
|
46
|
|
|
|
|
|
|
type => SCALAR,
|
47
|
|
|
|
|
|
|
default => 'CON',
|
48
|
|
|
|
|
|
|
},
|
49
|
|
|
|
|
|
|
check_syntax => {
|
50
|
|
|
|
|
|
|
type => CODEREF,
|
51
|
0
|
|
|
0
|
|
0
|
default => sub { return (1==1) },
|
52
|
|
|
|
|
|
|
},
|
53
|
1
|
50
|
|
|
|
133
|
server => {
|
54
|
|
|
|
|
|
|
type => SCALAR,
|
55
|
|
|
|
|
|
|
optional => (grep(/$print_mode/, qw/FTP LPR/) ? 0 : 1),
|
56
|
|
|
|
|
|
|
},
|
57
|
|
|
|
|
|
|
port => {
|
58
|
|
|
|
|
|
|
type => SCALAR,
|
59
|
|
|
|
|
|
|
optional => 1, # Net::FTP and Net::Printer define their own default values
|
60
|
|
|
|
|
|
|
depends => [ 'server' ],
|
61
|
|
|
|
|
|
|
},
|
62
|
|
|
|
|
|
|
user => {
|
63
|
|
|
|
|
|
|
type => SCALAR,
|
64
|
|
|
|
|
|
|
optional => 1, # Net::FTP defines its own default value
|
65
|
|
|
|
|
|
|
depends => [ 'server', 'password' ],
|
66
|
|
|
|
|
|
|
},
|
67
|
|
|
|
|
|
|
password => {
|
68
|
|
|
|
|
|
|
type => SCALAR,
|
69
|
|
|
|
|
|
|
optional => 1, # Net::FTP defines its own default value
|
70
|
|
|
|
|
|
|
depends => [ 'server', 'user' ],
|
71
|
|
|
|
|
|
|
},
|
72
|
|
|
|
|
|
|
output_file => {
|
73
|
|
|
|
|
|
|
type => SCALAR,
|
74
|
|
|
|
|
|
|
optional => ($print_mode ne 'FILE'),
|
75
|
|
|
|
|
|
|
},
|
76
|
|
|
|
|
|
|
});
|
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
18
|
my $self = bless {}, $class;
|
79
|
1
|
|
|
|
|
13
|
$self->{$_} = $params{$_} foreach (keys %params);
|
80
|
|
|
|
|
|
|
|
81
|
1
|
|
|
|
|
7
|
return $self;
|
82
|
|
|
|
|
|
|
}
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
85
|
|
|
|
|
|
|
# printout
|
86
|
|
|
|
|
|
|
# Builds the output data and sends it to a printing system
|
87
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub printout {
|
90
|
1
|
|
|
1
|
1
|
748
|
my $self = shift;
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# params validation
|
93
|
1
|
|
|
|
|
27
|
my %params = validate(@_, {
|
94
|
|
|
|
|
|
|
vars => { type => HASHREF },
|
95
|
|
|
|
|
|
|
});
|
96
|
|
|
|
|
|
|
|
97
|
1
|
|
|
|
|
11
|
$self->{$_} = $params{$_} foreach (keys %params);
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# builds the output data
|
100
|
1
|
|
|
|
|
6
|
$self->_set_output_data;
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# checks the syntax
|
103
|
1
|
50
|
|
|
|
412
|
$self->{check_syntax}->($self->{output_data}) or croak "Invalid output syntax while processing $self->{script_file}";
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# builds the print method name and calls it
|
106
|
1
|
|
|
|
|
180
|
my $method = '_print_to_' . lc($self->{print_mode});
|
107
|
1
|
50
|
|
|
|
14
|
$self->can("$method") or croak "Unknown print method: $method";
|
108
|
1
|
|
|
|
|
7
|
$self->$method;
|
109
|
|
|
|
|
|
|
}
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
112
|
|
|
|
|
|
|
# Private methods
|
113
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
116
|
|
|
|
|
|
|
# _set_output_data
|
117
|
|
|
|
|
|
|
# Builds the output data of the label
|
118
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _set_output_data {
|
121
|
1
|
|
|
1
|
|
2
|
my $self = shift;
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# detects the script's language
|
124
|
1
|
|
|
|
|
16
|
my $script_language = $self->{script_file};
|
125
|
1
|
|
|
|
|
10
|
$script_language =~ s/^.*\.([^\.]*$)/lc($1)/e;
|
|
1
|
|
|
|
|
5
|
|
126
|
1
|
50
|
|
|
|
6
|
$H_TEMPLATES->{$script_language} or croak "Unknown script language: $script_language";
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# invokes the corresponding method
|
129
|
1
|
|
|
|
|
5
|
my $method = '_build_output_from_' . $H_TEMPLATES->{$script_language};
|
130
|
1
|
50
|
|
|
|
13
|
$self->can("$method") or croak "Unknown print method: $method";
|
131
|
1
|
|
|
|
|
5
|
$self->$method;
|
132
|
|
|
|
|
|
|
}
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
135
|
|
|
|
|
|
|
# _build_output_from_perl
|
136
|
|
|
|
|
|
|
# Builds output data by processing a Perl script
|
137
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub _build_output_from_perl {
|
140
|
0
|
|
|
0
|
|
0
|
my $self = shift;
|
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# loads the script
|
143
|
0
|
|
|
|
|
0
|
my $script = read_file($self->{script_file}) ;
|
144
|
0
|
0
|
|
|
|
0
|
$script or croak "Error while loading the file: $self->{script_file}";
|
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# evaluates the script
|
147
|
0
|
|
|
|
|
0
|
my $output_data = eval("$script");
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# links the output data to the label
|
150
|
0
|
|
|
|
|
0
|
$self->{output_data} = $output_data;
|
151
|
|
|
|
|
|
|
}
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
154
|
|
|
|
|
|
|
# _build_output_from_tkit
|
155
|
|
|
|
|
|
|
# Builds output data by processing a Template Toolkit script
|
156
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub _build_output_from_tkit {
|
159
|
1
|
|
|
1
|
|
3
|
my $self = shift;
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# extracts the script's filename
|
162
|
1
|
|
|
|
|
8
|
my $script_file = (split(/[\\\/]/, $self->{script_file}))[-1];
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# extracts the path to the script file
|
165
|
1
|
|
|
|
|
4
|
my $script_path = $self->{script_file};
|
166
|
1
|
|
|
|
|
19
|
$script_path =~ s/\/$script_file//g;
|
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# creates a TT2 object
|
169
|
1
|
|
33
|
|
|
23
|
my $tt = Template->new({
|
170
|
|
|
|
|
|
|
INCLUDE_PATH => $script_path,
|
171
|
|
|
|
|
|
|
INTERPOLATE => 1,
|
172
|
|
|
|
|
|
|
}) || croak "$Template::ERROR";
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# evaluates the script
|
175
|
1
|
|
|
|
|
38988
|
my $output_data;
|
176
|
1
|
50
|
|
|
|
8
|
$tt->process($script_file, $self->{vars}, \$output_data) || croak $tt->error();
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# links the output data to the label
|
179
|
1
|
|
|
|
|
125950
|
$self->{output_data} = $output_data;
|
180
|
|
|
|
|
|
|
}
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
183
|
|
|
|
|
|
|
# _print_to_con
|
184
|
|
|
|
|
|
|
# Sends the content to the standard output
|
185
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub _print_to_con {
|
188
|
1
|
|
|
1
|
|
3
|
my $self = shift;
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# prints the output data to standard output
|
191
|
1
|
|
|
|
|
1162
|
print "$self->{output_data}\n";
|
192
|
|
|
|
|
|
|
}
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
195
|
|
|
|
|
|
|
# _print_to_ftp
|
196
|
|
|
|
|
|
|
# Sends the content to a FTP server
|
197
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub _print_to_ftp {
|
200
|
0
|
|
|
0
|
|
|
my $self = shift;
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# connects to the FTP server
|
203
|
0
|
0
|
|
|
|
|
my $session = Net::FTP->new(
|
204
|
|
|
|
|
|
|
$self->{server},
|
205
|
|
|
|
|
|
|
Passive => 0,
|
206
|
|
|
|
|
|
|
Debug => 0,
|
207
|
|
|
|
|
|
|
) or croak "Error while connecting to $self->{server}\n";
|
208
|
0
|
0
|
|
|
|
|
$session->login(
|
209
|
|
|
|
|
|
|
$self->{user},
|
210
|
|
|
|
|
|
|
$self->{password},
|
211
|
|
|
|
|
|
|
) or croak "Invalid user/password\n";
|
212
|
0
|
|
|
|
|
|
$session->ascii;
|
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
# sends the in-memory file to the FTP server
|
215
|
0
|
0
|
|
|
|
|
open my $output_file, "<", \$self->{output_data} or croak $!;
|
216
|
0
|
0
|
|
|
|
|
$session->put($output_file, "OUTPUT.TXT") or croak "Error while sending the file\n";
|
217
|
0
|
|
|
|
|
|
$session->quit;
|
218
|
0
|
|
|
|
|
|
close $output_file;
|
219
|
|
|
|
|
|
|
}
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
222
|
|
|
|
|
|
|
# _print_to_lpr
|
223
|
|
|
|
|
|
|
# Sends the content to a print queue
|
224
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
sub _print_to_lpr {
|
227
|
0
|
|
|
0
|
|
|
my $self = shift;
|
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# creates a print queue
|
230
|
0
|
|
|
|
|
|
my $printer = new Net::Printer(
|
231
|
|
|
|
|
|
|
server => $self->{server},
|
232
|
|
|
|
|
|
|
);
|
233
|
0
|
0
|
|
|
|
|
$printer->{port} = $self->{port} if defined($self->{port});
|
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
# sends the output data to the print queue
|
236
|
0
|
|
|
|
|
|
my $res = $printer->printstring($self->{output_data});
|
237
|
0
|
0
|
|
|
|
|
$res or croak "Error while printing on $self->{server} port $self->{port} using LPR: " . $printer->printerror() . "\n";
|
238
|
|
|
|
|
|
|
}
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
241
|
|
|
|
|
|
|
# _print_to_file
|
242
|
|
|
|
|
|
|
# Writes the content to a file
|
243
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------
|
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
sub _print_to_file {
|
246
|
0
|
|
|
0
|
|
|
my $self = shift;
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
# dumps the output data to a file
|
249
|
0
|
0
|
|
|
|
|
write_file($self->{output_file}, \$self->{output_data}) or croak "Error while sending the file\n";
|
250
|
|
|
|
|
|
|
}
|
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
1;
|
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
__END__
|