DashProfiler/Import.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 75 CC 8 SCAR 38.1
CC
8
Cov
0%
import
72.0
BEGIN
2.0
BEGIN
2.0
CRAP 45.1
1 package DashProfiler::Import;
2  
3 use strict;
4  
5 our $VERSION = sprintf("1.%06d", q$Revision: 45 $ =~ /(\d+)/o);
6  
7 use base qw(Exporter);
8  
9 use Carp;
10  
11 use DashProfiler;
12  
13 our $ExportLevel = 0;
14  
15 =head1 NAME
16  
17 DashProfiler::Import - Import curried DashProfiler sampler function at compile-time
18  
19 =head1 SYNOPSIS
20  
21 use DashProfiler::Import foo_profiler => [ "my context 1" ];
22  
23 use DashProfiler::Import foo_profiler => [ "my context 1" ],
24 bar_profiler => [ "my context 1", context2edit => sub { ... } ];
25  
26 use DashProfiler::Import -optional, baz_profiler => [ "my context 1" ];
27  
28 ...
29 my $sample = foo_profiler("baz");
30  
31 =head1 DESCRIPTION
32  
33 Firstly, read L<DashProfiler::UserGuide> for a general introduction.
34  
35 The example above imports a function called foo_profiler() that is a sample
36 factory for the DashProfiler called "foo", pre-configured ("curried") to
37 use the value "bar" for context1.
38  
39 =head2 Using *_profiler_enabled()
40  
41 It also imports a function called foo_profiler_enabled() that's a constant,
42 returning false if the named DashProfiler was disabled at the time.
43  
44 This is useful when profiling very time-senstive code and you want the
45 profiling to have I<zero> overhead when not in use. For example:
46  
47 my $sample = foo_profiler("baz") if foo_profiler_enabled();
48  
49 Because the C<*_profiler_enabled> function is a constant, the perl compiler
50 will completely remove the code if the corresponding DashProfiler is disabled.
51  
52 If there is no DashProfiler called "foo" then you'll get a compile-time error
53 unless the C<-optional> directive has been given first.
54  
55 Generally this style of code in perl is considered bad practice and error prone:
56  
57 my $var = ... if ...;
58  
59 because the behaviour when the condition is false on one execution having been
60 true on previous execution is not well defined (on purpose, because it's
61 surprisingly hard to explain what it does, and anyway, it may change).
62  
63 For the DashProfiler::Import module, however, that style of code is just fine.
64 That's because the condition is a compile-time constant.
65  
66 =cut
67  
68 sub import {
69 my $class = shift;
70 my $pkg = caller($ExportLevel);
71  
72 my $optional = 0;
73  
74 while (@_) {
75 local $_ = shift;
76  
77 if (m/^[-:](\w+)/) { # the ':optional' form is deprecated
78 if ($1 eq 'optional') {
79 $optional = 1;
80 }
81 else {
82 croak "Unknown DashProfiler::Import directive '$_'";
83 }
84 next;
85 }
86  
87 m/^((\w+)_profiler)$/
88 or croak "$class name '$_' must end with _profiler";
89 my ($var_name, $profile_name) = ($1, $2);
90 my $args = shift;
91  
92 my $profile = DashProfiler->get_profile($profile_name);
93 if (!$profile) {
94 croak "No profile called '$profile_name' has been defined"
95 unless $optional;
96 # fall-thru to check args and create stubs
97 }
98  
99 croak "$var_name => ... requires an array ref containing at least one element"
100 unless ref $args eq 'ARRAY' and @$args >= 1;
101 my $profiler = ($profile) ? $profile->prepare(@$args) : undef;
102  
103 #warn "$pkg $var_name ($profile_name) => $context1 $profiler";
104 {
105 no strict 'refs'; ## no critic
106 # if profile has been disabled then export a dummy sub instead
107 *{"${pkg}::$var_name"} = $profiler || sub { undef };
108 # also export a constant sub that can be used to optimize away the
109 # call to the profiler - see docs
110 *{"${pkg}::${var_name}_enabled"} = ($profiler) ? sub () { 1 } : sub () { 0 };
111 }
112 }
113 }
114  
115 1;
116  
117 =head1 AUTHOR
118  
119 DashProfiler by Tim Bunce, L<http://www.tim.bunce.name> and
120 L<http://blog.timbunce.org>
121  
122 =head1 COPYRIGHT
123  
124 The DashProfiler distribution is Copyright (c) 2007-2008 Tim Bunce. Ireland.
125 All rights reserved.
126  
127 You may distribute under the terms of either the GNU General Public
128 License or the Artistic License, as specified in the Perl README file.
129  
130 =cut
131