Sidef

 view release on metacpan or  search on metacpan

lib/Sidef/Types/Perl/Perl.pm  view on Meta::CPAN

                or $ref eq 'Math::BigInt'
                or $ref eq 'Math::BigRat'
                or $ref eq 'Math::BigInt::Lite') {
                return Sidef::Types::Number::Number->new($val->bstr);
            }

            if ($ref eq 'Math::Complex') {
                return Sidef::Types::Number::Complex->new($val->Re, $val->Im);
            }

            if (   $ref eq 'Math::MPFR'
                or $ref eq 'Math::GMPz'
                or $ref eq 'Math::GMPq'
                or $ref eq 'Math::MPC') {
                return Sidef::Types::Number::Number->new($val);
            }

            if ($ref eq '') {

                if (Scalar::Util::looks_like_number($val) and $val !~ /^0[0-9]/) {

                    if ($val =~ tr/e.//) {    # parse as float
                        return Sidef::Types::Number::Number::_set_str('float', "$val");
                    }

                    return Sidef::Types::Number::Number->new("$val");
                }

                return Sidef::Types::String::String->new($val);
            }

            if ($ref eq 'JSON::PP::Boolean') {
                return (
                        $val
                        ? Sidef::Types::Bool::Bool::TRUE
                        : Sidef::Types::Bool::Bool::FALSE
                       );
            }

            # Return an OO object when $val is blessed
            if (defined Scalar::Util::blessed($val)) {
                return Sidef::Module::OO->__NEW__($val);
            }

            $val;
          }
          ->($data);
    }

    sub execute {
        ref($_[0]) || shift(@_);
        my ($self) = @_;
        __PACKAGE__->to_sidef(CORE::eval($$self));
    }

    *run  = \&execute;
    *eval = \&execute;

    sub tie {
        my ($self, $variable, $class_name, @args) = @_;
        state $x = require Scalar::Util;
        my $type = Scalar::Util::reftype($variable);
        __PACKAGE__->to_sidef(
                              CORE::tie(
                                        ($type eq 'ARRAY' ? (@$variable) : $type eq 'HASH' ? %$variable : $variable),
                                        "$class_name",
                                        map { defined($_) ? $_->get_value : $_ } @args
                                       )
                             );
    }

    sub untie {
        my ($self, $variable) = @_;
        state $x = require Scalar::Util;
        my $type = Scalar::Util::reftype($variable);
        __PACKAGE__->to_sidef(CORE::untie($type eq 'ARRAY' ? (@$variable) : $type eq 'HASH' ? %$variable : $variable));
    }

    sub dump {
        my ($self) = @_;
        Sidef::Types::String::String->new("$self");
    }

    *to_s   = \&dump;
    *to_str = \&dump;
};

1



( run in 3.086 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )