File Coverage

lib/IOMux/File/Write.pm
Criterion Covered Total %
statement 43 46 93.4
branch 13 20 65.0
condition 4 8 50.0
subroutine 9 10 90.0
pod 2 3 66.6
total 71 87 81.6


line stmt bran cond sub pod time code
1             # Copyrights 2011 by Mark Overmeer.
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 1.07.
5 3     3   2714 use warnings;
  3         7  
  3         94  
6 3     3   16 use strict;
  3         6  
  3         120  
7              
8             package IOMux::File::Write;
9 3     3   14 use vars '$VERSION';
  3         6  
  3         149  
10             $VERSION = '0.12';
11              
12 3     3   18 use base 'IOMux::Handler::Write';
  3         5  
  3         948  
13              
14 3     3   20 use Log::Report 'iomux';
  3         5  
  3         18  
15 3     3   867 use Fcntl;
  3         5  
  3         1002  
16 3     3   19 use File::Basename 'basename';
  3         5  
  3         1332  
17              
18              
19             sub init($)
20 4     4 0 10 { my ($self, $args) = @_;
21              
22 4 50       16 my $file = $args->{file}
23             or error __x"no file to open specified in {pkg}", pkg => __PACKAGE__;
24              
25 4         16 my $flags = $args->{modeflags};
26 4   100     29 my $mode = $args->{mode} || '>';
27 4 50 33     28 unless(ref $file || defined $flags)
28 4 100       22 { if($mode eq '>>') { $args->{append} = 1 }
  1 50       3  
29 3 100       13 elsif($mode eq '>') { $mode = '>>' if $args->{append} }
30             else
31 0         0 { error __x"unknown file mode '{mode}' for {fn} in {pkg}"
32             , mode => $mode, fn => $file, pkg => __PACKAGE__;
33             }
34            
35 4         10 $flags = O_WRONLY|O_NONBLOCK;
36 4 50 33     17 $flags |= O_CREAT unless exists $args->{create} && !$args->{create};
37 4 100       12 $flags |= O_APPEND if $args->{append};
38 4 50       11 $flags |= O_EXCL if $args->{exclusive};
39             }
40              
41 4         6 my $fh;
42 4 50       11 if(ref $file)
43 0         0 { $fh = $file;
44             }
45             else
46 4 50       142868 { sysopen $fh, $file, $flags
47             or fault __x"cannot open file {fn} for {pkg}"
48             , fn => $file, pkg => __PACKAGE__;
49 4         55 $self->{IMFW_mode} = $flags;
50             }
51 4         257 $args->{name} = $mode.(basename $file);
52 4         16 $args->{fh} = $fh;
53              
54 4         38 $self->SUPER::init($args);
55 4         20 $self;
56             }
57              
58              
59             sub open($$@)
60 2     2 1 4 { my ($class, $mode, $file, %args) = @_;
61 2         15 $class->new(file => $file, mode => $mode, %args);
62             }
63              
64              
65             #-------------------
66              
67 0     0 1   sub mode() {shift->{IMFW_mode}}
68              
69             1;