File Coverage

blib/lib/Data/Sofu/Value.pm
Criterion Covered Total %
statement 30 48 62.5
branch 4 8 50.0
condition n/a
subroutine 9 17 52.9
pod 15 15 100.0
total 58 88 65.9


line stmt bran cond sub pod time code
1             ###############################################################################
2             #Value.pm
3             #Last Change: 2006-11-01
4             #Copyright (c) 2006 Marc-Seabstian "Maluku" Lucksch
5             #Version 0.28
6             ####################
7             #This file is part of the sofu.pm project, a parser library for an all-purpose
8             #ASCII file format. More information can be found on the project web site
9             #at http://sofu.sourceforge.net/ .
10             #
11             #sofu.pm is published under the terms of the MIT license, which basically means
12             #"Do with it whatever you want". For more information, see the license.txt
13             #file that should be enclosed with libsofu distributions. A copy of the license
14             #is (at the time of this writing) also available at
15             #http://www.opensource.org/licenses/mit-license.php .
16             ###############################################################################
17              
18              
19             =head1 NAME
20              
21             Data::Sofu::Value - A Sofu Value
22              
23             =head1 DESCRIPTION
24              
25             Provides a interface similar to the original SofuD (sofu.sf.net)
26              
27             =head1 Synopsis
28              
29             require Data::Sofu::Value;
30             my $v = Data::Sofu::Value->new();
31             $v->set("Hello World");
32              
33             =head1 SYNTAX
34              
35             This Module is pure OO, exports nothing
36              
37             =cut
38              
39             package Data::Sofu::Value;
40              
41 3     3   16 use strict;
  3         6  
  3         108  
42 3     3   16 use warnings;
  3         6  
  3         1912  
43             require Data::Sofu::Object;
44             require Data::Sofu::List;
45             require Data::Sofu::Undefined;
46             require Data::Sofu;
47             our @ISA = qw/Data::Sofu::Object/;
48             our $VERSION="0.29";
49              
50             =head1 METHODS
51              
52             Also look at C for methods, cause Value inherits from it
53              
54             =head2 new([DATA])
55              
56             Creates a new C and returns it
57              
58             Converts DATA to a string if DATA is given.
59              
60             $val = Data::Sofu::Value->new("Hello World");
61              
62             =cut
63              
64              
65             sub new {
66 672     672 1 1362 my $self={};
67 672         3045 bless $self,shift;
68 672         4940 $$self{Value}=undef;
69 672 50       2525 $$self{Value}="".shift if @_;
70 672 50       14627 if (@_) {
71 0         0 return Data::Sofu::List->new($$self{Value},@_);
72             }
73 672 50       3795 return $self if defined $$self{Value};
74 0         0 return Data::Sofu::Undefined->new();
75             }
76              
77             =head2 set(DATA)
78              
79             Sets the contents of this Value (replaces the old contents).
80              
81             Note: DATA will be converted to a string.
82              
83             $v->set("Foobar");
84              
85             =cut
86              
87             sub set {
88 576     576 1 28898 my $self=shift;
89 576         2351 $$self{Value}="".shift;
90             }
91              
92             =head2 asValue()
93              
94             Returns itself, used to make sure this Value is really a Value (Data::Sofu::Map and Data::Sofu::List will die if called with this method)
95              
96             =cut
97              
98             sub asValue {
99 66     66 1 418 return shift;
100             }
101              
102             =head2 asScalar()
103              
104             Perl only
105              
106             Returns this Value as a perl Scalar (same as toString)
107              
108             =cut
109              
110             sub asScalar {
111 0     0 1 0 my $self=shift;
112 0         0 return $$self{Value};
113             }
114              
115             =head2 toString()
116              
117             Returns this as a string
118              
119             =cut
120              
121             sub toString {
122 598     598 1 756 my $self=shift;
123 598         8555 return $$self{Value};
124             }
125              
126             =head2 C, C, toUTF32()
127              
128             Not working in Perl (cause there is no wchar, char, dchar stuff going on, if you need to convert strings use "Encode")
129              
130             They just return the same as toString()
131              
132             =cut
133              
134             #TODO
135             sub toUTF16 {
136 0     0 1 0 my $self=shift;
137 0         0 return $$self{Value};
138             }
139             sub toUTF8 {
140 0     0 1 0 my $self=shift;
141 0         0 return $$self{Value};
142             }
143             sub toUTF32 {
144 0     0 1 0 my $self=shift;
145 0         0 return $$self{Value};
146             }
147             #/TODO
148              
149             =head2 toInt()
150              
151             Return the Value as an Integer
152              
153             $v->toInt() === int $v->toString();
154              
155             =cut
156              
157             sub toInt {
158 0     0 1 0 my $self=shift;
159 0         0 return int $$self{Value};
160             }
161              
162             =head2 toFloat()
163              
164             Return the Value as a Float
165              
166             $v->toFloat() === $v->toString()+0;
167              
168             =cut
169              
170             sub toFloat {
171 0     0 1 0 my $self=shift;
172 0         0 return $$self{Value}+0;
173             }
174              
175             =head2 toLong()
176              
177             Return the Value as a Long
178              
179             $v->toLong() === int $v->toString();
180              
181             =cut
182              
183             sub toLong {
184 0     0 1 0 my $self=shift;
185 0         0 return int $$self{Value};
186             }
187              
188             =head2 toDouble()
189              
190             Return the Value as a Double
191              
192             $v->toDouble() === $v->toString()+0;
193              
194             =cut
195              
196             sub toDouble {
197 0     0 1 0 my $self=shift;
198 0         0 return $$self{Value}+0;
199             }
200              
201             =head2 isValue()
202              
203             Returns 1
204              
205             =cut
206              
207             sub isValue {
208 564     564 1 2126 return 1
209             }
210              
211              
212             #=head2 string()
213              
214             #Same as Data::Sofu::Object::string(), but skips the Reference building.
215              
216             #=cut
217              
218             #sub string { #No References for Values at this time (just remove this function to enable them
219             # my $self=shift;
220             # return $self->stringify(@_);
221             #}
222              
223             =head2 C
224              
225             Returns a string representation of this Value.
226              
227             LEVEL and TREE are ignored...
228              
229             =cut
230              
231             sub stringify {
232 57     57 1 84 my $self=shift;
233 57         65 my $level=shift;
234 57         65 my $tree=shift;
235 57 50       107 return "Value = ".Data::Sofu::Sofuescape($$self{Value}).$self->stringComment()."\n" unless $level;
236 57         168 return Data::Sofu::Sofuescape($$self{Value}).$self->stringComment()."\n";
237             }
238              
239             =head2 C
240              
241             Returns the binary version of this Value using the BINARY DRIVER. Don't call this one, use binaryPack instead.
242              
243             =cut
244              
245             sub binarify {
246 108     108 1 1272 my $self=shift;
247 108         195 my $tree=shift;
248 108         113 my $bin=shift;
249 108         319 my $str=$bin->packType(1);
250 108         338 $str.=$self->packComment($bin);
251 108         2925 $str.=$bin->packText($$self{Value});
252 108         1112 return $str;
253             }
254              
255             =head1 BUGS
256              
257             most of the methods do the same, because perl does the converting for you.
258              
259             =head1 SEE ALSO
260              
261             L, L, L, L, L, L, L
262              
263             =cut
264              
265             1;