File Coverage

blib/lib/Acme/IsItJSON.pm
Criterion Covered Total %
statement 24 25 96.0
branch 3 4 75.0
condition n/a
subroutine 7 7 100.0
pod 1 2 50.0
total 35 38 92.1


line stmt bran cond sub pod time code
1             =encoding UTF-8
2              
3             =head1 NAME
4              
5             Acme::IsItJSON - Is my variable JSON or a Perl data structure?
6              
7             =head1 SYNOPSIS
8              
9             use Acme::IsItJSON 'is_it_json';
10             my $json = '{"zilog":"z80"}';
11             is_it_json ($json);
12             my $perl = {zilog => 'z80'};
13             is_it_json ($json);
14              
15             =head1 DESCRIPTION
16              
17             Not sure if your variable is a Perl data structure or a JSON string?
18              
19             This Perl module can help.
20              
21             =head1 FUNCTIONS
22              
23             =head2 is_it_json
24              
25             Given a variable containing something which you are not sure about,
26             and it may or may not be JSON or a Perl data structure, feed it to
27             this routine. This module uses support vector machines running on an
28             OCAML cluster backed up by a Node pipeline in an S3 cloud to
29             distinguish JSON from Perl data structures.
30              
31             =cut
32              
33             package Acme::IsItJSON;
34             require Exporter;
35             @ISA = qw(Exporter);
36             @EXPORT_OK = qw/is_it_json/;
37             %EXPORT_TAGS = (
38             all => \@EXPORT_OK,
39             );
40 1     1   20943 use warnings;
  1         2  
  1         29  
41 1     1   5 use strict;
  1         2  
  1         21  
42 1     1   5 use Carp;
  1         5  
  1         81  
43 1     1   746 use JSON::Parse qw/parse_json valid_json/;
  1         968  
  1         72  
44 1     1   734 use JSON::Create 'create_json';
  1         869  
  1         279  
45             our $VERSION = 0.01;
46              
47             my @responses = (
48             "That seems to be {X}.",
49             "That might be {X}.",
50             "I'm not sure whether that is {X}.",
51             "It could be {X}.",
52             "OK, it's definitely {X}. Maybe.",
53             );
54              
55             sub babble
56             {
57 2     2 0 4 my ($what) = @_;
58 2         35 my $response = $responses[int (rand (scalar (@responses)))];
59 2         9 $response =~ s/\{X\}/$what/;
60 2 50       7 if (rand (2) > 1) {
61 0         0 $response = create_json ($response);
62             }
63 2         11 print "$response\n";
64             }
65              
66             sub is_it_json
67             {
68 2     2 1 1845 my ($input) = @_;
69 2 100       8 if (valid_json ($input)) {
70 1         22 babble ('JSON');
71             }
72             else {
73 1         45 babble ('a Perl data structure');
74             }
75             }
76              
77             1;