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-11-20'; # DATE
6             our $DIST = 'Date-strftimeq'; # DIST
7             our $VERSION = '0.001'; # VERSION
8              
9 1     1   65915 use 5.010001;
  1         12  
10 1     1   5 use strict;
  1         3  
  1         18  
11 1     1   5 use warnings;
  1         1  
  1         38  
12              
13 1     1   487 use POSIX ();
  1         6163  
  1         30  
14 1     1   7 use Scalar::Util 'blessed';
  1         2  
  1         50  
15              
16 1     1   6 use Exporter 'import';
  1         1  
  1         200  
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 3026 my ($format, @time) = @_;
45              
46 6         20 my ($caller_pkg) = caller();
47 6         10 my %compiled_code;
48              
49 6         83 $format =~ s{$regex}{
50             # for faster acccess
51 1     1   412 my %m = %+;
  1         335  
  1         184  
  19         196  
52              
53             #use DD; dd \%m; # DEBUG
54              
55 19 100       64 if (exists $m{code}) {
56 3 50       18 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   2  
  1     1   64  
  1         6  
  1         2  
  1         22  
  1         5  
  1         1  
  1         60  
  1         7  
  1         2  
  1         21  
  1         5  
  1         2  
  1         56  
  3         199  
59 3 50       13 die "Can't compile code in $m{all}: $@" if $@;
60             }
61 3         80 my $code_res = $compiled_code{$m{code}}->(@time);
62 3   50     133 $code_res //= "";
63 3         6 $code_res =~ s/%/%%/g;
64 3         13 $code_res;
65             } else {
66 16         91 $m{all};
67             }
68             }xego;
69              
70 6         249 POSIX::strftime($format, @time);
71             }
72              
73             1;
74             # ABSTRACT: POSIX::strftime() with support for embedded perl code in %(...)q
75              
76             __END__