1 |
package Webpacus::Model::WebPAC; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
use lib '/data/webpac2/lib'; |
6 |
use base qw/ |
7 |
Catalyst::Model |
8 |
/; |
9 |
use WebPAC::Store 0.08; |
10 |
use Search::Estraier 0.04; |
11 |
use File::Slurp; |
12 |
use Time::HiRes qw/time/; |
13 |
use Encode qw/encode decode from_to/; |
14 |
use Template; |
15 |
use Data::Dumper; |
16 |
|
17 |
=head1 NAME |
18 |
|
19 |
Webpacus::Model::WebPAC - Catalyst Model |
20 |
|
21 |
=head1 SYNOPSIS |
22 |
|
23 |
See L<Webpacus> and L<WebPAC>. |
24 |
|
25 |
=head1 DESCRIPTION |
26 |
|
27 |
Catalyst Model for access to WebPAC data. |
28 |
|
29 |
=head2 new |
30 |
|
31 |
Configuration for hyperestraier in C<config.yaml> like this: |
32 |
|
33 |
--- #YAML:1.0 |
34 |
# DO NOT USE TABS FOR INDENTATION OR label/value SEPARATION!!! |
35 |
|
36 |
# configuration for hyper estraier full text search engine |
37 |
hyperestraier: |
38 |
masterurl: 'http://localhost:1978/node/webpac2' |
39 |
defaultnode: 'webpac2' |
40 |
defaultdepth: 1 |
41 |
user: 'admin' |
42 |
passwd: 'admin' |
43 |
hits_on_page: 100 |
44 |
hits_for_pager: 1000 |
45 |
|
46 |
webpac: |
47 |
db_path: '/data/webpac2/db' |
48 |
template_path: '/data/webpac2/conf/output/tt' |
49 |
template: 'html_ffzg_results_short.tt' |
50 |
# encoding comming from webpac |
51 |
webpac_encoding: 'iso-8859-2' |
52 |
|
53 |
=cut |
54 |
|
55 |
sub new { |
56 |
my ( $self, $c, $config ) = @_; |
57 |
|
58 |
$self = $self->NEXT::new($c, $config); |
59 |
$self->config($config); |
60 |
|
61 |
my $log = $c->log; |
62 |
$self->{log} = $log; |
63 |
|
64 |
my $est_cfg = $c->config->{hyperestraier}; |
65 |
$est_cfg->{'log'} = $log; |
66 |
|
67 |
$est_cfg->{encoding} = $est_cfg->{catalyst_encoding} || $c->config->{catalyst_encoding} or $c->log->fatal("can't find catalyst_encoding"); |
68 |
|
69 |
$log->debug("using config:" . Dumper($est_cfg) ); |
70 |
|
71 |
if (! $est_cfg->{database}) { |
72 |
my $defaultnode = $est_cfg->{defaultnode} || $log->logdie("can't find defaultnode in estraier configuration"); |
73 |
$log->info("using default node $defaultnode"); |
74 |
$est_cfg->{database} = $defaultnode; |
75 |
} |
76 |
|
77 |
my $url = $est_cfg->{masterurl} . '/node/' . $est_cfg->{database}; |
78 |
|
79 |
$log->info("opening Hyper Estraier index $url as $est_cfg->{'user'}"); |
80 |
|
81 |
$self->{est_node} = Search::Estraier::Node->new( |
82 |
url => $url, |
83 |
user => $est_cfg->{user}, |
84 |
passwd => $est_cfg->{passwd}, |
85 |
); |
86 |
|
87 |
$log->fatal("can't create Search::Estraier::Node $url") unless ($self->{est_node}); |
88 |
|
89 |
# save config parametars in object |
90 |
foreach my $f (qw/db_path template_path hits_on_page webpac_encoding defaultdepth/) { |
91 |
$self->{$f} = $c->config->{hyperestraier}->{$f} || |
92 |
$c->config->{webpac}->{$f}; |
93 |
$log->debug("self->{$f} = " . $self->{$f}); |
94 |
} |
95 |
my $db_path = $self->{db_path}; |
96 |
my $template_path = $self->{template_path}; |
97 |
|
98 |
$log->debug("using db path '$db_path', template path '$template_path'"); |
99 |
|
100 |
$self->{db} = new WebPAC::Store( |
101 |
path => $db_path, |
102 |
read_only => 1, |
103 |
database => $est_cfg->{database}, |
104 |
); |
105 |
|
106 |
# default template from config.yaml |
107 |
$self->{template} ||= $c->config->{webpac}->{template}; |
108 |
|
109 |
$log->debug("converting encoding from webpac_encoding '" . |
110 |
$c->config->{webpac}->{webpac_encoding} . |
111 |
"'" |
112 |
); |
113 |
|
114 |
$self->{databases} = $c->config->{databases} || $log->fatal("can't find databases in config"); |
115 |
|
116 |
# create Template toolkit instance |
117 |
$self->{'tt'} = Template->new( |
118 |
INCLUDE_PATH => $template_path, |
119 |
FILTERS => { |
120 |
dump_html => sub { |
121 |
return unless (@_); |
122 |
my $out; |
123 |
my $i = 1; |
124 |
foreach my $v (@_) { |
125 |
$out .= qq{<div id="dump_$i">} . |
126 |
Data::HTMLDumper->Dump([ $v ],[ "v$i" ]) . |
127 |
qq{</div>}; |
128 |
$i++; |
129 |
} |
130 |
$out =~ s!<table[^>/]*>!<table class="dump">!gis if ($out); |
131 |
return $out; |
132 |
} |
133 |
}, |
134 |
EVAL_PERL => 1, |
135 |
); |
136 |
|
137 |
return $self; |
138 |
|
139 |
} |
140 |
|
141 |
|
142 |
=head2 search |
143 |
|
144 |
my $m->search( |
145 |
phrase => 'query phrase', |
146 |
add_attr => \@add_attr |
147 |
get_attr => [ '@uri' ], |
148 |
max => 42, |
149 |
template => 'result_template.tt', |
150 |
depth => 1, |
151 |
); |
152 |
|
153 |
All fields are standard C<WebPAC::Search::Estraier> parametars except |
154 |
C<template> which will (if specified) return results in HTML using |
155 |
selected template. |
156 |
|
157 |
=cut |
158 |
|
159 |
sub search { |
160 |
my $self = shift; |
161 |
|
162 |
my $search_start_t = time(); |
163 |
|
164 |
my $args = {@_}; |
165 |
|
166 |
my $log = $self->{log}; |
167 |
|
168 |
$log->debug("search args: " . Dumper( $args )); |
169 |
|
170 |
my $query = $args->{phrase} || $log->warn("no query phrase") && return; |
171 |
|
172 |
my $template_filename = $args->{template} || $self->{template}; |
173 |
|
174 |
$args->{max} ||= $self->{'hits_for_pager'}; |
175 |
if (! $args->{max}) { |
176 |
$args->{max} = 100; |
177 |
$log->warn("max not set when calling model. Using default of $args->{max}"); |
178 |
} |
179 |
|
180 |
my $times; # store some times for benchmarking |
181 |
|
182 |
my $t = time(); |
183 |
|
184 |
# transfer depth of search |
185 |
if (! $args->{depth}) { |
186 |
my $default = $self->{defaultdepth} || $log->logdie("can't find defaultdepth in estraier configuration"); |
187 |
$args->{depth} = $default; |
188 |
$log->warn("using default search depth $default"); |
189 |
} |
190 |
$args->{depth} ||= 0; |
191 |
|
192 |
$log->debug("searching for maximum $args->{max} results using depth $args->{depth} phrase: ", $query || '[none]'); |
193 |
|
194 |
# |
195 |
# construct condition for Hyper Estraier |
196 |
# |
197 |
my $cond = Search::Estraier::Condition->new(); |
198 |
if ( ref($args->{add_attr}) eq 'ARRAY' ) { |
199 |
$log->debug("adding search attributes: " . join(", ", @{ $args->{add_attr} }) ); |
200 |
map { |
201 |
$cond->add_attr( $_ ); |
202 |
$log->debug(" + $_"); |
203 |
} @{ $args->{add_attr} }; |
204 |
}; |
205 |
|
206 |
$cond->set_phrase( $query ) if ($query); |
207 |
$cond->set_options( $args->{options} ) if ($args->{options}); |
208 |
$cond->set_order( $args->{order} ) if ($args->{order}); |
209 |
|
210 |
my $max = $args->{max} || 7; |
211 |
my $page = $args->{page} || 1; |
212 |
if ($page < 1) { |
213 |
$log->warn("page number $page < 1"); |
214 |
$page = 1; |
215 |
} |
216 |
|
217 |
$cond->set_max( $page * $max ); |
218 |
|
219 |
my $result = $self->{est_node}->search($cond, $args->{depth}); |
220 |
my $hits = $result->doc_num; |
221 |
|
222 |
$times->{est} += time() - $t; |
223 |
|
224 |
$log->debug( sprintf("search took %.6fs and returned $hits hits.", $times->{est}) ); |
225 |
|
226 |
$log->debug( "hints: " . Dumper($result->{hints}) ); |
227 |
|
228 |
# |
229 |
# fetch results |
230 |
# |
231 |
|
232 |
my @results; |
233 |
|
234 |
for my $i ( (($page - 1) * $max) .. ( $hits - 1 ) ) { |
235 |
|
236 |
$t = time(); |
237 |
|
238 |
#$log->debug("get_doc($i)"); |
239 |
my $doc = $result->get_doc( $i ); |
240 |
if (! $doc) { |
241 |
$log->warn("can't find result $i"); |
242 |
next; |
243 |
} |
244 |
|
245 |
my $hash; |
246 |
|
247 |
foreach my $attr (@{ $args->{get_attr} }) { |
248 |
my $val = $doc->attr( $attr ); |
249 |
#$log->debug("attr $attr = ", $val || 'undef'); |
250 |
$hash->{$attr} = $val if (defined($val)); |
251 |
} |
252 |
|
253 |
$times->{hash} += time() - $t; |
254 |
|
255 |
next unless ($hash); |
256 |
|
257 |
if (! $args->{'template'}) { |
258 |
push @results, $hash; |
259 |
} else { |
260 |
my ($database, $prefix, $id); |
261 |
|
262 |
if ( $hash->{'@uri'} =~ m!/([^/]+)/([^/]+)/(\d+)$!) { |
263 |
($database, $prefix,$id) = ($1,$2,$3); |
264 |
} else { |
265 |
$log->warn("can't decode database/prefix/id from " . $hash->{'@uri'}); |
266 |
next; |
267 |
} |
268 |
|
269 |
#$log->debug("load_ds( id => $id, prefix => '$prefix' )"); |
270 |
|
271 |
$t = time(); |
272 |
|
273 |
my $ds = $self->{db}->load_ds( database => $database, prefix => $prefix, id => $id ); |
274 |
if (! $ds) { |
275 |
$log->error("can't load_ds( ${database}/${prefix}/${id} )"); |
276 |
next; |
277 |
} |
278 |
|
279 |
$times->{db} += time() - $t; |
280 |
|
281 |
#$log->debug( "ds = " . Dumper( \@html_results ) ); |
282 |
|
283 |
$t = time(); |
284 |
|
285 |
my $html = $self->apply( |
286 |
template => $template_filename, |
287 |
data => $ds, |
288 |
record_uri => "${database}/${prefix}/${id}", |
289 |
config => $self->{databases}->{$database}, |
290 |
); |
291 |
|
292 |
$times->{apply} += time() - $t; |
293 |
|
294 |
$t = time(); |
295 |
|
296 |
$html = decode($self->{webpac_encoding}, $html); |
297 |
|
298 |
$times->{decode} += time() - $t; |
299 |
|
300 |
push @results, $html; |
301 |
} |
302 |
|
303 |
} |
304 |
|
305 |
#$log->debug( '@results = ' . Dumper( \@results ) ); |
306 |
|
307 |
$log->debug( sprintf( |
308 |
"duration breakdown: estraier %.6fs, hash %.6fs, store %.6fs, apply %.6fs, decode %.06f, total: %.6fs", |
309 |
$times->{est}, $times->{hash}, $times->{db}, $times->{apply}, $times->{decode}, time() - $search_start_t, |
310 |
) ); |
311 |
|
312 |
return \@results; |
313 |
} |
314 |
|
315 |
=head2 record |
316 |
|
317 |
my $html = $m->record( |
318 |
mfn => 42, |
319 |
template => 'foo.tt', |
320 |
); |
321 |
|
322 |
This will load one record, convert it to html using C<template> and return |
323 |
it. |
324 |
|
325 |
=cut |
326 |
|
327 |
sub record { |
328 |
my $self = shift; |
329 |
|
330 |
my $args = {@_}; |
331 |
my $log = $self->{log}; |
332 |
$log->debug("record args: " . Dumper( $args )); |
333 |
|
334 |
foreach my $f (qw/record_uri template/) { |
335 |
$log->fatal("need $f") unless ($args->{$f}); |
336 |
} |
337 |
|
338 |
my ($database, $prefix, $id); |
339 |
|
340 |
if ($args->{record_uri} =~ m#^([^/]+)/([^/]+)/([^/]+)$#) { |
341 |
($database, $prefix, $id) = ($1,$2,$3); |
342 |
} else { |
343 |
$log->error("can't parse $args->{record_uri} into prefix, database and uri"); |
344 |
return; |
345 |
} |
346 |
|
347 |
my $ds = $self->{db}->load_ds( id => $id, prefix => $prefix, database => $database ); |
348 |
if (! $ds) { |
349 |
$log->error("can't load_ds( $database/$prefix/$id )"); |
350 |
return; |
351 |
} |
352 |
|
353 |
my $html = $self->apply( |
354 |
template => $args->{template}, |
355 |
data => $ds, |
356 |
record_uri => $args->{record_uri}, |
357 |
config => $self->{databases}->{$database}, |
358 |
); |
359 |
|
360 |
$html = decode($self->{webpac_encoding}, $html); |
361 |
|
362 |
return $html; |
363 |
} |
364 |
|
365 |
|
366 |
=head2 save_html |
367 |
|
368 |
$m->save_html( '/full/path/to/file', $content ); |
369 |
|
370 |
It will use C<Encode> to convert content encoding back to |
371 |
Webpac codepage, recode JavaScript Unicode entities (%u1234), |
372 |
strip extra newlines at beginning and end, and save to |
373 |
C</full/path/to/file.new> and if that succeeds, just rename |
374 |
it over original file which should be atomic on filesystem level. |
375 |
|
376 |
=cut |
377 |
|
378 |
sub save_html { |
379 |
my ($self, $path, $content) = @_; |
380 |
|
381 |
# FIXME Should this be UTF-8 or someting? |
382 |
my $js_encoding = $self->{webpac_encoding}; |
383 |
$js_encoding = 'UTF-16'; |
384 |
|
385 |
sub _conv_js { |
386 |
return '0x' . $_[1]; |
387 |
return encode($_[0], chr(hex($_[1]))); |
388 |
} |
389 |
#$content =~ s/%u([a-fA-F0-9]{4})/_conv_js($js_encoding,$1)/gex; |
390 |
$content =~ s/^[\n\r]+//s; |
391 |
$content =~ s/[\n\r]+$/\n/s; |
392 |
$content =~ s/\n\r/\n/gs; |
393 |
|
394 |
my $disk_encoding = $self->{webpac_encoding} || 'utf-8'; |
395 |
$self->{log}->debug("convert encoding to $disk_encoding"); |
396 |
from_to($content, 'utf-8', $disk_encoding) || $self->{log}->warn("encoding from utf-8 to $disk_encoding failed for: $content"); |
397 |
|
398 |
write_file($path . '.new', {binmode => ':raw' }, $content) || die "can't save ${path}.new $!"; |
399 |
rename $path . '.new', $path || die "can't rename to $path: $!"; |
400 |
} |
401 |
|
402 |
=head2 load_html |
403 |
|
404 |
my $html = $m->load_html('/full/path/to/file'); |
405 |
|
406 |
This will convert file from Webpac encoding to Catalyst and |
407 |
convert that data to escaped HTML (for sending into |
408 |
C<< <textarea/> >> tags in html. |
409 |
|
410 |
=cut |
411 |
|
412 |
sub load_html { |
413 |
my ($self, $path) = @_; |
414 |
|
415 |
die "no path?" unless ($path); |
416 |
|
417 |
my $content = read_file($path, {binmode => ':raw' }) || die "can't read $path: $!"; |
418 |
|
419 |
return decode($self->{webpac_encoding}, $content); |
420 |
} |
421 |
|
422 |
|
423 |
=head2 apply |
424 |
|
425 |
Create output from in-memory data structure using Template Toolkit template. |
426 |
|
427 |
my $text = $tt->apply( |
428 |
template => 'text.tt', |
429 |
data => $ds, |
430 |
record_uri => 'database/prefix/mfn', |
431 |
); |
432 |
|
433 |
It also has follwing template toolikit filter routies defined: |
434 |
|
435 |
=cut |
436 |
|
437 |
sub apply { |
438 |
my $self = shift; |
439 |
|
440 |
my $args = {@_}; |
441 |
|
442 |
my $log = $self->{log} || die "no log?"; |
443 |
|
444 |
foreach my $a (qw/template data/) { |
445 |
$log->fatal("need $a") unless ($args->{$a}); |
446 |
} |
447 |
|
448 |
=head3 tt_filter_type |
449 |
|
450 |
filter to return values of specified from $ds, usage from TT template is in form |
451 |
C<d('FieldName','delimiter')>, where C<delimiter> is optional, like this: |
452 |
|
453 |
[% d('Title') %] |
454 |
[% d('Author',', ' %] |
455 |
|
456 |
=cut |
457 |
|
458 |
sub tt_filter_type { |
459 |
my ($data,$type) = @_; |
460 |
|
461 |
die "no data?" unless ($data); |
462 |
$type ||= 'display'; |
463 |
|
464 |
my $default_delimiter = { |
465 |
'display' => '¶<br/>', |
466 |
'index' => '\n', |
467 |
}; |
468 |
|
469 |
return sub { |
470 |
|
471 |
my ($name,$join) = @_; |
472 |
|
473 |
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
474 |
# Hm? Should we die here? |
475 |
return unless ($name); |
476 |
|
477 |
my $item = $data->{'data'}->{$name} || return; |
478 |
|
479 |
my $v = $item->{$type} || return; |
480 |
|
481 |
if (ref($v) eq 'ARRAY') { |
482 |
if ($#{$v} == 0) { |
483 |
$v = $v->[0]; |
484 |
} else { |
485 |
$join = $default_delimiter->{$type} unless defined($join); |
486 |
$v = join($join, @{$v}); |
487 |
} |
488 |
} else { |
489 |
warn("TT filter $type(): field $name values aren't ARRAY, ignoring"); |
490 |
} |
491 |
|
492 |
return $v; |
493 |
} |
494 |
} |
495 |
|
496 |
$args->{'d'} = tt_filter_type($args, 'display'); |
497 |
$args->{'display'} = tt_filter_type($args, 'display'); |
498 |
|
499 |
=head3 tt_filter_search |
500 |
|
501 |
filter to return links to search, usage in TT: |
502 |
|
503 |
[% search('FieldToDisplay','FieldToSearch','optional delimiter', 'optional_template.tt') %] |
504 |
|
505 |
=cut |
506 |
|
507 |
sub tt_filter_search { |
508 |
|
509 |
my ($data) = @_; |
510 |
|
511 |
die "no data?" unless ($data); |
512 |
|
513 |
return sub { |
514 |
|
515 |
my ($display,$search,$delimiter,$template) = @_; |
516 |
|
517 |
# default delimiter |
518 |
$delimiter ||= '¶<br/>', |
519 |
|
520 |
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
521 |
# Hm? Should we die here? |
522 |
return unless ($display); |
523 |
|
524 |
my $item = $data->{'data'}->{$display} || return; |
525 |
|
526 |
return unless($item->{'display'}); |
527 |
if (! $item->{'search'}) { |
528 |
warn "error in TT template: field $display didn't insert anything into search, use d('$display') and not search('$display'...)"; |
529 |
return; |
530 |
} |
531 |
|
532 |
my @warn; |
533 |
foreach my $type (qw/display search/) { |
534 |
push @warn, "field $display type $type values aren't ARRAY" unless (ref($item->{$type}) eq 'ARRAY'); |
535 |
} |
536 |
|
537 |
if (@warn) { |
538 |
warn("TT filter search(): " . join(",", @warn) . ", skipping"); |
539 |
return; |
540 |
} |
541 |
my @html; |
542 |
|
543 |
my $d_el = $#{ $item->{'display'} }; |
544 |
my $s_el = $#{ $item->{'search'} }; |
545 |
|
546 |
# easy, both fields have same number of elements or there is just |
547 |
# one search and multiple display |
548 |
if ( $d_el == $s_el || $s_el == 0 ) { |
549 |
|
550 |
foreach my $i ( 0 .. $d_el ) { |
551 |
|
552 |
my $s; |
553 |
if ($s_el > 0) { |
554 |
$s = $item->{'search'}->[$i] or warn "can't find value $i for type search in field $search"; |
555 |
} else { |
556 |
$s = $item->{'search'}->[0]; |
557 |
} |
558 |
#$s =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/eg; |
559 |
$s = __quotemeta( $s ); |
560 |
|
561 |
my $d = $item->{'display'}->[$i] or warn "can't find value $i for type display in field $display"; |
562 |
|
563 |
my $template_arg = ''; |
564 |
$template_arg = qq{,'$template'} if ($template); |
565 |
|
566 |
push @html, qq{<a href="#" onclick="return search_via_link('$search','$s'${template_arg})">$d</a>}; |
567 |
} |
568 |
|
569 |
return join($delimiter, @html); |
570 |
} else { |
571 |
my $html = qq{<div class="notice">WARNING: we should really support if there is $d_el display elements and $s_el search elements, but currently there is no nice way to do so, so we will just display values</div>}; |
572 |
my $v = $item->{'display'}; |
573 |
|
574 |
if ($#{$v} == 0) { |
575 |
$html .= $v->[0]; |
576 |
} else { |
577 |
$html .= join($delimiter, @{$v}); |
578 |
} |
579 |
return $html; |
580 |
} |
581 |
} |
582 |
} |
583 |
|
584 |
$args->{'search'} = tt_filter_search($args); |
585 |
|
586 |
=head3 load_rec |
587 |
|
588 |
Used mostly for onClick events like this: |
589 |
|
590 |
<a href="#" onClick="[% load_rec( record_uri, 'template_name.tt') %]>foo</a> |
591 |
|
592 |
It will automatically do sanity checking and create correct JavaScript code. |
593 |
|
594 |
=cut |
595 |
|
596 |
$args->{'load_rec'} = sub { |
597 |
my @errors; |
598 |
|
599 |
my $record_uri = shift or push @errors, "record_uri missing"; |
600 |
my $template = shift or push @errors, "template missing"; |
601 |
|
602 |
if ($record_uri !~ m#^[^/]+/[^/]+/[^/]+$#) { |
603 |
push @errors, "invalid format of record_uri: $record_uri"; |
604 |
} |
605 |
|
606 |
if (@errors) { |
607 |
return "Logger.error('errors in load_rec: " . join(", ", @errors) . "'); return false;"; |
608 |
} else { |
609 |
return "load_rec('$record_uri','$template'); return false;"; |
610 |
} |
611 |
}; |
612 |
|
613 |
=head3 load_template |
614 |
|
615 |
Used to re-submit search request and load results in different template |
616 |
|
617 |
<a href="#" onClick="[% load_template( 'template_name.tt' ) %]">bar</a> |
618 |
|
619 |
=cut |
620 |
|
621 |
$args->{'load_template'} = sub { |
622 |
my $template = shift or return "Logger.error('load_template missing template name!'); return false;"; |
623 |
return "load_template($template); return false;"; |
624 |
}; |
625 |
|
626 |
my $out; |
627 |
|
628 |
$self->{'tt'}->process( |
629 |
$args->{'template'}, |
630 |
$args, |
631 |
\$out |
632 |
) || $log->error( "apply can't process template: ", $self->{'tt'}->error() ); |
633 |
|
634 |
return $out; |
635 |
} |
636 |
|
637 |
|
638 |
=head2 __quotemeta |
639 |
|
640 |
Helper to quote JavaScript-friendly characters |
641 |
|
642 |
=cut |
643 |
|
644 |
sub __quotemeta { |
645 |
local $_ = shift; |
646 |
$_ = decode('iso-8859-2', $_); |
647 |
|
648 |
s<([\x{0080}-\x{fffd}]+)>{sprintf '\u%0*v4X', '\u', $1}ge if ( Encode::is_utf8($_) ); |
649 |
{ |
650 |
use bytes; |
651 |
s<((?:[^ \x21-\x7E]|(?:\\(?!u)))+)>{sprintf '\x%0*v2X', '\x', $1}ge; |
652 |
} |
653 |
|
654 |
s/\\x09/\\t/g; |
655 |
s/\\x0A/\\n/g; |
656 |
s/\\x0D/\\r/g; |
657 |
s/"/\\"/g; |
658 |
s/\\x5C/\\\\/g; |
659 |
|
660 |
return $_; |
661 |
} |
662 |
|
663 |
|
664 |
|
665 |
=head1 AUTHOR |
666 |
|
667 |
Dobrica Pavlinusic C<< <dpavlin@rot13.org> >> |
668 |
|
669 |
=head1 LICENSE |
670 |
|
671 |
This library is free software, you can redistribute it and/or modify |
672 |
it under the same terms as Perl itself. |
673 |
|
674 |
=cut |
675 |
|
676 |
1; |