| 1 | package autobox::dump; | ||||||||||
| 2 | # vi: ht=4 sw=4 ts=4 et : | ||||||||||
| 3 | |||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 4 | ▶ | use base "autobox"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 4 | our $VERSION = '20090426.1746'; | |||||||||
| 10 | |||||||||||
| 11 | 4 | our @options = qw/Indent Terse Useqq Sortkeys Deparse/; | |||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | sub options { | ||||||||
|
Subroutine
options: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 14 | #let them call it autobox::dump::options or autobox::dump->options | ||||||||||
| 15 | 1 | ▶ | shift @_ if $_[0] eq 'autobox::dump'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 1 | @options = @_; | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 4 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 4 | my $class = shift; | |||||||||
| 21 | |||||||||||
| 22 | 4 | my $dumper = "autobox::dump::inner"; | |||||||||
| 23 | |||||||||||
| 24 | 4 | $class->SUPER::import( | |||||||||
| 25 | SCALAR => $dumper, | ||||||||||
| 26 | ARRAY => $dumper, | ||||||||||
| 27 | HASH => $dumper, | ||||||||||
| 28 | CODE => $dumper, | ||||||||||
| 29 | ); | ||||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | { | ||||||||||
| 33 | package autobox::dump::inner; | ||||||||||
| 34 | |||||||||||
| 35 | 21 | ▶ | sub perl { | ||||||||
|
Subroutine
perl: called CC=5 SCAR=16.1
| |||||||||||
| 36 | 21 | require Data::Dumper; | |||||||||
| 37 | #use Test::More; | ||||||||||
| 38 | |||||||||||
| 39 | 21 | my $self = shift; | |||||||||
| 40 | 21 | ▶ | my @options = @_ ? @_ : @autobox::dump::options; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | 21 | my $dumper = Data::Dumper->new([$self]); | |||||||||
| 43 | |||||||||||
| 44 | 21 | for my $option (@options) { | |||||||||
| 45 | 94 | ▶ | my ($opt, @opt_args) = ref $option ? @$option : ($option, 1); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | 94 | ▶ | $dumper->$opt(@opt_args) if $dumper->can($opt); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 21 | return $dumper->Dump; | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | =head1 NAME | ||||||||||
| 56 | |||||||||||
| 57 | autobox::dump - human/perl readable strings from the results of an EXPR | ||||||||||
| 58 | |||||||||||
| 59 | =head1 VERSION | ||||||||||
| 60 | |||||||||||
| 61 | Version 20090426.1746 | ||||||||||
| 62 | |||||||||||
| 63 | =head1 SYNOPSIS | ||||||||||
| 64 | |||||||||||
| 65 | The autobox::dump pragma adds, via the autobox pragma, a method to | ||||||||||
| 66 | normal expression (such as scalars, arrays, hashes, math, literals, etc.) | ||||||||||
| 67 | that produces a human/perl readable representation of the value of that | ||||||||||
| 68 | expression. | ||||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | use autobox::dump; | ||||||||||
| 72 | |||||||||||
| 73 | my $foo = "foo"; | ||||||||||
| 74 | print $foo->perl; # "foo"; | ||||||||||
| 75 | |||||||||||
| 76 | print +(5*6)->perl; # 30; | ||||||||||
| 77 | |||||||||||
| 78 | my @a = (1..3); | ||||||||||
| 79 | |||||||||||
| 80 | print @a->perl; | ||||||||||
| 81 | # [ | ||||||||||
| 82 | # 1, | ||||||||||
| 83 | # 2, | ||||||||||
| 84 | # 3 | ||||||||||
| 85 | # ]; | ||||||||||
| 86 | |||||||||||
| 87 | print {a=>1, b=>2}->perl; | ||||||||||
| 88 | # { | ||||||||||
| 89 | # "a" => 1, | ||||||||||
| 90 | # "b" => 2 | ||||||||||
| 91 | # }; | ||||||||||
| 92 | |||||||||||
| 93 | sub func { | ||||||||||
| 94 | my ($x, $y) = @_; | ||||||||||
| 95 | return $x + $y; | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | my $func = \&func; | ||||||||||
| 99 | print $func->perl; | ||||||||||
| 100 | #sub { | ||||||||||
| 101 | # BEGIN { | ||||||||||
| 102 | # $^H{'autobox_scope'} = q(154456408); | ||||||||||
| 103 | # $^H{'autobox'} = q(HASH(0x93a3e00)); | ||||||||||
| 104 | # $^H{'autobox_leave'} = q(Scope::Guard=ARRAY(0x9435078)); | ||||||||||
| 105 | # } | ||||||||||
| 106 | # my($x, $y) = @_; | ||||||||||
| 107 | # return $x + $y; | ||||||||||
| 108 | #} | ||||||||||
| 109 | |||||||||||
| 110 | You can set Data::Dumper options by passing either arrayrefs of option | ||||||||||
| 111 | and value pairs or just keys (in which case the option will be set to | ||||||||||
| 112 | 1). The default options are C<qw/Indent Terse Useqq Sortkeys Deparse/>. | ||||||||||
| 113 | |||||||||||
| 114 | print ["a", 0, 1]->perl([Indent => 3], [Varname => "a"], qw/Useqq/); | ||||||||||
| 115 | #$a1 = [ | ||||||||||
| 116 | # #0 | ||||||||||
| 117 | # "a", | ||||||||||
| 118 | # #1 | ||||||||||
| 119 | # 0, | ||||||||||
| 120 | # #2 | ||||||||||
| 121 | # 1 | ||||||||||
| 122 | # ]; | ||||||||||
| 123 | |||||||||||
| 124 | You can also call the class method ->options to set a different default. | ||||||||||
| 125 | |||||||||||
| 126 | #set Indent to 0, but leave the rest of the options | ||||||||||
| 127 | autobox::dump->options([Indent => 0], qw/Terse Useqq Sortkeys Deparse/); | ||||||||||
| 128 | |||||||||||
| 129 | print ["a", 0, 1]->perl; #["a",0,1] | ||||||||||
| 130 | |||||||||||
| 131 | =head1 AUTHOR | ||||||||||
| 132 | |||||||||||
| 133 | Chas. J Owens IV, C<< <chas.owens at gmail.com> >> | ||||||||||
| 134 | |||||||||||
| 135 | =head1 BUGS | ||||||||||
| 136 | |||||||||||
| 137 | Has all the issues L<autobox> has. | ||||||||||
| 138 | |||||||||||
| 139 | Has all the issues L<Data::Dumper> has. | ||||||||||
| 140 | |||||||||||
| 141 | This pragma errs on the side of human readable to the detriment of | ||||||||||
| 142 | Perl readable. In particular it uses the terse and deparse options | ||||||||||
| 143 | of Data::Dumper by default. These options may create code that cannot | ||||||||||
| 144 | be eval'ed. For best eval results, set options to C<qw/Purity/>. | ||||||||||
| 145 | Note, this turns off coderef dumping. | ||||||||||
| 146 | |||||||||||
| 147 | Please report any bugs or feature requests to | ||||||||||
| 148 | http://github.com/cowens/autobox-dump/issues | ||||||||||
| 149 | |||||||||||
| 150 | =head1 SUPPORT | ||||||||||
| 151 | |||||||||||
| 152 | You can find documentation for this module with the perldoc command. | ||||||||||
| 153 | |||||||||||
| 154 | perldoc autobox::dump | ||||||||||
| 155 | |||||||||||
| 156 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 157 | |||||||||||
| 158 | Michael Schwern for starting the perl5i pragma which prompted me to add | ||||||||||
| 159 | a feature I wanted to autobox. | ||||||||||
| 160 | |||||||||||
| 161 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 162 | |||||||||||
| 163 | Copyright 2009 Chas. J Owens IV, all rights reserved. | ||||||||||
| 164 | |||||||||||
| 165 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 166 | under the same terms as Perl itself. | ||||||||||
| 167 | |||||||||||
| 168 | =cut | ||||||||||
| 169 | |||||||||||
| 170 | 4 | 1; # End of autobox::dump | |||||||||