/[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.7 - (hide annotations)
Fri Nov 29 04:44:53 2002 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.6: +22 -34 lines
- sub array_getRelations
+ sub getNewPerlObjectByPkgName

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

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