File Coverage

blib/lib/Persistent/DataType/VarChar.pm
Criterion Covered Total %
statement 25 29 86.2
branch 6 14 42.8
condition 4 12 33.3
subroutine 6 6 100.0
pod 1 2 50.0
total 42 63 66.6


line stmt bran cond sub pod time code
1             ########################################################################
2             # File: VarChar.pm
3             # Author: David Winters
4             # RCS: $Id: VarChar.pm,v 1.9 2000/02/08 02:36:40 winters Exp winters $
5             #
6             # A variable length character string class.
7             #
8             # This file contains POD documentation that may be viewed with the
9             # perldoc, pod2man, or pod2html utilities.
10             #
11             # Copyright (c) 1998-2000 David Winters. All rights reserved.
12             # This program is free software; you can redistribute it
13             # and/or modify it under the same terms as Perl itself.
14             ########################################################################
15              
16             package Persistent::DataType::VarChar;
17             require 5.004;
18              
19 5     5   27 use strict;
  5         10  
  5         265  
20 5     5   27 use vars qw($VERSION $REVISION @ISA);
  5         9  
  5         375  
21              
22             ### a subclass of the all-powerful Persistent::DataType::String class ###
23 5     5   3356 use Persistent::DataType::String;
  5         14  
  5         195  
24             @ISA = qw(Persistent::DataType::String);
25              
26 5     5   28 use Carp;
  5         8  
  5         1564  
27              
28             ### copy version number from superclass ###
29             $VERSION = $Persistent::DataType::String::VERSION;
30             $REVISION = (qw$Revision: 1.9 $)[1];
31              
32             =head1 NAME
33              
34             Persistent::DataType::VarChar - A Variable Length Character String Class
35              
36             =head1 SYNOPSIS
37              
38             use Persistent::DataType::VarChar;
39             use English;
40              
41             eval { ### in case an exception is thrown ###
42              
43             ### allocate a string ###
44             my $string = new Persistent::DataType::VarChar($value,
45             $max_length);
46              
47             ### get/set value of string ###
48             $value = $string->value($new_value);
49              
50             ### get length of string ###
51             my $length = $string->length();
52              
53             ### get/set maximum length of string ###
54             my $max = $string->max_length($new_max);
55              
56             ### returns 'eq' for strings ###
57             my $cmp_op = $string->get_compare_op();
58             };
59              
60             if ($EVAL_ERROR) { ### catch those exceptions! ###
61             print "An error occurred: $EVAL_ERROR\n";
62             }
63              
64             =head1 ABSTRACT
65              
66             This is a variable length character string class used by the
67             Persistent framework of classes to implement the attributes of
68             objects. This class provides methods for accessing the value, length,
69             maximum length, and comparison operator of a variable length character
70             string. A variable length string (VarChar) always has a finite
71             maximum length that can not be exceeded. This is different from a
72             character string (String) which can have an unlimited maximum length.
73              
74             This class is usually not invoked directly, at least not when used
75             with the Persistent framework of classes. However, the constructor
76             arguments of this class are usually of interest when defining the
77             attributes of a Persistent object since the I method of
78             the Persistent classes instantiates this class directly. Also, the
79             arguments to the I method are of interest when dealing with the
80             accessor methods of the Persistent classes since the accessor methods
81             pass their arguments to the I method and return the string
82             value from the I method.
83              
84             This class is part of the Persistent base package which is available
85             from:
86              
87             http://www.bigsnow.org/persistent
88             ftp://ftp.bigsnow.org/pub/persistent
89              
90             =head1 DESCRIPTION
91              
92             Before we get started describing the methods in detail, it should be
93             noted that all error handling in this class is done with exceptions.
94             So you should wrap an eval block around all of your code. Please see
95             the L documentation for more information on exception
96             handling in Perl.
97              
98             =head1 METHODS
99              
100             =cut
101              
102             ########################################################################
103             #
104             # --------------------------------------------------------------------
105             # PUBLIC ABSTRACT METHODS OVERRIDDEN (REDEFINED) FROM THE PARENT CLASS
106             # --------------------------------------------------------------------
107             #
108             ########################################################################
109              
110             ########################################################################
111             # initialize
112             ########################################################################
113              
114             =head2 Constructor -- Creates the VarChar Object
115              
116             eval {
117             my $string = new Persistent::DataType::VarChar($value,
118             $max_length);
119             };
120             croak "Exception caught: $@" if $@;
121              
122             Initializes a variable length character string object. This method
123             throws Perl execeptions so use it with an eval block.
124              
125             Parameters:
126              
127             =over 4
128              
129             =item I<$value>
130              
131             Actual value of the string. This argument is optional and may be set
132             to undef.
133              
134             =item I<$max_length>
135              
136             Maximum length of the string value. This argument is optional and
137             will be initialized to the length of the I<$value> as a default or 1
138             if no I<$value> argument is passed.
139              
140             =back
141              
142             =cut
143              
144             sub initialize {
145 44     44 0 205 my($this, $value, $max_length) = @_;
146              
147 44         107 $this->_trace();
148              
149 44 50       106 if (!defined($max_length)) {
150 0 0 0     0 if (!defined($value) || $value eq '') {
151 0         0 $max_length = 1;
152             } else {
153 0         0 $max_length = length($value);
154             }
155             }
156 44         89 $this->max_length($max_length);
157 44         105 $this->value($value);
158             }
159              
160             ########################################################################
161             #
162             # --------------
163             # PUBLIC METHODS
164             # --------------
165             #
166             ########################################################################
167              
168             ########################################################################
169             # max_length
170             ########################################################################
171              
172             =head2 max_length -- Accesses the Maximum Length of the String
173              
174             eval {
175             ### set the maximum length ###
176             $string->max_length($new_max);
177              
178             ### get the maximum length ###
179             $max_length = $string->max_length();
180             };
181             croak "Exception caught: $@" if $@;
182              
183             Sets the maximum length of the string and/or returns it. This method
184             throws Perl execeptions so use it with an eval block.
185              
186             Parameters:
187              
188             =over 4
189              
190             =item I<$max_length>
191              
192             Maximum length of the string value. The maximum length must be
193             greater than zero, otherwise, an exception is thrown.
194              
195             =back
196              
197             =cut
198              
199             sub max_length {
200 166 50 66 166 1 479 (@_ == 1 || @_ == 2) or croak 'Usage: $obj->max_length([$max_length])';
201 166         187 my $this = shift;
202 166 50       329 ref($this) or croak "$this is not an object";
203              
204 166         380 $this->_trace();
205              
206             ### check the arguments ###
207 166 100       321 if (@_) {
208 44         51 my($max_length) = @_;
209 44 50 33     298 if (!defined($max_length) || $max_length eq '' || $max_length <= 0) {
      33        
210 0 0       0 croak(sprintf("maximum length (%s) must be > 0",
211             defined $max_length ? $max_length : 'undef'));
212             }
213             }
214              
215             ### superclass does the work ###
216 166         543 $this->SUPER::max_length(@_);
217             }
218              
219             ### end of library ###
220             1;
221             __END__