| 1 |
#BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net) |
| 2 |
#BSD All rights reserved. |
| 3 |
#BSD |
| 4 |
#BSD Redistribution and use in source and binary forms, with or without |
| 5 |
#BSD modification, are permitted provided that the following conditions |
| 6 |
#BSD are met: |
| 7 |
#BSD |
| 8 |
#BSD - Redistributions of source code must retain the above copyright |
| 9 |
#BSD notice, this list of conditions and the following disclaimer. |
| 10 |
#BSD |
| 11 |
#BSD - Redistributions in binary form must reproduce the above copyright |
| 12 |
#BSD notice, this list of conditions and the following disclaimer in the |
| 13 |
#BSD documentation and/or other materials provided with the distribution. |
| 14 |
#BSD |
| 15 |
#BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 |
#BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 |
#BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 |
#BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 |
#BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 |
#BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 |
#BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 |
#BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 |
#BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 |
#BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
#BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
#BSD POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
######################################################################## |
| 29 |
|
| 30 |
=item B<dump> |
| 31 |
|
| 32 |
Params: $name, \@array [, $name, \%hash, $name, \$scalar ] |
| 33 |
|
| 34 |
Return: $code [ undef on error ] |
| 35 |
|
| 36 |
The dump function will take the given $name and data reference, and |
| 37 |
will create an ASCII perl code representation suitable for eval'ing |
| 38 |
later to recreate the same structure. $name is the name of the variable |
| 39 |
that it will be saved as. Example: |
| 40 |
|
| 41 |
$output = LW2::dump('request',\%request); |
| 42 |
|
| 43 |
NOTE: dump() creates anonymous structures under the name given. For |
| 44 |
example, if you dump the hash %hin under the name 'hin', then when you |
| 45 |
eval the dumped code you will need to use %$hin, since $hin is now a |
| 46 |
*reference* to a hash. |
| 47 |
|
| 48 |
=cut |
| 49 |
|
| 50 |
sub dump { |
| 51 |
my %what = @_; |
| 52 |
my ( $final, $k, $v ) = (''); |
| 53 |
while ( ( $k, $v ) = each %what ) { |
| 54 |
return undef if ( ref($k) || !ref($v) ); |
| 55 |
$final .= "\$$k = " . _dump( 1, $v, 1 ); |
| 56 |
$final =~ s#,\n$##; |
| 57 |
$final .= ";\n"; |
| 58 |
} |
| 59 |
return $final; |
| 60 |
} |
| 61 |
|
| 62 |
######################################################################## |
| 63 |
|
| 64 |
=item B<dump_writefile> |
| 65 |
|
| 66 |
Params: $file, $name, \@array [, $name, \%hash, $name, \@scalar ] |
| 67 |
|
| 68 |
Return: 0 if success; 1 if error |
| 69 |
|
| 70 |
This calls dump() and saves the output to the specified $file. |
| 71 |
|
| 72 |
Note: LW does not checking on the validity of the file name, it's |
| 73 |
creation, or anything of the sort. Files are opened in overwrite |
| 74 |
mode. |
| 75 |
|
| 76 |
=cut |
| 77 |
|
| 78 |
sub dump_writefile { |
| 79 |
my $file = shift; |
| 80 |
my $output = &dump(@_); |
| 81 |
return 1 if ( !open( OUT, ">$file" ) || !defined $output ); |
| 82 |
binmode(OUT); |
| 83 |
print OUT $output; |
| 84 |
close(OUT); |
| 85 |
} |
| 86 |
|
| 87 |
######################################################################## |
| 88 |
|
| 89 |
sub _dump { # dereference and dump an element |
| 90 |
my ( $t, $ref, $depth ) = @_; |
| 91 |
my ( $out, $k, $v ) = (''); |
| 92 |
$depth ||= 1; |
| 93 |
|
| 94 |
# to protect against circular loops |
| 95 |
return 'undef' if ( $depth > 128 ); |
| 96 |
|
| 97 |
if ( !defined $ref ) { |
| 98 |
return 'undef'; |
| 99 |
} |
| 100 |
elsif ( ref($ref) eq 'HASH' ) { |
| 101 |
$out .= "{\n"; |
| 102 |
while ( ( $k, $v ) = each %$ref ) { |
| 103 |
# next if ( $k eq '' ); |
| 104 |
$out .= "\t" x $t; |
| 105 |
$out .= _dumpd($k) . ' => '; |
| 106 |
if ( ref($v) ) { $out .= _dump( $t + 1, $v, $depth + 1 ); } |
| 107 |
else { $out .= _dumpd($v); } |
| 108 |
$out .= ",\n" unless ( substr( $out, -2, 2 ) eq ",\n" ); |
| 109 |
} |
| 110 |
$out =~ s#,\n$#\n#; |
| 111 |
$out .= "\t" x ( $t - 1 ); |
| 112 |
$out .= "},\n"; |
| 113 |
} |
| 114 |
elsif ( ref($ref) eq 'ARRAY' ) { |
| 115 |
$out .= "["; |
| 116 |
if ( ~~@$ref ) { |
| 117 |
$out .= "\n"; |
| 118 |
foreach $v (@$ref) { |
| 119 |
$out .= "\t" x $t; |
| 120 |
if ( ref($v) ) { $out .= _dump( $t + 1, $v, $depth + 1 ); } |
| 121 |
else { $out .= _dumpd($v); } |
| 122 |
$out .= ",\n" unless ( substr( $out, -2, 2 ) eq ",\n" ); |
| 123 |
} |
| 124 |
$out =~ s#,\n$#\n#; |
| 125 |
$out .= "\t" x ( $t - 1 ); |
| 126 |
} |
| 127 |
$out .= "],\n"; |
| 128 |
} |
| 129 |
elsif ( ref($ref) eq 'SCALAR' ) { |
| 130 |
$out .= _dumpd($$ref); |
| 131 |
} |
| 132 |
elsif ( ref($ref) eq 'REF' ) { |
| 133 |
$out .= _dump( $t, $$ref, $depth + 1 ); |
| 134 |
} |
| 135 |
elsif ( ref($ref) ) { # unknown/unsupported ref |
| 136 |
$out .= "undef"; |
| 137 |
} |
| 138 |
else { # normal scalar |
| 139 |
$out .= _dumpd($ref); |
| 140 |
} |
| 141 |
return $out; |
| 142 |
} |
| 143 |
|
| 144 |
######################################################################## |
| 145 |
|
| 146 |
sub _dumpd { # escape a scalar string |
| 147 |
my $v = shift; |
| 148 |
return 'undef' if ( !defined $v ); |
| 149 |
return "''" if ( $v eq '' ); |
| 150 |
return "$v" if ( $v eq '0' || $v !~ tr/0-9//c && $v !~ m#^0+# ); |
| 151 |
if ( $v !~ tr/ !-~//c ) { |
| 152 |
$v =~ s/(['\\])/\\$1/g; |
| 153 |
return "'$v'"; |
| 154 |
} |
| 155 |
$v =~ s#\\#\\\\#g; |
| 156 |
$v =~ s#"#\\"#g; |
| 157 |
$v =~ s#\r#\\r#g; |
| 158 |
$v =~ s#\n#\\n#g; |
| 159 |
$v =~ s#\t#\\t#g; |
| 160 |
$v =~ s#\$#\\\$#g; |
| 161 |
$v =~ s#([^!-~ ])#sprintf('\\x%02x',ord($1))#eg; |
| 162 |
return "\"$v\""; |
| 163 |
} |
| 164 |
|
| 165 |
######################################################################## |
| 166 |
|