/[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.8 - (hide annotations)
Sun Dec 1 22:11:35 2002 UTC (21 years, 11 months ago) by joko
Branch: MAIN
Changes since 1.7: +24 -1 lines
+ sub cmd
+ sub run_cmds

1 cvsjoko 1.1 #################################
2     #
3 joko 1.8 # $Id: libp.pm,v 1.7 2002/11/29 04:44:53 joko Exp $
4 cvsjoko 1.2 #
5     # $Log: libp.pm,v $
6 joko 1.8 # Revision 1.7 2002/11/29 04:44:53 joko
7     # - sub array_getRelations
8     # + sub getNewPerlObjectByPkgName
9     #
10 joko 1.7 # Revision 1.6 2002/11/17 07:18:59 joko
11     # + sub deep_copy
12     #
13 joko 1.6 # Revision 1.5 2002/10/27 18:34:28 joko
14     # + sub now
15     #
16 joko 1.5 # Revision 1.4 2002/08/16 19:06:39 cvsjoko
17     # + sub getDirList
18     #
19 cvsjoko 1.4 # Revision 1.3 2002/07/19 18:13:50 cvsjoko
20     # no message
21     #
22 cvsjoko 1.3 # Revision 1.2 2002/06/27 02:14:22 cvsjoko
23     # + stripHtml stripSpaces stripNewLines toReal
24     #
25 cvsjoko 1.2 # Revision 1.1 2002/06/24 14:49:59 cvsjoko
26     # + new
27 cvsjoko 1.1 #
28     #
29     #################################
30    
31     package libp;
32    
33 joko 1.7 use strict;
34     use warnings;
35    
36 cvsjoko 1.1 require Exporter;
37 joko 1.7 our @ISA = qw( Exporter );
38     our @EXPORT_OK = qw(
39 cvsjoko 1.3 Dumper
40     md5 md5_hex md5_base64
41     ParseDate UnixDate
42     strftime
43     stripHtml stripSpaces stripNewLines toReal trim
44     croak
45     array_getDifference
46 cvsjoko 1.4 getDirList
47 joko 1.5 now
48 joko 1.6 deep_copy
49 joko 1.7 getNewPerlObjectByPkgName
50 joko 1.8 cmd
51     run_cmds
52 cvsjoko 1.1 );
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 cvsjoko 1.2
60     require LWP::UserAgent;
61     use HTML::PullParser;
62    
63 cvsjoko 1.3 # $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 cvsjoko 1.4 use DirHandle;
70    
71 cvsjoko 1.2
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 cvsjoko 1.3 sub trim {
84     my $string = shift;
85     return stripSpaces($string);
86     }
87    
88 cvsjoko 1.2 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/&nbsp;//g;
118     return $result;
119 cvsjoko 1.3 }
120    
121    
122 cvsjoko 1.4
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 joko 1.5
194     sub now {
195     return strftime("%Y-%m-%d %H:%M:%S", localtime);
196 joko 1.6 }
197    
198 joko 1.7 # ACK's go to ...
199 joko 1.6 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 joko 1.7 }
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 joko 1.8 }
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 joko 1.5 }
239 cvsjoko 1.1
240     1;

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