JPRED-2 Move Jpred 3.0.1 to public Git
[jpred.git] / jpred / lib / Write.pm
1 package Write;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 use base qw(Root);
8
9 use IO::String;
10 use File::Temp;
11
12 =head1 NAME
13
14 Write - Two base methods for writing information.
15
16 =head1 DESCRIPTION
17
18 This module contains three methods for writing information into a program. They allow the writing of information from a string or filename and expect the method write() to be defind by the class that inherits this module.
19
20 =head1 METHODS
21
22 =head2 write_file($path)
23
24 Opens a file at the given path and then calls the write method.
25
26 =head2 write_gzip_file($path)
27
28 Like write_file, but compresses the output using the system gzip command.
29
30 =head2 write_string($scalar)
31
32 Writes the data in the scalar and passes it to the write method.
33
34 =head2 write($filehandle)
35
36 This method will cause a fatal error unless it's overidden by the class that inherits this module.
37
38 =cut
39
40 sub write {
41         confess "The inheriting package hasn't defined the write() method\n"
42 }
43
44 sub write_file {
45         my ($self, $fn) = @_;
46
47         open my $fh, ">$fn" or confess "Can't open file $fn: $!";
48         $self->write($fh);
49         close $fh;
50 }
51
52 sub write_gzip_file {
53                 my ($self, $fn) = @_;
54
55                 my $gzipd_fn = File::Temp->new->filename;
56
57                 $self->write_file($gzipd_fn);
58
59                 system "gzip -c $gzipd_fn > $fn";
60 }
61
62 sub write_string {
63         my ($self) = @_;
64
65         my $string;
66         $self->write(IO::String->new($string));
67         return $string;
68 }
69
70 1;