| 1 | #============================================================================== | ||||||||||||||||||||
| 2 | # LibWeb::File -- File manipulations for libweb applications. | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | package LibWeb::File; | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | # Copyright (C) 2000 Colin Kong | ||||||||||||||||||||
| 7 | # | ||||||||||||||||||||
| 8 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||
| 9 | # modify it under the terms of the GNU General Public License | ||||||||||||||||||||
| 10 | # as published by the Free Software Foundation; either version 2 | ||||||||||||||||||||
| 11 | # of the License, or (at your option) any later version. | ||||||||||||||||||||
| 12 | # | ||||||||||||||||||||
| 13 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||
| 16 | # GNU General Public License for more details. | ||||||||||||||||||||
| 17 | # | ||||||||||||||||||||
| 18 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||
| 19 | # along with this program; if not, write to the Free Software | ||||||||||||||||||||
| 20 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||||||||||||||||||||
| 21 | #============================================================================= | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | # $Id: File.pm,v 1.4 2000/07/18 06:33:30 ckyc Exp $ | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | #-############################## | ||||||||||||||||||||
| 26 | # Use standard library. | ||||||||||||||||||||
| 27 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 1 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 1 | require FileHandle; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | #-############################## | ||||||||||||||||||||
| 33 | # Use custom library. | ||||||||||||||||||||
| 34 | 1 | require LibWeb::Class; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | #-############################## | ||||||||||||||||||||
| 37 | # Version. | ||||||||||||||||||||
| 38 | 1 | $VERSION = '0.02'; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | #-############################## | ||||||||||||||||||||
| 41 | # Inheritance. | ||||||||||||||||||||
| 42 | 1 | @ISA = qw(LibWeb::Class); | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | #-############################## | ||||||||||||||||||||
| 45 | # Methods. | ||||||||||||||||||||
| 46 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 47 | 1 | my($class, $Class, $self); | |||||||||||||||||||
| 48 | 1 | $class = shift; | |||||||||||||||||||
| 49 | 1 | ▶ | $Class = ref($class) || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 50 | 1 | $self = $Class->SUPER::new(); | |||||||||||||||||||
| 51 | 1 | bless( $self, $Class ); | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | ▶ | sub DESTROY {} | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 0 | ▶ | sub read_lines_from_file { | ||||||||||||||||||
|
Subroutine
read_lines_from_file: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 57 | # | ||||||||||||||||||||
| 58 | # Params: -file => $file | ||||||||||||||||||||
| 59 | # | ||||||||||||||||||||
| 60 | # Open, read all lines, close the file and return lines in an ARRAY ref. | ||||||||||||||||||||
| 61 | # | ||||||||||||||||||||
| 62 | 0 | my ($self, $file, $fh, @lines); | |||||||||||||||||||
| 63 | 0 | $self = shift; | |||||||||||||||||||
| 64 | 0 | ($file) = $self->rearrange(['FILE'], @_); | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | ▶ | $fh = FileHandle->new($file) or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | croak "LibWeb::File::read_lines_from_file() error (the file is $file): $!"; | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | @lines = $fh->getlines() or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | croak "LibWeb::File::read_lines_from_file() error (the file is $file): $!"; | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | $fh->close(); | |||||||||||||||||||
| 73 | 0 | return \@lines; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | sub write_lines_to_file { | ||||||||||||||||||
|
Subroutine
write_lines_to_file: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 77 | # | ||||||||||||||||||||
| 78 | # Params: -file => 'abs_path_to_file', -lines => $lines | ||||||||||||||||||||
| 79 | # | ||||||||||||||||||||
| 80 | # Pre: | ||||||||||||||||||||
| 81 | # - $lines is an ARRAY ref. to lines which are scalars. | ||||||||||||||||||||
| 82 | # | ||||||||||||||||||||
| 83 | # Post: | ||||||||||||||||||||
| 84 | # - Overwrite the file with the lines. | ||||||||||||||||||||
| 85 | # | ||||||||||||||||||||
| 86 | 0 | my ($self, $lines, $old_file, $new_file, $NEW); | |||||||||||||||||||
| 87 | 0 | $self = shift; | |||||||||||||||||||
| 88 | 0 | ($old_file, $lines) = $self->rearrange(['FILE', 'LINES'], @_); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | $new_file = "${old_file}." . time(); | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | $NEW = new FileHandle($new_file, 'w') || | ||||||||||||||||||
|
Condition0%0/2
'FileHandle'->new($new_file, 'w') || croak('LibWeb::File::write_lines_to_file(): cannot open new file')
MC/DC0%0/1
'FileHandle'->new($new_file, 'w') || croak('LibWeb::File::write_lines_to_file(): cannot open new file')
'FileHandle'->new($new_file, 'w')
Truth table0%0/2
A
A= 'FileHandle'->new($new_file, 'w')
| |||||||||||||||||||||
| 93 | croak "LibWeb::File::write_lines_to_file(): cannot open new file"; | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | foreach (@$lines) { | |||||||||||||||||||
| 96 | 0 | print $NEW $_; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | 0 | $NEW->close(); | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | ▶ | rename($new_file, $old_file) || | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | croak "LibWeb::File::write_lines_to_file(): could not rename files."; | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | chmod(0644, $old_file); | |||||||||||||||||||
| 104 | 0 | return 1; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 1 | 1; | |||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||