Convert-Pluggable

 view release on metacpan or  search on metacpan

lib/Convert/Pluggable.pm  view on Meta::CPAN


    my $conversion = shift;
    my $matches    = get_matches( $self,
        [ $conversion->{'from_unit'}, $conversion->{'to_unit'} ] );

    return
      if !( $matches->{'type_1'} && $matches->{'type_2'} );

    if ( looks_like_number( $conversion->{'factor'} ) ) {

        # looks_like_number thinks 'Inf' and 'NaN' are numbers:
        return
          if float_is_infinite( $conversion->{'factor'} )
          || float_is_nan( $conversion->{'factor'} );

        # if factor is < 0 and first thing can't be negative ...
        return
          if $conversion->{'factor'} < 0 && !$matches->{'can_be_negative_1'};
    }
    else {
     # if it doesn't look like a number, and it contains a number (e.g., '6^2'):

t/Convert-Pluggable.t  view on Meta::CPAN


    $result = $cp->convert(
        { 'factor' => '5', 'from_unit' => 'f', 'to_unit' => 'celsius', } );
    is( $result->{'result'}, '-15', 'OK' );

    $result = $cp->convert(
        { 'factor' => '6^2', 'from_unit' => 'oz', 'to_unit' => 'grams', } );
    is( $result->{'result'}, '1020.582', 'OK' );

    $result = $cp->convert(
        { 'factor' => 'NaN', 'from_unit' => 'oz', 'to_unit' => 'stones', } );
    is( $result->{'result'}, undef, 'OK' );

    $result = $cp->convert(
        { 'factor' => '45x10', 'from_unit' => 'oz', 'to_unit' => 'stones', } );
    is( $result->{'result'}, undef, 'OK' );

    $result = $cp->convert(
        { 'factor' => '-9', 'from_unit' => 'g', 'to_unit' => 'ozs', } );
    is( $result->{'result'}, undef, 'OK' );



( run in 0.287 second using v1.01-cache-2.11-cpan-4d50c553e7e )