File Coverage

blib/lib/Text/Amuse/Compile/Utils.pm
Criterion Covered Total %
statement 30 30 100.0
branch 8 16 50.0
condition n/a
subroutine 7 7 100.0
pod 3 3 100.0
total 48 56 85.7


line stmt bran cond sub pod time code
1             package Text::Amuse::Compile::Utils;
2              
3 39     39   1178255 use utf8;
  39         1463  
  39         347  
4 39     39   1910 use strict;
  39         88  
  39         1067  
5 39     39   245 use warnings;
  39         80  
  39         19197  
6              
7             =head1 NAME
8              
9             Text::Amuse::Compile::Utils - Common routines used
10              
11             =head1 FILE READING/WRITING/APPENDING
12              
13             These functions are replacements for L, which has deemed
14             deprecated. Candidate modules are L and
15             L. But given that we always use utf8, and don't need all
16             the L features (which are cool, but would make sense to
17             use them everywhere instead of L etc, let's cook our own
18             in the meanwhile.
19              
20             The following functions always use the binmode C on
21             the files, so they takes and return decoded strings.
22              
23             The purpose of this module is just to save some typing.
24              
25             =head2 read_file($file)
26              
27             =head2 write_file($file, @strings)
28              
29             =head2 append_file($file, @strings)
30              
31             =head1 EXPORTS
32              
33             None by default, only on demand.
34              
35             =cut
36              
37             our @ISA = qw(Exporter);
38             our @EXPORT_OK = qw/append_file
39             write_file
40             read_file
41             /;
42              
43              
44             sub read_file {
45 438     438 1 1377964 my $file = shift;
46 438 50       2005 die "Missing file" unless $file;
47 438 50       10027 die "File $file doesn't exist" unless -f $file;
48 438 50       19046 open (my $fh, '<:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
49 438         39817 local $/;
50 438         33373 my $content = <$fh>;
51 438 50       24835 close $fh or die "Couldn't close $file $!";
52 438         5176 return $content;
53             }
54              
55             sub write_file {
56 218     218 1 7908356 my $file = shift;
57 218 50   13   70558 open (my $fh, '>:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
  13         10941  
  13         257  
  13         92  
58 218         55330 print $fh @_;
59 218 50       18757 close $fh or die "Couldn't close $file $!";
60 218         1909 return;
61             }
62              
63             sub append_file {
64 1     1 1 1816 my $file = shift;
65 1 50       57 open (my $fh, '>>:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
66 1         103 print $fh @_;
67 1 50       82 close $fh or die "Couldn't close $file $!";
68 1         8 return;
69             }