/[cvs]/nfo/perl/libs/libp.pm
ViewVC logotype

Contents of /nfo/perl/libs/libp.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations)
Sun Dec 22 14:15:02 2002 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.11: +16 -1 lines
+ sub mkObject

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed