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   582831 use utf8;
  39         206  
  39         225  
4 39     39   1113 use strict;
  39         84  
  39         750  
5 39     39   187 use warnings;
  39         70  
  39         13087  
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 866337 my $file = shift;
46 438 50       1586 die "Missing file" unless $file;
47 438 50       6657 die "File $file doesn't exist" unless -f $file;
48 438 50       16025 open (my $fh, '<:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
49 438         30919 local $/;
50 438         17451 my $content = <$fh>;
51 438 50       25090 close $fh or die "Couldn't close $file $!";
52 438         4244 return $content;
53             }
54              
55             sub write_file {
56 218     218 1 4323024 my $file = shift;
57 218 50   13   44273 open (my $fh, '>:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
  13         116  
  13         30  
  13         94  
58 218         60563 print $fh @_;
59 218 50       12160 close $fh or die "Couldn't close $file $!";
60 218         1674 return;
61             }
62              
63             sub append_file {
64 1     1 1 869 my $file = shift;
65 1 50       35 open (my $fh, '>>:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
66 1         60 print $fh @_;
67 1 50       30 close $fh or die "Couldn't close $file $!";
68 1         5 return;
69             }