File Coverage

blib/lib/Date/strftimeq.pm
Criterion Covered Total %
statement 53 53 100.0
branch 4 6 66.6
condition 1 2 50.0
subroutine 14 14 100.0
pod 1 1 100.0
total 73 76 96.0


line stmt bran cond sub pod time code
1             ## no critic: Modules::ProhibitAutomaticExportation
2              
3             package Date::strftimeq;
4              
5             our $DATE = '2019-12-24'; # DATE
6             our $DIST = 'Date-strftimeq'; # DIST
7             our $VERSION = '0.002'; # VERSION
8              
9 1     1   70576 use 5.010001;
  1         14  
10 1     1   6 use strict;
  1         2  
  1         19  
11 1     1   4 use warnings;
  1         3  
  1         36  
12              
13 1     1   469 use POSIX ();
  1         6331  
  1         32  
14 1     1   7 use Scalar::Util 'blessed';
  1         2  
  1         50  
15              
16 1     1   6 use Exporter 'import';
  1         2  
  1         209  
17             our @EXPORT = qw(strftimeq);
18              
19             our $regex = qr{
20             (?(DEFINE)
21             (? ( [^()]+ | \((?&def_code)\) )*)
22             )
23             (?
24              
25             (?
26             %
27             (? [_0^#-]+)?
28             (? [0-9]+)?
29             (?[EO])?
30             (? [%aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYZz+])
31             )|
32             (?
33             %\(
34             (? (?&def_code))
35             \)q)
36             )
37             }x;
38              
39             # faster version, without using named capture
40             if (0) {
41             }
42              
43             sub strftimeq {
44 6     6 1 3020 my ($format, @time) = @_;
45              
46 6         20 my ($caller_pkg) = caller();
47 6         10 my %compiled_code;
48              
49 6         64 $format =~ s{$regex}{
50             # for faster acccess
51 1     1   457 my %m = %+;
  1         335  
  1         186  
  19         199  
52              
53             #use DD; dd \%m; # DEBUG
54              
55 19 100       64 if (exists $m{code}) {
56 3 50       21 unless (defined $compiled_code{$m{code}}) {
57             #say "D: compiling $m{code}"; # DEBUG
58 1     1   7 $compiled_code{$m{code}} = eval "package $caller_pkg; no strict; no warnings; sub { $m{code} }";
  1     1   2  
  1     1   26  
  1     1   5  
  1     1   1  
  1     1   70  
  1         7  
  1         2  
  1         22  
  1         5  
  1         2  
  1         93  
  1         6  
  1         2  
  1         21  
  1         5  
  1         2  
  1         55  
  3         196  
59 3 50       12 die "Can't compile code in $m{all}: $@" if $@;
60             }
61 3         81 my $code_res = $compiled_code{$m{code}}->(@time);
62 3   50     132 $code_res //= "";
63 3         7 $code_res =~ s/%/%%/g;
64 3         14 $code_res;
65             } else {
66 16         94 $m{all};
67             }
68             }xego;
69              
70 6         256 POSIX::strftime($format, @time);
71             }
72              
73             1;
74             # ABSTRACT: POSIX::strftime() with support for embedded perl code in %(...)q
75              
76             __END__