1 |
################################# |
2 |
# |
3 |
# $Id: libp.pm,v 1.7 2002/11/29 04:44:53 joko Exp $ |
4 |
# |
5 |
# $Log: libp.pm,v $ |
6 |
# Revision 1.7 2002/11/29 04:44:53 joko |
7 |
# - sub array_getRelations |
8 |
# + sub getNewPerlObjectByPkgName |
9 |
# |
10 |
# Revision 1.6 2002/11/17 07:18:59 joko |
11 |
# + sub deep_copy |
12 |
# |
13 |
# Revision 1.5 2002/10/27 18:34:28 joko |
14 |
# + sub now |
15 |
# |
16 |
# Revision 1.4 2002/08/16 19:06:39 cvsjoko |
17 |
# + sub getDirList |
18 |
# |
19 |
# Revision 1.3 2002/07/19 18:13:50 cvsjoko |
20 |
# no message |
21 |
# |
22 |
# Revision 1.2 2002/06/27 02:14:22 cvsjoko |
23 |
# + stripHtml stripSpaces stripNewLines toReal |
24 |
# |
25 |
# Revision 1.1 2002/06/24 14:49:59 cvsjoko |
26 |
# + new |
27 |
# |
28 |
# |
29 |
################################# |
30 |
|
31 |
package libp; |
32 |
|
33 |
use strict; |
34 |
use warnings; |
35 |
|
36 |
require Exporter; |
37 |
our @ISA = qw( Exporter ); |
38 |
our @EXPORT_OK = qw( |
39 |
Dumper |
40 |
md5 md5_hex md5_base64 |
41 |
ParseDate UnixDate |
42 |
strftime |
43 |
stripHtml stripSpaces stripNewLines toReal trim |
44 |
croak |
45 |
array_getDifference |
46 |
getDirList |
47 |
now |
48 |
deep_copy |
49 |
getNewPerlObjectByPkgName |
50 |
cmd |
51 |
run_cmds |
52 |
); |
53 |
|
54 |
use Data::Dumper; |
55 |
use Digest::MD5 qw(md5 md5_hex md5_base64); |
56 |
|
57 |
$main::TZ = 'GMT'; |
58 |
use Date::Manip; |
59 |
|
60 |
require LWP::UserAgent; |
61 |
use HTML::PullParser; |
62 |
|
63 |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
64 |
# see "perldoc -f localtime" |
65 |
use POSIX qw(strftime); |
66 |
|
67 |
use Carp; |
68 |
|
69 |
use DirHandle; |
70 |
|
71 |
|
72 |
######################################## |
73 |
|
74 |
sub stripSpaces { |
75 |
my $text = shift; |
76 |
#print "text: $text", "\n"; |
77 |
#print "ord: ", ord(substr($text, 0, 1)), "\n"; |
78 |
$text =~ s/^\s*//g; |
79 |
$text =~ s/\s*$//g; |
80 |
return $text; |
81 |
} |
82 |
|
83 |
sub trim { |
84 |
my $string = shift; |
85 |
return stripSpaces($string); |
86 |
} |
87 |
|
88 |
sub stripNewLines { |
89 |
my $text = shift; |
90 |
#print "text: $text", "\n"; |
91 |
#print "ord: ", ord(substr($text, 0, 1)), "\n"; |
92 |
$text =~ s/\n//g; |
93 |
#$text =~ s/\s*$//g; |
94 |
return $text; |
95 |
} |
96 |
|
97 |
sub toReal { |
98 |
my $string = shift; |
99 |
$string =~ m/(\d+\.*\d+)/; |
100 |
my $real = $1; |
101 |
return $real; |
102 |
} |
103 |
|
104 |
sub stripHtml { |
105 |
my $html = shift; |
106 |
my $result = ''; |
107 |
#$html =~ s/<br>(.*)/ - ($1)/i; |
108 |
my $p = HTML::PullParser->new( |
109 |
doc => \$html, |
110 |
text => 'text', |
111 |
unbroken_text => 1, |
112 |
); |
113 |
while (my $token = $p->get_token()) { |
114 |
my $text = join('', @{$token}); |
115 |
$result .= $text; |
116 |
} |
117 |
#$result =~ s/ //g; |
118 |
return $result; |
119 |
} |
120 |
|
121 |
|
122 |
|
123 |
|
124 |
# ============================================= |
125 |
# "global" vars used in directory-recursive-parsing |
126 |
my $dirlist_buf; |
127 |
my @dirlist_path; |
128 |
my $dirlist_base; |
129 |
|
130 |
sub entry_callback { |
131 |
|
132 |
my $entry = shift; |
133 |
|
134 |
# CHECKS |
135 |
# dont't use this: |
136 |
if ($entry eq '.' || $entry eq '..') { return; } |
137 |
|
138 |
# PREPARE |
139 |
# prepare path to current entry |
140 |
my $cur_entry = join('/', @dirlist_path, $entry); |
141 |
# prepare path to current entry (absolute) |
142 |
my $cur_entry_abs = join('/', $dirlist_base, @dirlist_path, $entry); |
143 |
|
144 |
# ENTRY |
145 |
# add current entry to buffer |
146 |
$dirlist_buf .= $cur_entry . "\n"; |
147 |
|
148 |
# (SUB-)DIRECTORY |
149 |
# check if current entry is a (sub-)directory ... |
150 |
if (-d $cur_entry_abs) { |
151 |
push @dirlist_path, $cur_entry; |
152 |
# ... and parse this (recursion here!!!) |
153 |
iterate_path($cur_entry_abs); |
154 |
pop @dirlist_path; |
155 |
} |
156 |
} |
157 |
|
158 |
sub iterate_path { |
159 |
|
160 |
my $path = shift; |
161 |
|
162 |
# create new "DirHandle"-object |
163 |
my $d = new DirHandle $path; |
164 |
if (defined $d) { |
165 |
|
166 |
# iterate through all entries in $path ($d->read) and call out entry-handler on each entry |
167 |
while (defined(my $line = $d->read)) { |
168 |
entry_callback($line); |
169 |
} |
170 |
|
171 |
undef $d; |
172 |
} |
173 |
} |
174 |
|
175 |
sub getDirList { |
176 |
|
177 |
$dirlist_base = shift; |
178 |
|
179 |
# reset vars |
180 |
$dirlist_buf = ''; |
181 |
@dirlist_path = (); |
182 |
|
183 |
# start parsing file-structure |
184 |
iterate_path($dirlist_base); |
185 |
|
186 |
# return complete list of directory-content including files and subdirs |
187 |
# entries are newline (\n) - seperated |
188 |
return $dirlist_buf; |
189 |
|
190 |
} |
191 |
# ============================================= |
192 |
|
193 |
|
194 |
sub now { |
195 |
return strftime("%Y-%m-%d %H:%M:%S", localtime); |
196 |
} |
197 |
|
198 |
# ACK's go to ... |
199 |
sub deep_copy { |
200 |
my $this = shift; |
201 |
if (not ref $this) { |
202 |
$this; |
203 |
} elsif (ref $this eq "ARRAY") { |
204 |
[map deep_copy($_), @$this]; |
205 |
} elsif (ref $this eq "HASH") { |
206 |
+{map { $_ => deep_copy($this->{$_}) } keys %$this}; |
207 |
} elsif (ref $this eq "CODE") { |
208 |
$this; |
209 |
} else { die "what type is $_?" } |
210 |
} |
211 |
|
212 |
sub getNewPerlObjectByPkgName { |
213 |
my $pkgname = shift; |
214 |
my $args = shift; |
215 |
#$logger->debug( __PACKAGE__ . "->getNewPerlObjectByPkgName( pkgname $pkgname args $args )" ); |
216 |
my $evstring = "use $pkgname;"; |
217 |
eval($evstring); |
218 |
#$@ && $logger->error( __PACKAGE__ . ':' . __LINE__ . " Error in eval $evstring: " . $@ ); |
219 |
$@ && print( __PACKAGE__ . ':' . __LINE__ . " Error in eval \"$evstring\": " . $@ ); |
220 |
return $pkgname->new($args); |
221 |
} |
222 |
|
223 |
sub cmd ($) { |
224 |
my $cmd = shift; |
225 |
$cmd = 'perl ' . $cmd; |
226 |
my $sep = "-" x 90; |
227 |
print $sep, "\n"; |
228 |
print " ", $cmd, "\n"; |
229 |
print $sep, "\n"; |
230 |
system($cmd); |
231 |
print "\n"; |
232 |
} |
233 |
|
234 |
sub run_cmds { |
235 |
foreach (@_) { |
236 |
cmd($_); |
237 |
} |
238 |
} |
239 |
|
240 |
1; |