File Coverage

blib/lib/Data/Define.pm
Criterion Covered Total %
statement 35 37 94.5
branch 13 14 92.8
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 60 63 95.2


line stmt bran cond sub pod time code
1             # Data/Define.pm
2             #
3             # Copyright (c) 2006 Serguei Trouchelle. All rights reserved.
4             #
5             # This program is free software; you can redistribute it and/or modify it
6             # under the same terms as Perl itself.
7            
8             # History:
9             # 1.03 2007/02/04 Quality update (Test::Pod, Test::Pod::Coverage, eg)
10             # 1.02 2006/11/02 Dist fixed
11             # 1.01 2006/09/07 Initial revision
12            
13             =head1 NAME
14            
15             Data::Define - Make undef's defined
16            
17             =head1 SYNOPSIS
18            
19             use Data::Define;
20            
21             print define undef; # prints ''
22            
23            
24             use Data::Define qw/ brockets /;
25            
26             print define undef; # prints '';
27            
28            
29             use Data::Define qw/ define_html brockets /;
30            
31             print define_html undef; # prints '<undef>';
32            
33             use Data::Define qw/ define_html div-class-undef /;
34            
35             print define_html undef; # prints '<undef>';
36            
37            
38             =head1 DESCRIPTION
39            
40             Data::Define
41            
42             =head1 METHODS
43            
44             =head2 define
45            
46             This method takes one parameter and returns it defined even if it was not
47             defined primordially.
48            
49             This method is exported by default.
50            
51             Default return value is ''. If you asked to export 'brockets' using
52             Data::Define, return value becomes 'EundefE'.
53             You can specify your own default value using Data::Define-EL.
54            
55             =head2 define_html
56            
57             This method works exactly the same as 'define', but when exporting 'brockets',
58             return value becomes '<undef*gt;', so you can send it to HTML browser
59             without need to escape.
60            
61             Additionally, you can ask to export 'div-class-undef', then return value will be
62             'Ediv class="undef"EE/divE'.
63            
64             You can specify your own default value using Data::Define-EL.
65            
66             =head2 set_undef_value( $value )
67            
68             This method allows you to specify your own default value for define.
69             Usage is Cset_undef_value( $value )>.
70            
71             If $value is not defined, default value ('', or 'EundefE' if 'brockets'
72             is exported) is used.
73            
74             =head2 set_undef_value_html( $value )
75            
76             This method allows you to specify your own default value for define_html.
77             Usage is Cset_undef_value_html( $value )>.
78            
79             If $value is not defined, default value ('', or '<undef>' if 'brockets'
80             is exported, or 'Ediv class="undef"EE/divE' if
81             'div-class-undef' is exported) is used.
82            
83             =cut
84            
85             package Data::Define;
86            
87             require Exporter;
88 8     8   52228 use Config;
  8         16  
  8         355  
89            
90 8     8   38 use strict;
  8         15  
  8         235  
91 8     8   46 use warnings;
  8         17  
  8         4508  
92            
93             our @EXPORT = qw/ define /;
94             our @EXPORT_OK = qw/ define_html div-class-undef brockets /;
95             our %EXPORT_TAGS = ();
96             our @ISA = qw/Exporter/;
97            
98             $Data::Define::VERSION = "1.03";
99            
100             our $UNDEFVALUE = '';
101             our $UNDEFVALUEHTML = '';
102             our $DEFAULT_UNDEFVALUE = '';
103             our $DEFAULT_UNDEFVALUEHTML = '';
104            
105             sub define {
106 33     33 1 760 my $val = shift;
107 33 100       192 return defined $val ? $val : $UNDEFVALUE;
108             }
109            
110             sub define_html {
111 22     22 1 40 my $val = shift;
112 22 100       110 return defined $val ? $val : $UNDEFVALUEHTML;
113             }
114            
115             sub set_undef_value {
116 9     9 1 49 my $self = shift;
117 9         19 $UNDEFVALUE = shift;
118 9 100       38 $UNDEFVALUE = $DEFAULT_UNDEFVALUE unless defined $UNDEFVALUE;
119             }
120            
121             sub set_undef_value_html {
122 9     9 1 32 my $self = shift;
123 9         17 $UNDEFVALUEHTML = shift;
124 9 100       32 $UNDEFVALUEHTML = $DEFAULT_UNDEFVALUEHTML unless defined $UNDEFVALUEHTML;
125             }
126            
127             sub import {
128 8     8   53 my $pkg = shift;
129 8         14 my %routines;
130             my @name;
131            
132 8 50       56 if (@name = grep m/^name=/, @_) {
133 0         0 my $n = (split(/=/,$name[0]))[1];
134 0         0 @_ = grep !/^name=/, @_;
135             }
136 8         46 grep $routines{$_}++, @_, @EXPORT ;
137            
138 8 100       32 if ($routines{'brockets'}) {
139 3         6 $UNDEFVALUEHTML = '<undef>';
140 3         6 $UNDEFVALUE = '';
141 3         25 $DEFAULT_UNDEFVALUE = '';
142 3         7 $DEFAULT_UNDEFVALUEHTML = '<undef>'; # for set_undef_value with undefined param
143             }
144 8 100       39 if ($routines{'div-class-undef'}) {
145 3         6 $UNDEFVALUEHTML = '
';
146 3         16 $DEFAULT_UNDEFVALUEHTML = '
'; # for set_undef_value_html with undefined param
147             }
148            
149 8         12 my $oldlevel = $Exporter::ExportLevel;
150 8         29 $Exporter::ExportLevel = 1;
151 8         505 Exporter::import($pkg, keys %routines);
152 8         14546 $Exporter::ExportLevel = $oldlevel;
153             }
154            
155            
156             1;
157            
158             =head1 AUTHORS
159            
160             Serguei Trouchelle EFE
161            
162             =head1 COPYRIGHT
163            
164             Copyright (c) 2006 Serguei Trouchelle. All rights reserved.
165            
166             This program is free software; you can redistribute it and/or modify it
167             under the same terms as Perl itself.
168            
169             =cut