7 PSPP-Perl - Perl extension to PSPP
15 PSPP-Perl provides an interface to the libraries used by pspp to read and
24 $PSPP::VERSION='@VERSION_FOR_PERL@';
26 XSLoader::load('PSPP', $PSPP::VERSION);
29 PSPP::onBoot($PSPP::VERSION);
33 =head1 PROGRAMMER'S INTERFACE
35 The subroutines in this package return zero or unref on error.
36 When errors occur, a string describing the error is written
44 use constant { SYSMIS => -(POSIX::DBL_MAX),
45 PERL_EPOCH => 12219379200 # Number of seconds between
57 =head2 PSPP::Dict::new
59 Creates a new dictionary. This returned dictionary will be empty.
60 Returns undef on failure.
62 =head3 set_documents ($string)
64 Sets the documents (comments) to C<string>.
66 =head3 add_document ($string)
68 Appends C<string> to the documents.
70 =head3 clear_documents ()
72 Removes all documents.
74 =head3 set_weight ($var)
76 Sets the weighting variable to C<var>.
83 my $self = pxs_dict_new ();
84 bless ($self, $class);
92 Returns the number of variables in the dictionary.
96 Returns the C<idx>th variable from the dictionary.
97 Returns undef if C<idx> is greater than or equal to the number
98 of variables in the dictionary.
106 my $var = pxs_get_variable ($dict, $idx);
110 bless ($var, "PSPP::Var");
117 =head3 get_var_by_name ($name)
119 Returns the variable from the dictionary whose name is C<name>.
120 If there is no such variable, a null reference will be returned.
128 my $var = pxs_get_var_by_name ($dict, $name);
132 bless ($var, "PSPP::Var");
144 Contains constants used to denote variable format types.
145 The identifiers are the same as those used in pspp to denote formats.
146 For example C<PSPP::Fmt::F> defines floating point format, and
147 C<PSPP::Fmt::A> denotes string format.
151 # These must correspond to the values in src/data/format.h
199 =head3 new ($dict, $name, %input_fmt)
201 Creates and returns a new variable in the dictionary C<dict>. The
202 new variable will have the name C<name>. C<name> must be a valid UTF8 string.
203 The input format is set by the C<input_fmt> parameter
205 By default, the write and print formats are the same as the input format.
206 The write and print formats may be changed (See L</set_write_format>),
207 L</set_print_format>). The input format may not be changed after
208 the variable has been created.
209 If the variable cannot be created, undef is returned.
219 my $self = pxs_dict_create_var ($dict, $name, \%format);
222 bless ($self, $class);
229 =head3 set_label ($label)
231 Sets the variable label to C<label>, which must be a valid UTF8 string.
238 =head3 set_write_format (%fmt)
240 Sets the write format to C<fmt>. <fmt> is a hash containing the keys:
246 A constant denoting the format type. See L</PSPP::Fmt>.
250 An integer denoting the number of decimal places for the format.
254 An integer denoting the width of the format.
258 On error the subroutine returns zero.
266 pxs_set_write_format ($var, \%format);
271 =head3 set_print_format (%fmt)
273 Sets the print format to C<fmt>.
274 On error the subroutine returns zero.
282 pxs_set_print_format ($var, \%format);
288 =head3 get_write_format ()
290 Returns a reference to a hash containing the write format for the variable.
293 =head3 get_print_format ()
295 Returns a reference to a hash containing the print format for the variable.
297 =head3 set_output_format (%fmt)
299 Sets the write and print formats to C<fmt>. This is the same as
300 calling set_write_format followed by set_print_format.
301 On error the subroutine returns zero.
306 sub set_output_format
310 pxs_set_output_format ($var, \%format);
315 =head3 clear_value_labels ()
317 Removes all value labels from the variable.
324 =head3 add_value_label ($key, $label)
326 Adds the value label C<label> to the variable for the value C<key>.
327 C<label> must be a valid UTF8 string.
328 On error the subroutine returns zero.
330 =head3 add_value_labels (@array)
341 while ( @li = each %values )
343 if ( $var->add_value_label ($li[0], "$li[1]") )
354 =head3 set_value_labels ($key, $label)
356 C<Set_value_labels> is identical to calling L</clear_value_labels>
357 followed by L</add_value_labels>.
358 On error the subroutine returns zero.
366 $self->clear_value_labels () ;
367 $self->add_value_labels (%labels);
372 =head3 set_missing_values ($val1 [, $val2[, $val3] ])
374 Sets the missing values for the variable.
375 No more than three missing values may be specified.
377 =head3 get_attributes()
379 Returns a reference to a hash of the custom variable attributes.
380 Each value of the hash is a reference to an array containing the
385 Returns the name of the variable.
389 Returns the label of the variable or undef if there is no label.
391 =head3 get_value_labels ()
393 Returns a reference to a hash containing the value labels for the variable.
394 The hash is keyed by data values which correpond to the labels.
398 package PSPP::Sysfile;
404 =head3 new ($filename, $dict [,%opts])
406 Creates a new system file from the dictionary C<dict>. The file will
407 be written to the file called C<filename>. The string C<filename> must
409 C<opt>, if specified, is a hash containing optional parameters for the
410 system file. Currently, the only supported parameter is
411 C<compress>. If C<compress> is non zero, then the system file written
412 will be in the compressed format.
413 On error, undef is returned.
416 =head3 append_case (@case)
418 Appends a case to the system file.
419 C<Case> is an array of scalars, each of which are the values of
420 the variables in the dictionary corresponding to the system file.
421 If the case contains strings, then the strings must be UTF8 encoded.
422 The special value C<PSPP::SYSMIS> may be used to indicate that a value
424 If the array contains less elements than variables in the dictionary,
425 remaining values will be set to system missing.
432 my $filename = shift;
436 my $self = pxs_create_sysfile ($filename, $dict, $opts);
440 bless ($self, $class);
449 Closes the system file.
451 This subroutine closes the system file and flushes it to disk. No
452 further cases may be written once the file has been closed.
453 The system file will be automatically closed when it goes out of scope.
457 package PSPP::Reader;
468 my $filename = shift;
470 my $self = pxs_open_sysfile ($filename);
474 bless ($self, $class);
481 =head3 open ($filename)
483 Opens a system file for reading.
485 Open is used to read data from an existing system file.
486 It creates and returns a PSPP::Reader object which can be used to read
487 data and dictionary information from C<filename>. The string C<filename>
488 must be in UTF-8 encoding.
490 =head3 get_case_cnt ()
492 Returns the number of cases in a open system file. Some files
493 do not store the number of cases. In these instances undef
494 will be returned. Therefore, then programmer must check that the
495 returned value is not undef before using it.
503 my $dict = pxs_get_dict ($reader);
505 bless ($dict, "PSPP::Dict");
514 Returns the dictionary associated with the reader.
516 =head3 get_next_case ()
518 Retrieves the next case from the reader.
519 This method returns an array of scalars, each of which are the values of
520 the data in the system file.
521 The first call to C<get_next_case> after C<open> has been called retrieves
522 the first case in the system file. Each subsequent call retrieves the next
523 case. If there are no more cases to be read, the function returns an empty
526 If the case contains system missing values, these values are set to the
529 =head2 Miscellaneous subroutines
531 The following subroutines provide (hopefully) useful information about the
532 values retrieved from a reader.
534 =head3 PSPP::format_value ($value, $variable)
536 Returns a scalar containing a string representing C<value> formatted according
537 to the print format of C<variable>.
538 In the most common usage, C<value> should be a value of C<variable>.
541 =head3 PSPP::value_is_missing ($value, $variable)
543 Returns non-zero if C<value> is either system missing, or if it matches the
544 user missing criteria for C<variable>.
554 John Darrington, E<lt>john@darrington.wattle.id.auE<gt>
556 =head1 COPYRIGHT AND LICENSE
558 Copyright (C) 2007, 2008, 2009 by Free Software Foundation
560 This program is free software: you can redistribute it and/or modify
561 it under the terms of the GNU General Public License as published by
562 the Free Software Foundation, either version 3 of the License, or
563 (at your option) any later version.
565 This program is distributed in the hope that it will be useful,
566 but WITHOUT ANY WARRANTY; without even the implied warranty of
567 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
568 GNU General Public License for more details.
570 You should have received a copy of the GNU General Public License
571 along with this program. If not, see <http://www.gnu.org/licenses/>.