da2c2d501c140cc1fe30377b2b5a806f8c262aa3
[jackhill/mal.git] / perl / interop.pm
1 package interop;
2 use strict;
3 use warnings FATAL => qw(all);
4 no if $] >= 5.018, warnings => "experimental::smartmatch";
5 use feature qw(switch);
6
7 use Exporter 'import';
8 our @EXPORT_OK = qw( pl_to_mal );
9 use Scalar::Util qw(looks_like_number);
10
11 use types qw($nil);
12
13 sub pl_to_mal {
14 my($obj) = @_;
15 given (ref $obj) {
16 when(/^ARRAY/) {
17 my @arr = map {pl_to_mal($_)} @$obj;
18 return Mal::List->new(\@arr);
19 }
20 when(/^HASH/) {
21 my $hsh = {};
22 foreach my $key (keys %$obj) {
23 $hsh->{$key} = pl_to_mal($obj->{$key});
24 }
25 return Mal::HashMap->new($hsh)
26 }
27 default {
28 if (!defined($obj)) {
29 return $nil;
30 } elsif (looks_like_number($obj)) {
31 return Mal::Integer->new($obj);
32 } else {
33 return Mal::String->new($obj);
34 }
35 }
36 }
37 }
38
39 1;