blib/lib/Fax.pm untested

filter: Statementstmt 0.0% 0 / 132 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 191 CC 15 SCAR 29.0
CC
15
Cov
0%
AUTOLOAD
56.0
DESTROY
20.0
new
12.0
CRAP 18.3
1 package Data::Fax;
2  
3 =head1 NAME
4  
5 Data::Fax - Perl extension for setting up DataFAX object.
6  
7 =head1 VERSION
8  
9 This document refers to version 0.01 of Data::Fax released April 25,
10 2001.
11  
12 =head1 SYNOPSIS
13  
14 use Data::Fax;
15 my $df = Data::Fax->new; # create an empty object
16 $df->debug(1); # set debug level
17 $df->reset('N'); # change reset to no
18  
19 # we can set it up using one line
20 my $df = Data::Fax->new('debug', '2', 'reset', 'N');
21  
22 # we can define a hash array for the parameters
23 my %param = ( 'debug'=>'1', 'reset'=>'N',
24 'df_param'=>$ary_ref), 'ss_param'=>\{});
25 my $df = Data::Fax->new(%param); # or
26 my $df = new Data::Fax %param; # or indirect obj call
27 my $df = new Data::Fax (%param); # the same with indirect obj call
28  
29 # methods to set/get values
30 $df->set_debug(0); # the same as $df->debug(0);
31 $df->get_debug; # the same as $df->debug;
32 $df->set_reset('Y'); # the same as $df->reset('Y');
33 $df->get_reset; # the same as $df->reset;
34  
35 # All the methods to get/set scalar value have corresponding
36 # methods without 'get_' or 'set_' prefix. So do the following
37 # methods:
38 $df->set_FS('\t'); # set field separator
39 $df->get_FS; # get feild separator
40 $df->set_OFS('|'); # set output field separator
41 $df->get_OFS; # get output field separator
42 $df->set_DirSep('/'); # set directory separator
43 $df->get_DirSep; # get directory separator
44 $df->set_first_row('Y'); # indicates the first row is column names
45 $df->set_first_row('N'); # first row does not contain column names
46 $df->get_first_row; # get first row indicator
47 $df->set_debug(2); # set msg level to 2
48 $df->get_debug; # get msg level
49  
50 # methods to set/get array or hash array are a little different
51 $df->set_df_param($fn); # read from a DataFAX initial file
52 $df->set_df_param($arf); # or set it to a hash array ref
53 %ha=$df->get_df_param; # get the hash array
54 $df->df_param; # get hash array ref
55 $df->df_param($key); # get value of $key from the array
56 $df->df_param($key,$val); # set the $key = $val
57 # the same for ss_param
58 $df->set_ss_param($sn); # read from DFserver.cf of study number
59 $df->set_ss_param($arf); # set it to a hash array ref
60 %ha=$df->get_ss_param; # get the hash array
61 $df->ss_param; # get hash array ref
62 $df->ss_param($key); # get value of $key from the array
63 $df->ss_param($key,$val); # set the $key = $val
64  
65 # some utility methods
66 $df->echoMSG($msg, $lvl); # echo message of level $lvl
67 $df->disp_param; # display all internal parameters
68 $df->debug; # debug
69  
70 =head1 DESCRIPTION
71  
72 Data::Fax class is intended to be used as parent class for all the
73 sub-sequent classes with Data::Fax name space.
74  
75 =head2 Overview
76  
77 I<Data::Fax> is a class for setting up DataFAX and study specific
78 environment and parameters.
79  
80 =head2 Constructor and initialization
81  
82 The constructor is I<new>. You can call it directly or indirectly with
83 or without parameters or parameter hash array. Here are some examples:
84  
85 # direct call
86 my $df = Data::Fax->new;
87 my $df = Data::Fax->new('p1', 'value1', 'p2', 'value2');
88 my $df = Data::Fax->new(%p);
89  
90 # indirect call
91 my $df = new Data::Fax;
92 my $df = new Data::Fax ('p1', 'value1', 'p2', 'value2');
93 my $df = new Data::Fax (%p);
94  
95 =cut
96  
97 require 5.005_62;
98 use strict;
99 use vars qw($AUTOLOAD);
100 use warnings;
101 use Carp;
102 # use Data::Fax::Subs qw(:misc);
103 use Debug::EchoMessage;
104  
105 # require Exporter;
106 # require DynaLoader;
107 # use AutoLoader;
108  
109 # our @ISA = qw(Exporter DynaLoader);
110 our @ISA;
111  
112 # Items to export into callers namespace by default. Note: do not export
113 # names by default without a very good reason. Use EXPORT_OK instead.
114 # Do not simply export all your public functions/methods/constants.
115  
116 # This allows declaration use Data::Fax ':all';
117 # If you do not need this, moving things directly into @EXPORT or
118 # @EXPORT_OK will save memory.
119 our %EXPORT_TAGS = ( 'all' => [ qw(
120
121 ) ] );
122  
123 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
124  
125 our @EXPORT = qw(
126
127 );
128 our $VERSION = '0.02';
129  
130 use Data::Dumper; # used in debug
131 # use Data::Subs "read_config"; # used in set_df_param
132 # use Data::Fax::Subs qw(:common :param_subs echoMSG debug read_config
133 # open_datafile);
134 # use Data::Fax::JavaScript qw(:common :functions);
135 # use Data::Subs "prtHashVar"; # used in echoMSG
136  
137 { # Encapsulated class data
138 my %_attr_data = # default accessibility
139 ( _ifs => ['$','read/write', '|'], # field separator
140 _ofs => ['$','read/write', '|'], # output field sparator
141 _IFS => ['$','read/write', '|'], # field separator
142 _OFS => ['$','read/write', '|'], # output field sparator
143 _init_file => ['$','read/write', ''], # DataFAX.ini file
144 _DirSep => ['$','read/write', '/'], # directory sparator
145 _debug => ['$','read/write', 0], # debug level: 0,1,..,N
146 _reset => ['$','read/write', 'Y'], # reset parameters: Y/N
147 _sfr => ['$','read/write', 0], # default 1st row no cols
148 _SFR => ['$','read/write', 0], # default 1st row no cols
149 _dfdb_fn => ['$','read/write',''], # default to null
150 _df_param => ['%','read/write',{}], # ary_ref for df_param
151 _sn => ['$','read/write',0 ], # active study number
152 _ss_param => ['%','read/write',{}], # ary_ref for ss_param
153 _wb_param => ['%','read/write',{}], # ary_ref for wb_param
154 _dfdb => ['%','read/write',{}], # ary_ref for DFDB
155 _dfdb_ss => ['%','read/write',{}], # study specific
156 );
157 # class methods, to operate on encapsulated class data
158 # is a specified object attribute accessible in a given mode
159 sub _accessible {
160 my ($self, $attr, $mode) = @_;
161 # if (!exists $_attr_data{$attr}) {
162 # print "\$attr = $attr\n"; $self->debug; }
163 return $_attr_data{$attr}[1] =~ /$mode/
164 if exists $_attr_data{$attr};
165 }
166 # classwide default value for a specified object attributes
167 sub _default_for {
168 my ($self, $attr) = @_;
169 return $_attr_data{$attr}[2] if exists $_attr_data{$attr};
170 }
171 sub _accs_type {
172 my ($self, $attr) = @_;
173 return $_attr_data{$attr}[0] if exists $_attr_data{$attr};
174 }
175 # list of names of all specified object attributes
176 sub _standard_keys { keys %_attr_data; }
177 }
178  
179 =head2 The Constructor new(%arg)
180  
181 Without any input, i.e., new(), the constructor generates an empty
182 object with default values for its parameters.
183  
184 If any argument is provided, the constructor expects them in
185 the name and value pairs, i.e., in a hash array.
186  
187 The constructor also calls the I<set_df_param> method to use an
188 initial file for its system parameters and I<set_dfdb> method to
189 use study database for its study parameters.
190  
191 The default initial file is B<DataFAX.ini> located in ~/Fax/DataFax/.
192 It can also be set by I<init_file> method.
193  
194 =back
195  
196 =cut
197  
198 sub new {
199 my $caller = shift;
200 my $caller_is_obj = ref($caller);
201 my $class = $caller_is_obj || $caller;
202 my $self = bless {}, $class;
203 my %arg = @_; # convert rest of inputs into hash array
204 # print join "|", $caller, $caller_is_obj, $class, $self, "\n";
205 foreach my $attrname ( $self->_standard_keys() ) {
206 my ($argname) = ($attrname =~ /^_(.*)/);
207 # print "attrname = $attrname: argname = $argname\n";
208 if (exists $arg{$argname}) {
209 $self->{$attrname} = $arg{$argname};
210 } elsif ($caller_is_obj) {
211 $self->{$attrname} = $caller->{$attrname};
212 } else {
213 $self->{$attrname} = $self->_default_for($attrname);
214 }
215 # print $attrname, " = ", $self->{$attrname}, "\n";
216 }
217 # $self->debug(5);
218 # $self->set_df_param;
219 # $self->set_dfdb;
220 return $self;
221 }
222  
223 sub input_field_sep { my $s = shift; return $s->IFS(@_); }
224 sub input_field_separator { my $s = shift; return $s->IFS(@_); }
225  
226 =head2 Class and object methods
227  
228 The following are the methods and their usages.
229  
230 =cut
231  
232 # implement other get_... and set_... method (create as neccessary)
233 sub AUTOLOAD {
234 no strict "refs";
235 my ($self, $v1, $v2) = @_;
236 (my $sub = $AUTOLOAD) =~ s/.*:://;
237 my $m = $sub;
238 (my $attr = $sub) =~ s/(get_|set_)//;
239 $attr = "_$attr";
240 # print join "|", $self, $v1, $v2, $sub, $attr,"\n";
241 my $type = $self->_accs_type($attr);
242 croak "ERR: No such method: $AUTOLOAD.\n" if !$type;
243 my $v = "";
244 my $msg = "WARN: no permission to change";
245 if ($type eq '$') { # scalar method
246 $v = "\n";
247 $v .= " my \$s = shift;\n";
248 $v .= " croak \"ERR: Too many args to $m.\" if \@_ > 1;\n";
249 if ($self->_accessible($attr, 'write')) {
250 $v .= " \@_ ? (\$s->{$attr}=shift) : ";
251 $v .= "return \$s->{$attr};\n";
252 } else {
253 $v .= " \@_ ? (carp \"$msg $m.\n\") : ";
254 $v .= "return \$s->{$attr};\n";
255 }
256 } elsif ($type eq '@') { # array method
257 $v = "\n";
258 $v .= " my \$s = shift;\n";
259 $v .= " my \$a = \$s->{$attr}; # get array ref\n";
260 $v .= " if (\@_ && (ref(\$_[0]) eq 'ARRAY' ";
261 $v .= "|| \$_[0] =~ /.*=ARRAY/)) {\n";
262 $v .= " \$s->{$attr} = shift; return;\n }\n";
263 $v .= " my \$i; # array index\n";
264 $v .= " \@_ ? (\$i=shift) : return \$a;\n";
265 $v .= " croak \"ERR: Too many args to $m.\" if \@_ > 1;\n";
266 if ($self->_accessible($attr, 'write')) {
267 $v .= " \@_ ? (\${\$a}[\$i]=shift) : ";
268 $v .= "return \${\$a}[\$i];\n";
269 } else {
270 $v .= " \@_ ? (carp \"$msg $m.\n\") : ";
271 $v .= "return \${\$a}[\$i];\n";
272 }
273 } else { # assume hash method: type = '%'
274 $v = "\n";
275 $v .= " my \$s = shift;\n";
276 $v .= " my \$a = \$s->{$attr}; # get hash array ref\n";
277 $v .= " if (\@_ && (ref(\$_[0]) eq 'HASH' ";
278 $v .= " || \$_[0] =~ /.*=HASH/)) {\n";
279 $v .= " \$s->{$attr} = shift; return;\n }\n";
280 $v .= " my \$k; # hash array key\n";
281 $v .= " \@_ ? (\$k=shift) : return \$a;\n";
282 $v .= " croak \"ERR: Too many args to $m.\" if \@_ > 1;\n";
283 if ($self->_accessible($attr, 'write')) {
284 $v .= " \@_ ? (\${\$a}{\$k}=shift) : ";
285 $v .= "return \${\$a}{\$k};\n";
286 } else {
287 $v .= " \@_ ? (carp \"$msg $m.\n\") : ";
288 $v .= "return \${\$a}{\$k};\n";
289 }
290 }
291 $self->echoMSG("sub $m {$v}\n",100);
292 *{$sub} = eval "sub {$v}";
293 goto &$sub;
294 }
295  
296 sub DESTROY {
297 my ($self) = @_;
298 # clean up base classes
299 return if !@ISA;
300 foreach my $parent (@ISA) {
301 next if $self::DESTROY{$parent}++;
302 my $destructor = $parent->can("DESTROY");
303 $self->$destructor() if $destructor;
304 }
305 }
306  
307 1;
308 __END__