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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide annotations)
Mon Dec 23 04:25:13 2002 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.12: +10 -1 lines
+ sub bool2status

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

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