aboutsummaryrefslogtreecommitdiff
path: root/perl/interop.pm
blob: ffa379f23d2943e35e165410a786d19291ac5b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package interop;
use strict;
use warnings FATAL => qw(all);
no if $] >= 5.018, warnings => "experimental::smartmatch";
use feature qw(switch);
use Exporter 'import';
our @EXPORT_OK = qw( pl_to_mal );
use Scalar::Util qw(looks_like_number);

use types;

sub pl_to_mal {
    my($obj) = @_;
    given (ref $obj) {
        when(/^ARRAY/) {
            my @arr = map {pl_to_mal($_)} @$obj;
            return List->new(\@arr);
        }
        when(/^HASH/) {
            my $hsh = {};
            foreach my $key (keys %$obj) {
                $hsh->{$key} = pl_to_mal($obj->{$key});
            }
            return HashMap->new($hsh)
        }
        default {
            if (looks_like_number($obj)) {
                return Integer->new($obj);
            } else {
                return String->new($obj);
            }
        }
    }
}

1;