File Coverage

blib/lib/Object/Exercise.pm
Criterion Covered Total %
statement 26 31 83.8
branch 8 18 44.4
condition n/a
subroutine 4 4 100.0
pod n/a
total 38 53 71.7


line stmt bran cond sub pod time code
1             # $Id: Exercise.pm 266 2009-06-04 13:48:25Z lembark $
2             #######################################################################
3             # housekeeping
4             #######################################################################
5              
6             package Object::Exercise;
7              
8             require 5.6.2; # I'm running 5.8.8; hopefully this is reasonable...
9              
10 5     5   27067 use strict;
  5         11  
  5         211  
11              
12 5     5   7095 use Symbol qw( qualify_to_ref );
  5         5684  
  5         441  
13              
14 5     5   3228 use Object::Exercise::Common qw( log_message continue verbose );
  5         20  
  5         58  
15              
16             ########################################################################
17             # package variables
18             ########################################################################
19              
20             our $VERSION = 1.02;
21              
22             ########################################################################
23             # subroutines
24             ########################################################################
25              
26             sub import
27             {
28 5     5   48 my $package = __PACKAGE__;
29 5         14 my $caller = caller;
30              
31             # discard the class argument.
32              
33 5 50       24 shift if $_[0] eq $package;
34              
35             # arguments:
36             # -k turns off fatal on error.
37             # -v turns on verbose.
38             # -b uses benchmark instead of execution handler.
39             # -e uses execution handler (default).
40             # -n specifies the installed name (vs. 'execution' or 'benchmark'.
41             # -p turns off plannng in the test loop.
42              
43 5         13 my %exportz = ();
44              
45 5         21 while( @_ )
46             {
47 1         2 my $arg = shift;
48              
49 1 50       8 if( $arg =~ /^-k/ )
    50          
    50          
    0          
50             {
51 0         0 $continue = 1;
52             }
53             elsif( $arg =~ /^-v/ )
54             {
55 0         0 $verbose = 1;
56             }
57             elsif( $arg =~ /^-b/ )
58             {
59 1 50       6 my $name = ( index $_[0], '-' ) ? 'benchmark' : shift ;
60              
61 1         5 $exportz{ benchmark } = $name;
62             }
63             elsif( $arg =~ /^-e/ )
64             {
65 0 0       0 my $name = ( index $_[0], '-' ) ? 'execute' : shift ;
66              
67 0         0 $exportz{ execute } = $name;
68             }
69             else
70             {
71 0         0 die "Bogus $package: unknown switch '$arg'";
72             }
73             }
74              
75 5 100       24 %exportz = qw( execute exercise ) unless %exportz;
76              
77 5         29 while( my($src,$dst) = each %exportz )
78             {
79 5 50       23 $log_message->( "$package installing '$src' into '$caller' as '$dst'" )
80             if $verbose;
81              
82 5         198 my $module = $package . '::' . ucfirst $src;
83              
84 5         397 my $handler = eval "require $module";
85              
86 5         36 my $ref = qualify_to_ref $dst, $caller;
87              
88 5         144 *$ref = \$handler;
89             }
90              
91             return
92 5         762 }
93              
94             # keep require happy
95              
96             1
97              
98             __END__