| 1 | package Any::Renderer::Data::Dumper; | ||||||||||||
| 2 | |||||||||||||
| 3 | # $Id: Dumper.pm,v 1.9 2006/08/23 19:41:16 johna Exp $ | ||||||||||||
| 4 | |||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use vars qw($VERSION); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 3 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 3 | $VERSION = sprintf"%d.%03d", q$Revision: 1.9 $ =~ /: (\d+)\.(\d+)/; | |||||||||||
| 10 | |||||||||||||
| 11 | 3 | ▶ | use constant FORMAT_NAME => 'Data::Dumper'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | sub new | ||||||||||||
| 14 | 7 | ▶ | { | ||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 15 | 7 | my ( $class, $format, $options ) = @_; | |||||||||||
| 16 | 7 | ▶ | die("Invalid format $format") unless($format eq FORMAT_NAME); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | |||||||||||||
| 18 | 6 | ▶ | $options ||= {}; | ||||||||||
|
Condition50%1/2
$options ||= {}
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||
| 19 | my $self = { | ||||||||||||
| 20 | 'options' => $options, | ||||||||||||
| 21 | 'varname' => delete $options->{VariableName}, | ||||||||||||
| 22 | 6 | }; | |||||||||||
| 23 | |||||||||||||
| 24 | 6 | ▶ | $options->{DumperOptions} ||= {}; | ||||||||||
|
Condition100%2/2
$options->{"DumperOptions"} ||= {}
MC/DC100%1/1
$options->{"DumperOptions"} ||= {}
$options->{"DumperOptions"}
Truth table100%2/2
A
A= $options->{"DumperOptions"}
| |||||||||||||
| 25 | 6 | foreach my $opt(keys %{$options->{DumperOptions}}) { | |||||||||||
| 26 | 2 | ▶ | die("Data::Dumper does not support a $opt method") unless(Data::Dumper->can($opt)); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | 5 | bless $self, $class; | |||||||||||
| 30 | |||||||||||||
| 31 | 5 | return $self; | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | sub render | ||||||||||||
| 35 | 11 | ▶ | { | ||||||||||
|
Subroutine
render: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 36 | 11 | my ( $self, $data ) = @_; | |||||||||||
| 37 | |||||||||||||
| 38 | 11 | TRACE ( "Rendering w/Data::Dumper" ); | |||||||||||
| 39 | 11 | DUMP ( $data ); | |||||||||||
| 40 | |||||||||||||
| 41 | 11 | my $o = Data::Dumper->new([ $data ], [ $self->{ 'varname' } ]); | |||||||||||
| 42 | 11 | $o->Sortkeys(1); | |||||||||||
| 43 | 11 | foreach my $opt(keys %{$self->{options}{DumperOptions}}) { | |||||||||||
| 44 | 1 | $o->$opt($self->{options}{DumperOptions}{$opt}); #Fire corresponding methods | |||||||||||
| 45 | } | ||||||||||||
| 46 | 11 | return $o->Dump(); | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | sub requires_template | ||||||||||||
| 50 | 2 | ▶ | { | ||||||||||
|
Subroutine
requires_template: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 51 | 2 | return 0; | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | sub available_formats | ||||||||||||
| 55 | 3 | ▶ | { | ||||||||||
|
Subroutine
available_formats: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 56 | 3 | return [ FORMAT_NAME ]; | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 11 | ▶ | sub TRACE {} | ||||||||||
|
Subroutine
TRACE: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 60 | 11 | ▶ | sub DUMP {} | ||||||||||
|
Subroutine
DUMP: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 61 | |||||||||||||
| 62 | 3 | 1; | |||||||||||
| 63 | |||||||||||||
| 64 | =head1 NAME | ||||||||||||
| 65 | |||||||||||||
| 66 | Any::Renderer::Data::Dumper - render data structures through Data::Dumper | ||||||||||||
| 67 | |||||||||||||
| 68 | =head1 SYNOPSIS | ||||||||||||
| 69 | |||||||||||||
| 70 | use Any::Renderer; | ||||||||||||
| 71 | |||||||||||||
| 72 | my %options = ('DumperOptions' => {'Indent' => 1}); | ||||||||||||
| 73 | my $format = "Data::Dumper"; | ||||||||||||
| 74 | my $r = new Any::Renderer ( $format, \%options ); | ||||||||||||
| 75 | |||||||||||||
| 76 | my $data_structure = [...]; # arbitrary structure | ||||||||||||
| 77 | my $string = $r->render ( $data_structure ); | ||||||||||||
| 78 | |||||||||||||
| 79 | =head1 DESCRIPTION | ||||||||||||
| 80 | |||||||||||||
| 81 | Any::Renderer::Data::Dumper renders any Perl data structure passed to it into | ||||||||||||
| 82 | a string representation via Data::Dumper. For example: | ||||||||||||
| 83 | |||||||||||||
| 84 | perl -MAny::Renderer -e "print Any::Renderer->new('Data::Dumper')->render({a => 1, b => [2,3]})" | ||||||||||||
| 85 | |||||||||||||
| 86 | results in: | ||||||||||||
| 87 | |||||||||||||
| 88 | $VAR1 = { | ||||||||||||
| 89 | 'a' => 1, | ||||||||||||
| 90 | 'b' => [ | ||||||||||||
| 91 | 2, | ||||||||||||
| 92 | 3 | ||||||||||||
| 93 | ] | ||||||||||||
| 94 | }; | ||||||||||||
| 95 | |||||||||||||
| 96 | =head1 FORMATS | ||||||||||||
| 97 | |||||||||||||
| 98 | =over 4 | ||||||||||||
| 99 | |||||||||||||
| 100 | =item Data::Dumper | ||||||||||||
| 101 | |||||||||||||
| 102 | =back | ||||||||||||
| 103 | |||||||||||||
| 104 | =head1 METHODS | ||||||||||||
| 105 | |||||||||||||
| 106 | =over 4 | ||||||||||||
| 107 | |||||||||||||
| 108 | =item $r = new Any::Renderer::Data::Dumper($format, \%options) | ||||||||||||
| 109 | |||||||||||||
| 110 | C<$format> must be C<Data::Dumper>. | ||||||||||||
| 111 | See L</OPTIONS> for a description of valid C<%options>. | ||||||||||||
| 112 | |||||||||||||
| 113 | =item $string = $r->render($data_structure) | ||||||||||||
| 114 | |||||||||||||
| 115 | The main method. | ||||||||||||
| 116 | |||||||||||||
| 117 | =item $bool = Any::Renderer::Data::Dumper::requires_template($format) | ||||||||||||
| 118 | |||||||||||||
| 119 | False in this case. | ||||||||||||
| 120 | |||||||||||||
| 121 | =item $list_ref = Any::Renderer::Data::Dumper::available_formats() | ||||||||||||
| 122 | |||||||||||||
| 123 | Just the one - C<Data::Dumper>. | ||||||||||||
| 124 | |||||||||||||
| 125 | =back | ||||||||||||
| 126 | |||||||||||||
| 127 | =head1 OPTIONS | ||||||||||||
| 128 | |||||||||||||
| 129 | =over 4 | ||||||||||||
| 130 | |||||||||||||
| 131 | =item VariableName | ||||||||||||
| 132 | |||||||||||||
| 133 | Name of the perl variable the structure is assigned to. Defaults to C<$VAR1>. | ||||||||||||
| 134 | |||||||||||||
| 135 | =item DumperOptions | ||||||||||||
| 136 | |||||||||||||
| 137 | This hashref of options is mapped to Data::Dumper methods (Indent, Purity, Useqq, etc.). For example: | ||||||||||||
| 138 | |||||||||||||
| 139 | perl -MAny::Renderer -e "print Any::Renderer->new('Data::Dumper', {DumperOptions => {Indent=>0}})->render({a => 1, b => [2,3]})" | ||||||||||||
| 140 | |||||||||||||
| 141 | results in: | ||||||||||||
| 142 | |||||||||||||
| 143 | $VAR1 = {'a' => 1,'b' => [2,3]}; | ||||||||||||
| 144 | |||||||||||||
| 145 | See L<Data::Dumper> for the list of I<Configuration Variables or Methods>. | ||||||||||||
| 146 | |||||||||||||
| 147 | =back | ||||||||||||
| 148 | |||||||||||||
| 149 | =head1 SEE ALSO | ||||||||||||
| 150 | |||||||||||||
| 151 | L<Data::Dumper>, L<Any::Renderer> | ||||||||||||
| 152 | |||||||||||||
| 153 | =head1 VERSION | ||||||||||||
| 154 | |||||||||||||
| 155 | $Revision: 1.9 $ on $Date: 2006/08/23 19:41:16 $ by $Author: johna $ | ||||||||||||
| 156 | |||||||||||||
| 157 | =head1 AUTHOR | ||||||||||||
| 158 | |||||||||||||
| 159 | Matt Wilson and John Alden <cpan _at_ bbc _dot_ co _dot_ uk> | ||||||||||||
| 160 | |||||||||||||
| 161 | =head1 COPYRIGHT | ||||||||||||
| 162 | |||||||||||||
| 163 | (c) BBC 2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL. | ||||||||||||
| 164 | |||||||||||||
| 165 | See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt | ||||||||||||
| 166 | |||||||||||||
| 167 | =cut | ||||||||||||