-
Notifications
You must be signed in to change notification settings - Fork 0
/
filereader.pm
78 lines (73 loc) · 1.74 KB
/
filereader.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use strict;
use warnings;
package perluim::filereader;
sub new {
my ($class,$fileName) = @_;
my $this = {
fileName => $fileName || undef,
content => undef
};
return bless($this,ref($class) || $class);
}
sub load {
my ($self,$fileName) = @_;
if(open(my $list, '<:encoding(UTF-8)',$self->{fileName} || $fileName)) {
my @Rows = ();
while(my $row = <$list>) {
$row =~ s/^\s+|\s+$//g;
push(@Rows,$row);
}
$self->{content} = @Rows;
return 1,@Rows;
}
else {
return 0,undef;
}
}
sub loadString {
my ($self,$fileName) = @_;
if(open(my $list, '<:encoding(UTF-8)',$self->{fileName} || $fileName)) {
my $Str = "";
while(my $row = <$list>) {
$row =~ s/^\s+|\s+$//g;
$Str .= $row;
}
return 1,$Str;
}
else {
return 0,undef;
}
}
sub save {
my ($self,$output,$Array,$ObjectKey) = @_;
if(defined($Array)) {
my $file_handler;
unless(open($file_handler,">", "$output")) {
warn "Unabled to open rejected_files \n";
return;
}
if(defined $ObjectKey) {
foreach(@{ $Array }) {
print $file_handler "$_->{$ObjectKey}\n";
}
}
else {
foreach(@{ $Array }) {
print $file_handler "$_\n";
}
}
close $file_handler;
}
else {
my $file_handler;
unless(open($file_handler,">", "$output")) {
warn "Unabled to open rejected_files \n";
return;
}
foreach(@{ $self->{content} }) {
print $file_handler "$_\n";
}
close $file_handler;
}
}
1;