| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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
|
|
|
1
|
|
1429
|
use Carp; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
79
|
|
|
28
|
1
|
|
|
1
|
|
6
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
39
|
|
|
29
|
1
|
|
|
1
|
|
7
|
use vars qw($VERSION @ISA); |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
506
|
|
|
30
|
|
|
|
|
|
|
require FileHandle; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
#-############################## |
|
33
|
|
|
|
|
|
|
# Use custom library. |
|
34
|
|
|
|
|
|
|
require LibWeb::Class; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#-############################## |
|
37
|
|
|
|
|
|
|
# Version. |
|
38
|
|
|
|
|
|
|
$VERSION = '0.02'; |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
#-############################## |
|
41
|
|
|
|
|
|
|
# Inheritance. |
|
42
|
|
|
|
|
|
|
@ISA = qw(LibWeb::Class); |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
#-############################## |
|
45
|
|
|
|
|
|
|
# Methods. |
|
46
|
|
|
|
|
|
|
sub new { |
|
47
|
1
|
|
|
1
|
0
|
395
|
my($class, $Class, $self); |
|
48
|
1
|
|
|
|
|
3
|
$class = shift; |
|
49
|
1
|
|
33
|
|
|
10
|
$Class = ref($class) || $class; |
|
50
|
1
|
|
|
|
|
10
|
$self = $Class->SUPER::new(); |
|
51
|
1
|
|
|
|
|
4
|
bless( $self, $Class ); |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
0
|
|
|
0
|
|
|
sub DESTROY {} |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub read_lines_from_file { |
|
57
|
|
|
|
|
|
|
# |
|
58
|
|
|
|
|
|
|
# Params: -file => $file |
|
59
|
|
|
|
|
|
|
# |
|
60
|
|
|
|
|
|
|
# Open, read all lines, close the file and return lines in an ARRAY ref. |
|
61
|
|
|
|
|
|
|
# |
|
62
|
0
|
|
|
0
|
0
|
|
my ($self, $file, $fh, @lines); |
|
63
|
0
|
|
|
|
|
|
$self = shift; |
|
64
|
0
|
|
|
|
|
|
($file) = $self->rearrange(['FILE'], @_); |
|
65
|
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
|
$fh = FileHandle->new($file) or |
|
67
|
|
|
|
|
|
|
croak "LibWeb::File::read_lines_from_file() error (the file is $file): $!"; |
|
68
|
|
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
|
@lines = $fh->getlines() or |
|
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
|
|
|
|
|
|
|
sub write_lines_to_file { |
|
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
|
|
|
0
|
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
|
|
0
|
|
|
|
$NEW = new FileHandle($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
|
0
|
|
|
|
|
rename($new_file, $old_file) || |
|
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; |
|
108
|
|
|
|
|
|
|
__END__ |