| 1 | 
################################# | 
| 2 | 
# | 
| 3 | 
#  $Id: libp.pm,v 1.2 2002/06/27 02:14:22 cvsjoko Exp $ | 
| 4 | 
# | 
| 5 | 
#  $Log: libp.pm,v $ | 
| 6 | 
#  Revision 1.2  2002/06/27 02:14:22  cvsjoko | 
| 7 | 
#  + stripHtml stripSpaces stripNewLines toReal | 
| 8 | 
# | 
| 9 | 
#  Revision 1.1  2002/06/24 14:49:59  cvsjoko | 
| 10 | 
#  + new | 
| 11 | 
# | 
| 12 | 
# | 
| 13 | 
################################# | 
| 14 | 
 | 
| 15 | 
package libp; | 
| 16 | 
 | 
| 17 | 
require Exporter; | 
| 18 | 
@ISA = qw( Exporter ); | 
| 19 | 
@EXPORT = qw(  | 
| 20 | 
    Dumper | 
| 21 | 
    md5 md5_hex md5_base64 | 
| 22 | 
    ParseDate UnixDate | 
| 23 | 
    strftime | 
| 24 | 
    stripHtml stripSpaces stripNewLines toReal trim | 
| 25 | 
    croak | 
| 26 | 
    array_getDifference | 
| 27 | 
); | 
| 28 | 
 | 
| 29 | 
use strict; | 
| 30 | 
use warnings; | 
| 31 | 
 | 
| 32 | 
use Data::Dumper; | 
| 33 | 
use Digest::MD5 qw(md5 md5_hex md5_base64); | 
| 34 | 
 | 
| 35 | 
$main::TZ = 'GMT'; | 
| 36 | 
use Date::Manip; | 
| 37 | 
 | 
| 38 | 
require LWP::UserAgent; | 
| 39 | 
use HTML::PullParser; | 
| 40 | 
 | 
| 41 | 
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; | 
| 42 | 
# see "perldoc -f localtime" | 
| 43 | 
use POSIX qw(strftime); | 
| 44 | 
 | 
| 45 | 
use Carp; | 
| 46 | 
 | 
| 47 | 
 | 
| 48 | 
######################################## | 
| 49 | 
 | 
| 50 | 
sub stripSpaces { | 
| 51 | 
  my $text = shift; | 
| 52 | 
  #print "text: $text", "\n"; | 
| 53 | 
  #print "ord: ", ord(substr($text, 0, 1)), "\n"; | 
| 54 | 
  $text =~ s/^\s*//g; | 
| 55 | 
  $text =~ s/\s*$//g; | 
| 56 | 
  return $text; | 
| 57 | 
} | 
| 58 | 
 | 
| 59 | 
sub trim { | 
| 60 | 
  my $string = shift; | 
| 61 | 
  return stripSpaces($string); | 
| 62 | 
} | 
| 63 | 
 | 
| 64 | 
sub stripNewLines { | 
| 65 | 
  my $text = shift; | 
| 66 | 
  #print "text: $text", "\n"; | 
| 67 | 
  #print "ord: ", ord(substr($text, 0, 1)), "\n"; | 
| 68 | 
  $text =~ s/\n//g; | 
| 69 | 
  #$text =~ s/\s*$//g; | 
| 70 | 
  return $text; | 
| 71 | 
} | 
| 72 | 
 | 
| 73 | 
sub toReal { | 
| 74 | 
  my $string = shift; | 
| 75 | 
  $string =~ m/(\d+\.*\d+)/; | 
| 76 | 
  my $real = $1; | 
| 77 | 
  return $real; | 
| 78 | 
} | 
| 79 | 
 | 
| 80 | 
sub stripHtml { | 
| 81 | 
  my $html = shift; | 
| 82 | 
  my $result = ''; | 
| 83 | 
  #$html =~ s/<br>(.*)/ - ($1)/i; | 
| 84 | 
  my $p = HTML::PullParser->new( | 
| 85 | 
    doc => \$html, | 
| 86 | 
    text => 'text', | 
| 87 | 
    unbroken_text => 1, | 
| 88 | 
  ); | 
| 89 | 
  while (my $token = $p->get_token()) { | 
| 90 | 
    my $text = join('', @{$token}); | 
| 91 | 
    $result .= $text; | 
| 92 | 
  } | 
| 93 | 
  #$result =~ s/ //g; | 
| 94 | 
  return $result; | 
| 95 | 
} | 
| 96 | 
 | 
| 97 | 
sub array_getRelations { | 
| 98 | 
  my $a_ref = shift; | 
| 99 | 
  my $b_ref = shift; | 
| 100 | 
  my @a = @{$a_ref}; | 
| 101 | 
  my @b = @{$b_ref}; | 
| 102 | 
 | 
| 103 | 
  my @isect = my @diff = my @union = (); | 
| 104 | 
  my $e; | 
| 105 | 
  my %count; | 
| 106 | 
   | 
| 107 | 
  foreach $e (@a, @b) { $count{$e}++ } | 
| 108 | 
 | 
| 109 | 
  foreach $e (keys %count) { | 
| 110 | 
      push(@union, $e); | 
| 111 | 
      push @{ $count{$e} == 2 ? \@isect : \@diff }, $e; | 
| 112 | 
  } | 
| 113 | 
   | 
| 114 | 
  my $result = { | 
| 115 | 
    union => \@union, | 
| 116 | 
    isect => \@isect, | 
| 117 | 
    diff => \@diff, | 
| 118 | 
  }; | 
| 119 | 
 | 
| 120 | 
} | 
| 121 | 
 | 
| 122 | 
sub array_getDifference { | 
| 123 | 
  my $res = array_getRelations(shift, shift); | 
| 124 | 
  return $res->{diff}; | 
| 125 | 
} | 
| 126 | 
 | 
| 127 | 
1; |