docs
[pspp] / perl-module / lib / PSPP.pm.in
1 ## PSPP - a program for statistical analysis.
2 ## Copyright (C) 2019 Free Software Foundation, Inc.
3 ##
4 ## This program is free software: you can redistribute it and/or modify
5 ## it under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation, either version 3 of the License, or
7 ## (at your option) any later version.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ## GNU General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 use 5.008008;
18 use strict;
19 use warnings;
20
21 =head1 NAME
22
23 PSPP-Perl - Perl extension to PSPP
24
25 =head1 SYNOPSIS
26
27   use PSPP;
28
29 =head1 DESCRIPTION
30
31 PSPP-Perl provides an interface to the libraries used by pspp to read and
32 write system files.
33
34 =head1 EXPORT
35
36 None by default.
37
38 =cut
39 BEGIN {
40         $PSPP::VERSION='@VERSION_FOR_PERL@';
41         require XSLoader;
42         XSLoader::load('PSPP', $PSPP::VERSION);
43 }
44
45 PSPP::onBoot($PSPP::VERSION);
46
47 =pod
48
49 =head1 PROGRAMMER'S INTERFACE
50
51 The subroutines in this package return zero or unref on error.
52 When errors occur, a string describing the error is written
53 to C<$PSPP::errstr>.
54
55 =cut
56
57 package PSPP;
58 use POSIX ;
59
60 use constant { SYSMIS => -(POSIX::DBL_MAX),
61                PERL_EPOCH => 12219379200 # Number of seconds between
62                    # 14th October 1582
63                    # and
64                    # 1st January 1970
65                };
66
67
68
69 package PSPP::Dict;
70
71 =pod
72
73 =head2 PSPP::Dict::new
74
75 Creates a new dictionary.  This returned dictionary will be empty.
76 Returns undef on failure.
77
78 =head3 set_documents ($string)
79
80 Sets the documents (comments) to C<string>.
81
82 =head3 add_document ($string)
83
84 Appends C<string> to the documents.
85
86 =head3 clear_documents ()
87
88 Removes all documents.
89
90 =head3 set_weight ($var)
91
92 Sets the weighting variable to C<var>.
93
94 =cut
95
96 sub new
97 {
98     my $class = shift;
99     my $self = pxs_dict_new ();
100     bless ($self, $class);
101     return $self;
102 }
103
104 =pod
105
106 =head3 get_var_cnt ()
107
108 Returns the number of variables in the dictionary.
109
110 =head3 get_var ($idx)
111
112 Returns the C<idx>th variable from the dictionary.
113 Returns undef if C<idx> is greater than or equal to the number
114 of variables in the dictionary.
115
116 =cut
117
118 sub get_var
119 {
120     my $dict = shift;
121     my $idx = shift;
122     my $var = pxs_get_variable ($dict, $idx);
123
124     if ( ref $var )
125     {
126         bless ($var, "PSPP::Var");
127     }
128     return $var;
129 }
130
131 =pod
132
133 =head3 get_var_by_name ($name)
134
135 Returns the variable from the dictionary whose name is C<name>.
136 If there is no such variable, a null reference will be returned.
137
138 =cut
139
140 sub get_var_by_name
141 {
142     my $dict = shift;
143     my $name = shift;
144     my $var = pxs_get_var_by_name ($dict, $name);
145
146     if ( ref $var )
147     {
148         bless ($var, "PSPP::Var");
149     }
150     return $var;
151 }
152
153
154 package PSPP::Fmt;
155
156 =pod
157
158 =head2 PSPP::Fmt
159
160 Contains constants used to denote variable format types.
161 The identifiers are the same as  those used in pspp to denote formats.
162 For  example C<PSPP::Fmt::F> defines floating point format, and
163 C<PSPP::Fmt::A> denotes string format.
164
165 =cut
166
167 # These must correspond to the values in src/data/format.h
168 use constant {
169     F =>        0,
170     COMMA =>    1,
171     DOT =>      2,
172     DOLLAR =>   3,
173     PCT =>      4,
174     E =>        5,
175     CCA =>      6,
176     CCB =>      7,
177     CCC =>      8,
178     CCD =>      9,
179     CCE =>      10,
180     N =>        11,
181     Z =>        12,
182     P =>        13,
183     PK =>       14,
184     IB =>       15,
185     PIB =>      16,
186     PIBHEX =>   17,
187     RB =>       18,
188     RBHEX =>    19,
189     DATE =>     20,
190     ADATE =>    21,
191     EDATE =>    22,
192     JDATE =>    23,
193     SDATE =>    24,
194     QYR =>      25,
195     MOYR =>     26,
196     WKYR =>     27,
197     DATETIME => 28,
198     YMDHMS =>   29,
199     MTIME =>    30,
200     TIME =>     31,
201     DTIME =>    32,
202     WKDAY =>    33,
203     MONTH =>    34,
204     A =>        35,
205     AHEX =>     36
206 };
207
208
209 =head2 PSPP::Var
210
211 =cut
212
213 package PSPP::Var;
214
215 =head3 new ($dict, $name, %input_fmt)
216
217 Creates and returns a new variable in the dictionary C<dict>.  The
218 new variable will have the name C<name>.  C<name> must be a valid UTF8 string.
219 The input format is set by the C<input_fmt> parameter
220 (See L</PSPP::Fmt>).
221 By default, the write and print formats are the same as the input format.
222 The write and print formats may be changed (See L</set_write_format>),
223 L</set_print_format>).  The input format may not be changed after
224 the variable has been created.
225 If the variable cannot be created, undef is returned.
226
227 =cut
228
229 sub new
230 {
231     my $class = shift;
232     my $dict = shift;
233     my $name = shift;
234     my %format = @_;
235     my $self = pxs_dict_create_var ($dict, $name, \%format);
236     if ( ref $self )
237     {
238         bless ($self, $class);
239     }
240     return $self;
241 }
242
243 =pod
244
245 =head3 set_label ($label)
246
247 Sets the variable label to C<label>, which must be a valid UTF8 string.
248
249
250 =cut
251
252 =pod
253
254 =head3 set_write_format (%fmt)
255
256 Sets the write format to C<fmt>. <fmt> is a hash containing the keys:
257
258 =over 2
259
260 =item FMT
261
262 A constant denoting the format type.  See L</PSPP::Fmt>.
263
264 =item decimals
265
266 An integer denoting the number of decimal places for the format.
267
268 =item width
269
270 An integer denoting the width of the format.
271
272 =back
273
274 On error the subroutine returns zero.
275
276 =cut
277
278 sub set_write_format
279 {
280     my $var = shift;
281     my %format = @_;
282     pxs_set_write_format ($var, \%format);
283 }
284
285 =pod
286
287 =head3 set_print_format (%fmt)
288
289 Sets the print format to C<fmt>.
290 On error the subroutine returns zero.
291
292 =cut
293
294 sub set_print_format
295 {
296     my $var = shift;
297     my %format = @_;
298     pxs_set_print_format ($var, \%format);
299 }
300
301 =pod
302
303
304 =head3 get_write_format ()
305
306 Returns a reference to a hash containing the write format for the variable.
307
308
309 =head3 get_print_format ()
310
311 Returns a reference to a hash containing the print format for the variable.
312
313 =head3 set_output_format (%fmt)
314
315 Sets the write and print formats to C<fmt>.  This is the same as
316 calling set_write_format followed by set_print_format.
317 On error the subroutine returns zero.
318
319 =cut
320
321
322 sub set_output_format
323 {
324     my $var = shift;
325     my %format = @_;
326     pxs_set_output_format ($var, \%format);
327 }
328
329 =pod
330
331 =head3 clear_value_labels ()
332
333 Removes all value labels from the variable.
334
335 =cut
336
337
338 =pod
339
340 =head3 add_value_label ($key, $label)
341
342 Adds the value label C<label> to the variable for the value C<key>.
343 C<label> must be a valid UTF8 string.
344 On error the subroutine returns zero.
345
346 =head3 add_value_labels (@array)
347
348 =cut
349
350 sub add_value_labels
351 {
352     my $var = shift;
353     my %values = @_;
354     my @li;
355
356     my $n = 0;
357     while ( @li = each %values )
358     {
359         if ( $var->add_value_label ($li[0], "$li[1]") )
360         {
361             $n++;
362         }
363     }
364
365     return $n;
366 }
367
368 =pod
369
370 =head3 set_value_labels ($key, $label)
371
372 C<Set_value_labels> is identical to calling L</clear_value_labels>
373 followed by L</add_value_labels>.
374 On error the subroutine returns zero.
375
376 =cut
377
378 sub set_value_labels
379 {
380     my $self = shift;
381     my %labels = @_;
382     $self->clear_value_labels () ;
383     $self->add_value_labels (%labels);
384 }
385
386 =pod
387
388 =head3 set_missing_values ($val1 [, $val2[, $val3] ])
389
390 Sets the missing values for the variable.
391 No more than three missing values may be specified.
392
393 =head3 get_attributes()
394
395 Returns a reference to a hash of the custom variable attributes.
396 Each value of the hash is a reference to an array containing the
397 attribute values.
398
399 =head3 get_name ()
400
401 Returns the name of the variable.
402
403 =head3 get_label ()
404
405 Returns the label of the variable or undef if there is no label.
406
407 =head3 get_value_labels ()
408
409 Returns a reference to a hash containing the value labels for the variable.
410 The hash is keyed by data values which correpond to the labels.
411
412 =cut
413
414 package PSPP::Sysfile;
415
416 =pod
417
418 =head2 PSPP::Sysfile
419
420 =head3 new ($filename, $dict [,%opts])
421
422 Creates a new system file from the dictionary C<dict>.  The file will
423 be written to the file called C<filename>. The string C<filename> must
424 be encoded in UTF-8.
425 C<opt>, if specified, is a hash containing optional parameters for the
426 system file.  Currently, the only supported parameter is
427 C<compress>. If C<compress> is non zero, then the system file written
428 will be in the compressed format.
429 On error, undef is returned.
430
431
432 =head3 append_case (@case)
433
434 Appends a case to the system file.
435 C<Case> is an array of scalars, each of which are the values of
436 the variables in the dictionary corresponding to the system file.
437 If the case contains strings, then the strings must be UTF8 encoded.
438 The special value C<PSPP::SYSMIS> may be used to indicate that a value
439 is system missing.
440 If the array contains less elements than variables in the dictionary,
441 remaining values will be set to system missing.
442
443 =cut
444
445 sub new
446 {
447     my $class = shift;
448     my $filename = shift;
449     my $dict = shift;
450     my $opts = shift;
451
452     my $self  = pxs_create_sysfile ($filename, $dict, $opts);
453
454     if ( ref $self )
455     {
456         bless ($self, $class);
457     }
458     return $self;
459 }
460
461 =pod
462
463 =head3 close ()
464
465 Closes the system file.
466
467 This subroutine closes the system file and flushes it to disk.  No
468 further cases may be written once the file has been closed.
469 The system file will be automatically closed when it goes out of scope.
470
471 =cut
472
473 package PSPP::Reader;
474
475 =pod
476
477 =head2 PSPP::Reader
478
479 =cut
480
481 sub open
482 {
483     my $class = shift;
484     my $filename = shift;
485
486     my $self  = pxs_open_sysfile ($filename);
487
488     if ( ref $self )
489     {
490         bless ($self, $class);
491     }
492     return $self;
493 }
494
495 =pod
496
497 =head3 open ($filename)
498
499 Opens a system file for reading.
500
501 Open is used to read data from an existing system file.
502 It creates and returns a PSPP::Reader object which can be used to read
503 data and dictionary information from C<filename>.  The string C<filename>
504 must be in UTF-8 encoding.
505
506 =head3 get_case_cnt ()
507
508 Returns the number of cases in a open system file.  Some files
509 do not store the number of cases.  In these instances undef
510 will be returned.  Therefore, then programmer must check that the
511 returned value is not undef before using it.
512
513 =cut
514
515 sub get_dict
516 {
517     my $reader = shift;
518
519     my $dict = pxs_get_dict ($reader);
520
521     bless ($dict, "PSPP::Dict");
522
523     return $dict;
524 }
525
526 =pod
527
528 =head3 get_dict ()
529
530 Returns the dictionary associated with the reader.
531
532 =head3 get_next_case ()
533
534 Retrieves the next case from the reader.
535 This method returns an array of scalars, each of which are the values of
536 the data in the system file.
537 The first call to C<get_next_case> after C<open> has been called retrieves
538 the first case in the system file.  Each subsequent call retrieves the next
539 case.  If there are no more cases to be read, the function returns an empty
540 list.
541
542 If the case contains system missing values, these values are set to the
543 empty string.
544
545 =head2 Miscellaneous subroutines
546
547 The following subroutines provide (hopefully) useful information about the
548 values retrieved from a reader.
549
550 =head3 PSPP::format_value ($value, $variable)
551
552 Returns a scalar containing a string representing C<value> formatted according
553 to the print format of C<variable>.
554 In the most common usage,  C<value> should be a value of C<variable>.
555
556
557 =head3 PSPP::value_is_missing ($value, $variable)
558
559 Returns non-zero if C<value> is either system missing, or if it matches the
560 user missing criteria for C<variable>.
561
562 =cut
563
564 1;
565 __END__
566
567
568 =head1 AUTHOR
569
570 John Darrington, E<lt>john@darrington.wattle.id.auE<gt>
571
572 =head1 COPYRIGHT AND LICENSE
573
574 Copyright (C) 2007, 2008, 2009 by Free Software Foundation
575
576 This program is free software: you can redistribute it and/or modify
577 it under the terms of the GNU General Public License as published by
578 the Free Software Foundation, either version 3 of the License, or
579 (at your option) any later version.
580
581 This program is distributed in the hope that it will be useful,
582 but WITHOUT ANY WARRANTY; without even the implied warranty of
583 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
584 GNU General Public License for more details.
585
586 You should have received a copy of the GNU General Public License
587 along with this program.  If not, see <http://www.gnu.org/licenses/>.
588
589 =cut