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   580733 use utf8;
  39         248  
  39         228  
4 39     39   1129 use strict;
  39         106  
  39         679  
5 39     39   167 use warnings;
  39         65  
  39         12623  
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 851791 my $file = shift;
46 438 50       1578 die "Missing file" unless $file;
47 438 50       6285 die "File $file doesn't exist" unless -f $file;
48 438 50       15900 open (my $fh, '<:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
49 438         30079 local $/;
50 438         17014 my $content = <$fh>;
51 438 50       23532 close $fh or die "Couldn't close $file $!";
52 438         3902 return $content;
53             }
54              
55             sub write_file {
56 218     218 1 4327763 my $file = shift;
57 218 50   13   19174 open (my $fh, '>:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
  13         102  
  13         26  
  13         96  
58 218         60878 print $fh @_;
59 218 50       69187 close $fh or die "Couldn't close $file $!";
60 218         1581 return;
61             }
62              
63             sub append_file {
64 1     1 1 962 my $file = shift;
65 1 50       44 open (my $fh, '>>:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
66 1         71 print $fh @_;
67 1 50       41 close $fh or die "Couldn't close $file $!";
68 1         25 return;
69             }