3 # a function that prints a complex variable such that the output is a
4 # valid perl representation of that variable (does not handle blessed objects)
6 # (c) 2002, Luis E. Garcia Ontanon <luis@ontanon.org>
10 # Wireshark - Network traffic analyzer
11 # By Gerald Combs <gerald@wireshark.org>
12 # Copyright 2004 Gerald Combs
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 my $_v2p_columns = 120;
34 sub var2perl { # converts a complex variable reference into perl code
44 $i .= " " for (0..$d);
49 #~ print "! $_[0] '$what'\n";
52 if ($what eq 'ARRAY') {
54 $buf .= "$i " . __v2p($d+1,$_) . ",\n" for (@{$_[0]});
58 elsif ($what eq 'HASH') {
60 $buf .= "$i " . __v2p($d+1,$_) . " =>" . __v2p($d+1,${$_[0]}{$_}) . ",\n" for (keys %{$_[0]});
64 elsif ($what eq 'SCALAR') {
65 $buf .= "\\" . __v2p($d+1,$_[0]);
67 elsif ($what eq 'REF') {
68 $buf .= "\\" . __v2p($d+1,\$_);
70 elsif ($what eq 'GLOB') {
71 $buf .= "*" . __v2p($d+1,\$_);
73 elsif ($what eq 'LVALUE') {
76 elsif ($what eq 'CODE') {
77 $buf .= 'sub { "sorry I cannot do perl code"; }';
80 $buf .= "what's '$what'?";
83 return "undef" unless defined $_[0];
84 return "''" if $_[0] eq '';
85 return "'$_[0]'" unless $_[0]=~ /^[0-9]+[\.][0-9]*?$/
87 or $_[0]=~ /^[0-9]*[\.][0-9]+?$/;
92 $buf .= "$i , " . __v2p($d+1,$_) for (@_);
94 $buf =~ s/^\( , /\( /;
98 if (length $buf < $_v2p_columns) {