This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Ryan Davies - JSON Server - Misterhouse

By: a guest on May 11th, 2011  |  syntax: Perl  |  size: 0.91 KB  |  views: 107  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # Authority: anyone
  2.  
  3. use JSON;
  4. use Switch;
  5. no strict;
  6.  
  7. my @parms = @ARGV;
  8. my %parms;
  9. my $size = 0;
  10.  
  11. foreach (@parms) {
  12.         $_ =~ m/(.+)=(.+)?/i;
  13.         %parms = ( %parms, $1 => $2 );
  14. }
  15. my %variables;
  16.  
  17. while ( my ( $key, $value ) = each(%parms) ) {
  18.         $size = keys %$key;
  19.         if ( $size ne 0 ) {
  20.                 if ( $value eq "" ) {
  21.                         if ( defined %$key ) { $variables{$key} = \%$key; } else { $variables{$key} = "undefined"; }
  22.                 }
  23.                 else {
  24.                         my @values = split( ',', $value );
  25.                         foreach my $val (@values) {
  26.                                 if ( defined $$key{$val} ) {
  27.                                         $variables{$key}{$val} = $$key{$val};
  28.                                 }  else { $variables{$key}{$val} = "undefined"; }
  29.                         }
  30.                 }
  31.         }
  32.         else {
  33.                 if ( defined $$key ) { $variables{$key} = $$key } else { $variables{$key} = "undefined"; }
  34.         }
  35. }
  36.  
  37. my $json = encode_json \%variables;
  38. my $js   = <<eof;
  39. HTTP/1.0 200 OK
  40. Server: MisterHouse
  41. Content-type: application/json
  42.  
  43. $json
  44. eof
  45. return $js;
  46. use strict;
clone this paste RAW Paste Data