blib/lib/Call/Haskell/ReadShow.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 160 CC 14 SCAR 29.2
CC
14
Cov
0%
showH
56.0
show_prim
12.0
show_algtype
12.0
CRAP 18.5
1 package Call::Haskell::ReadShow;
2 use version; our $VERSION = version->declare('v0.1.1');
3 use warnings;
4 use strict;
5 use v5.16;
6 # Uses ShowToPerl to serialise the output of the Haskell function
7  
8 use Data::Dumper;
9  
10 # This routine needs the type signature from the Haskell function.
11 sub showH { (my $data, my $hs_type)=@_;
12 my $t=$hs_type;
13 if(ref($t) eq 'ARRAY' && scalar( @{$t}) ==1) { # List
14 if ($hs_type->[0] eq 'Char') { # String
15 return show_string($data);
16 } else {
17 my $list_t= $t->[0];
18 return show_list($data, $list_t);
19 }
20 } elsif (ref($t) eq 'ARRAY' && scalar( @{$t})>1) { # Tuple
21 return show_tuple($data, $t);
22 } elsif (ref($t) eq 'HASH' ) { # Alg. Datatype
23 return show_algtype($data, $t);
24 } elsif ($t =~/[A-Z]/) { # Either prim, Map or Maybe
25 if($hs_type =~/^([A-Z]\w*)$/) { # Prim, which means Int, Integer, Float, Double, Integral, Fractional etc or Bool.
26 my $prim_t=$1;
27 return show_prim($data,$prim_t);
28 } else {
29 die "Something wrong (2) with the type ".Dumper($hs_type)."!\n";
30 }
31 } else {die "Something wrong (1) with the type ".Dumper($hs_type)."!\n" }
32 }
33  
34 sub show_string { '\"'.$_[0].'\"' };
35  
36 sub show_prim {
37 (my $data, my $hs_type)=@_;
38 if ($hs_type eq 'Bool') {
39 return ($data ? 'True' : 'False')
40 } else {
41 return $data;
42 }
43 }
44  
45 sub show_maybe {
46 (my $data, my $hs_type)=@_;
47 if (defined $data) {
48 return 'Just '.showH($data,$hs_type);
49 } else {
50 return 'Nothing';
51 }
52 }
53  
54 sub show_list {
55 (my $data, my $hs_type)=@_;
56 return '['.join(',', map { showH($_,$hs_type) } @{$data}).']';
57 }
58  
59 sub show_tuple {
60 (my $data, my $hs_type)=@_;
61 my @hs_types=@{$hs_type};
62 if (scalar @{$data} != scalar @hs_types) {die "Tuple type and values do not match!\n" };
63 return '('.join(',', map { showH($_,shift(@hs_types)) } @{$data}).')';
64 }
65  
66 sub show_hash {
67 (my $data,my $key_t,my $val_t)=@_;
68 my @kv_list=();
69 while ((my $k, my $v) = each(%{$data})) {
70 push @kv_list,'('.showH($k,$key_t).','.showH($v,$val_t).')';
71 }
72 return 'fromList ['.join(',',@kv_list).']';
73 }
74  
75 sub show_algtype {
76 (my $data, my $hs_type)=@_;
77 if ($hs_type->{'TypeName'} eq 'Map') {
78 (my $key_t,my $val_t) = @{$hs_type->{'TypeArgs'}};
79 show_hash($data,$key_t,$val_t);
80 } elsif ($hs_type->{'TypeName'} eq 'Maybe') {
81 show_maybe($data, $hs_type->{'TypeArgs'}->[0] );
82 } else {
83 # This is an unknown datatype. Give up
84 die "Type ". $hs_type->{'TypeName'}. " not supported, please create a Perl counterpart using the Functional::Types module\n";
85 }
86 }
87  
88 # Simply eval the string from ShowToPerl, using AUTOLOAD to clean up.
89 sub readH {
90 (my $vts)=@_;
91 my $ref=eval( $vts );
92 return $ref;
93 }
94  
95  
96 sub AUTOLOAD {
97 our $AUTOLOAD;
98 my $t=$AUTOLOAD;
99 $t=~s/^.+:://;
100 $t eq 'True' && do{$t=1};
101 $t eq 'False' && do{$t=1};
102 $t eq 'Nothing' && do{$t=undef};
103 if (not @_) {
104 return $t;
105 } else {
106 return {TypeName=>$t,TypeArgs=>[@_] };
107 }
108 }
109  
110 1;