-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpUtils.pm
More file actions
95 lines (80 loc) · 1.72 KB
/
Copy pathpUtils.pm
File metadata and controls
95 lines (80 loc) · 1.72 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package pUtils;
use File::Find;
# replace string (case sensitive)
# args:
# $_[1] - string to be replaced
# $_[2] - string to be replaced by
# $_[3] - input string
#
# returns:
# $string - string after replacements
sub replace {
my ($from, $to, $string) = @_;
$string =~s/$from/$to/g;
return $string;
}#replace
# generate a file
# args:
# $_[1] - file name (including its extension)
# $_[2] - data to be written
sub genFile {
my ($name, $data) = @_;
open(f, '>', $name) or die $!;
print f $data;
close(f);
}#genFile
# read file
# args:
# $_[1] - file name (including its path)
#
# returns:
# $data - string containing read file data
sub readFile {
my ($name) = @_;
my $data = "";
open(f, '<', $name) or die $!;
while (<f>) {
$data = "$data$_";
}
close(f);
return $data;
}
# find file
# args:
# $_[1] - file name (including its extension)
# $_[2] - directory to search
#
# returns:
# @path - list of paths of found files (null if not found)
sub findFile {
my ($file, $directory) = @_;
my @path;
find (
sub {
if (index($File::Find::name, $file) != -1) {
push @path, $File::Find::name;
}
},
$directory
);
return @path;
}
# split string into a list line by line (ignoring comments and whitespace lines)
# args:
# $_[1] - string to be converted into a list
#
# returns:
# @list - list created from the input string
sub getList {
my ($data) = @_;
my @list;
while ($data =~ /([^\n]+)\n?/g) {
if ($1 !~ /^\s*$/) {
if ((index($1, "//") == -1) and (index($1, "#") == -1)) {
push (@list, $1);
}
}
}
return @list;
}
1;