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   686986 use utf8;
  39         245  
  39         264  
4 39     39   1278 use strict;
  39         98  
  39         768  
5 39     39   192 use warnings;
  39         79  
  39         14601  
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 972470 my $file = shift;
46 438 50       1670 die "Missing file" unless $file;
47 438 50       7246 die "File $file doesn't exist" unless -f $file;
48 438 50       17209 open (my $fh, '<:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
49 438         34897 local $/;
50 438         18954 my $content = <$fh>;
51 438 50       28883 close $fh or die "Couldn't close $file $!";
52 438         4520 return $content;
53             }
54              
55             sub write_file {
56 218     218 1 4378968 my $file = shift;
57 218 50   13   16475 open (my $fh, '>:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
  13         111  
  13         28  
  13         122  
58 218         66214 print $fh @_;
59 218 50       16901 close $fh or die "Couldn't close $file $!";
60 218         1860 return;
61             }
62              
63             sub append_file {
64 1     1 1 1059 my $file = shift;
65 1 50       43 open (my $fh, '>>:encoding(UTF-8)', $file) or die "Couldn't open $file $!";
66 1         77 print $fh @_;
67 1 50       38 close $fh or die "Couldn't close $file $!";
68 1         7 return;
69             }