D

 view release on metacpan or  search on metacpan

lib/D.pm  view on Meta::CPAN

            elsif($ref eq 'ARRAY'){
                $proto = $seen->{$refaddr} = [];
                @{$proto} = _apply($code, $seen, @{$arg});
            }
            elsif($ref eq 'HASH'){
                $proto = $seen->{$refaddr} = {};
                %{$proto} = _apply($code, $seen, %{$arg});
            }
            elsif($ref eq 'REF' or $ref eq 'SCALAR'){
                $proto = $seen->{$refaddr} = \do{ my $scalar };
                ${$proto} = _apply($code, $seen, ${$arg});
            }
            else{ # CODE, GLOB, IO, LVALUE etc.
                $proto = $seen->{$refaddr} = $arg;
            }
 
            push @retval, $proto;
        }
        else{
            push @retval, defined($arg) && (! $DO_NOT_PROCESS_NUMERIC_VALUE || ! _is_number($arg)) ? $code->($arg) : $arg;
        }
    }
 
    return wantarray ? @retval : $retval[0];
}
 
# Copy from Data::Recursive::Encode
sub _encode {
    my ($encoding, $stuff, $check) = @_;
    $encoding = Encode::find_encoding($encoding)
        || Carp::croak("unknown encoding '$encoding'");
    $check ||= 0;
    _apply(sub { $encoding->encode($_[0], $check) }, {}, $stuff);
}
 
# Copy from Data::Recursive::Encode
sub _is_number {
    my $value = shift;
    return 0 unless defined $value;
 
    my $b_obj = B::svref_2object(\$value);
    my $flags = $b_obj->FLAGS;
    return $flags & ( B::SVp_IOK | B::SVp_NOK ) && !( $flags & B::SVp_POK ) ? 1 : 0;
}

1;

=encoding utf8

=head1 NAME

D - Provides utility functions to encode data and dump it to STDERR.

=head1 SYNOPSIS
  
  use utf8;
  
  # Export du, dw, de, dn, dustr, dwstr, destr, dnstr functions
  use D;
  
  # Reference data that contains decoded strings
  my $data = [{name => 'あ'}, {name => 'い'}];
  
  # Encode all strings in reference data to UTF-8 and dump the reference data to STDERR.
  du $data;

  # Encode all strings in reference data to cp932 and dump the reference data to STDERR.
  dw $data;

  # Encode all strings in reference data to EUC-JP and dump the reference data to STDERR.
  de $data;

  # Dump reference data to STDERR without encoding.
  dn $data;

  # Examples of useful oneliner.
  use D;du $data;
  use D;dw $data;
  use D;de $data;
  use D;dn $data;

  # Output example of du function.
  [
    {
      'name' => 'あ'
    },
    {
      'name' => 'い'
    }
  ] at test.pl line 7.

=head1 DESCRIPTION

D module provides utility functions to encode data and dump it to STDERR.

=head1 FEATURES

=over 2

=item * Export C<du>, C<dw>, C<de>, and C<dn> functions. Don't conflict debug command such as 'p' because these function names are consist of two characters.

=item * Encode all strings in reference data in C<dustr>, C<dwstr>, and C<destr> function.

=item * C<du> is a short name of "dump UTF-8"

=item * C<dw> is a short name of "dump Windows cp932"

=item * C<de> is a short name of "dump EUC-JP"

=item * C<dn> is a short name of "dump no encoding"

=item * Use C<Dump> method of L<Data::Dumper> to dump data

=item * Print line number and file name to STDERR

=item * Keys of hash of dumped data is sorted.

=item * Don't print "$VAR1 =" unlike L<Data::Dumper> default.

=back



( run in 0.548 second using v1.01-cache-2.11-cpan-39bf76dae61 )