-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUseful.pm
116 lines (85 loc) · 2.34 KB
/
Useful.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package Useful;
require Exporter;
# Copyright: Judita Preiss
# Date of creation: 3 August 2011
# This module contains functions used frequently throughout the
# Accurat project.
our @ISA = qw(Exporter);
our @EXPORT = qw(get_url remove_multiple_spaces execute_command is_member list_directory_contents);
our $VERSION = 1.0;
sub get_url {
my ($response, $ua, $url);
(scalar(@_) == 1) or die "get_url: url\n";
$url = $_[0];
$ua = LWP::UserAgent->new(env_proxy => 1,
keep_alive => 1,
timeout => 100,
max_size => 500000,
);
$response = $ua->get($url);
if((!($response->header(Client_Aborted))) && ($response->is_success)){
$html = ${$response}{"_content"};
}
else{
warn "WARNING: Couldn't get $url: " . $response->status_line . "\n";
undef $html;
}
return $html;
}
sub remove_multiple_spaces {
my ($line);
(scalar(@_) == 1) or die "remove_multiple_spaces: line\n";
$line = $_[0];
while($line =~ /^[\s]+(.+)$/){
$line = $1;
}
while($line =~ /^(.+)[\s]+$/){
$line = $1;
}
while($line =~ /^(.+)[\s]+[\s](.+)$/){
$line = $1 . " " . $2;
}
return $line;
}
sub execute_command {
my ($command, $debug);
(scalar(@_) == 2) or die "execute_command: command debug\n";
$command = $_[0];
$debug = $_[1];
if($debug == 0){
print STDERR "$command\n";
}
else{
(system($command) == 0) or die "Couldn't run: $command\n";
}
}
sub is_member {
my ($elt, @array, $member);
(scalar(@_) == 2) or die "is_member: member array\n";
$member = $_[0];
@array = @{$_[1]};
foreach $elt (@array){
if($elt eq $member){
return 0;
}
}
return 1;
}
sub list_directory_contents {
my ($dir, @files);
(scalar(@_) == 1) or die "list_directory_contents: dir\n";
$dir = $_[0];
if(-f $dir){
return $dir;
}
(-e $dir) or die "The directory $dir does not exist.\n";
opendir(DIR, $dir) or die "Can't opendir $dir: $!\n";
# @files = readdir(DIR);
@files = grep { (!/^\./) } readdir(DIR);
closedir DIR;
# Unix-only: $line = `ls $DATA`;
# Unix-only: ($? == 0) or die "Can't ls $DATA: $!\n";
# Unix-only @files = split(/\n/, $line);
return @files;
}
1;