MyCGI.pm 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package MyCGI;
  2. use strict;
  3. use CGI;
  4. sub getParam {
  5. my $cgi;
  6. if ($ARGV[0]) {
  7. $cgi = new CGI($ARGV[0]);
  8. } else {
  9. $cgi = new CGI;
  10. }
  11. my @param_names = $cgi->param();
  12. my %param = ();
  13. foreach (@param_names) {
  14. $param{$_} = $cgi->param($_);
  15. }
  16. return \%param;
  17. }
  18. sub getCookie {
  19. my $name = shift;
  20. my $cookie_value = &CGI::cookie($name);
  21. return &_parse($cookie_value);
  22. }
  23. sub outputHtml {
  24. my ($charset, $html) = @_;
  25. print &CGI::header(-charset => $charset);
  26. print $html;
  27. }
  28. sub outputXml {
  29. my ($charset, $xml) = @_;
  30. print &CGI::header( -type => 'text/xml', -charset => $charset );
  31. print $xml;
  32. }
  33. sub makeCookieValue {
  34. my $param = shift;
  35. my @data = ();
  36. foreach(keys %$param) {
  37. push(@data, $_ . "=" . $param->{$_});
  38. }
  39. return join("&", @data);
  40. }
  41. sub setCookie {
  42. my $param = shift;
  43. my $cookie = &CGI::cookie(
  44. -name => $param->{name} || return,
  45. -value => $param->{value},
  46. -domain => $param->{domain},
  47. -path => $param->{path},
  48. -expires => $param->{expires},
  49. );
  50. return &CGI::header(-cookie => $cookie);
  51. }
  52. sub redirect {
  53. my $dest = shift;
  54. &CGI::redirect($dest);
  55. }
  56. sub urlEncode {
  57. my $str = shift;
  58. $str =~ s/([^\w ])/'%'.unpack('H2', $1)/eg;
  59. $str =~ tr/ /+/;
  60. return $str;
  61. }
  62. sub urlDecode {
  63. my $str = shift;
  64. $str =~ tr/+/ /;
  65. $str =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack('H2', $1)/eg;
  66. return $str;
  67. }
  68. sub _parse {
  69. my $value = shift;
  70. my @pair = split(/&/, $value);
  71. my %data = ();
  72. foreach(@pair) {
  73. my ($name, $value) = split(/=/, $_);
  74. $data{$name} = $value;
  75. }
  76. return \%data;
  77. }
  78. 1;