PL_ prefix to all perlvars, part1
[p5sagit/p5-mst-13.2.git] / ext / Data / Dumper / Dumper.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 static SV       *freezer;
6 static SV       *toaster;
7
8 static I32 num_q _((char *s));
9 static I32 esc_q _((char *dest, char *src, STRLEN slen));
10 static SV *sv_x _((SV *sv, char *str, STRLEN len, I32 n));
11 static I32 DD_dump _((SV *val, char *name, STRLEN namelen, SV *retval,
12                       HV *seenhv, AV *postav, I32 *levelp, I32 indent,
13                       SV *pad, SV *xpad, SV *apad, SV *sep,
14                       SV *freezer, SV *toaster,
15                       I32 purity, I32 deepcopy, I32 quotekeys, SV *bless));
16
17 /* does a string need to be protected? */
18 static I32
19 needs_quote(register char *s)
20 {
21 TOP:
22     if (s[0] == ':') {
23         if (*++s) {
24             if (*s++ != ':')
25                 return 1;
26         }
27         else
28             return 1;
29     }
30     if (isIDFIRST(*s)) {
31         while (*++s)
32             if (!isALNUM(*s))
33                 if (*s == ':')
34                     goto TOP;
35                 else
36                     return 1;
37     }
38     else 
39         return 1;
40     return 0;
41 }
42
43 /* count the number of "'"s and "\"s in string */
44 static I32
45 num_q(register char *s)
46 {
47     register I32 ret = 0;
48     
49     while (*s) {
50         if (*s == '\'' || *s == '\\')
51             ++ret;
52         ++s;
53     }
54     return ret;
55 }
56
57
58 /* returns number of chars added to escape "'"s and "\"s in s */
59 /* slen number of characters in s will be escaped */
60 /* destination must be long enough for additional chars */
61 static I32
62 esc_q(register char *d, register char *s, register STRLEN slen)
63 {
64     register I32 ret = 0;
65     
66     while (slen > 0) {
67         switch (*s) {
68         case '\'':
69         case '\\':
70             *d = '\\';
71             ++d; ++ret;
72         default:
73             *d = *s;
74             ++d; ++s; --slen;
75             break;
76         }
77     }
78     return ret;
79 }
80
81 /* append a repeated string to an SV */
82 static SV *
83 sv_x(SV *sv, register char *str, STRLEN len, I32 n)
84 {
85     if (sv == Nullsv)
86         sv = newSVpv("", 0);
87     else
88         assert(SvTYPE(sv) >= SVt_PV);
89
90     if (n > 0) {
91         SvGROW(sv, len*n + SvCUR(sv) + 1);
92         if (len == 1) {
93             char *start = SvPVX(sv) + SvCUR(sv);
94             SvCUR(sv) += n;
95             start[n] = '\0';
96             while (n > 0)
97                 start[--n] = str[0];
98         }
99         else
100             while (n > 0) {
101                 sv_catpvn(sv, str, len);
102                 --n;
103             }
104     }
105     return sv;
106 }
107
108 /*
109  * This ought to be split into smaller functions. (it is one long function since
110  * it exactly parallels the perl version, which was one long thing for
111  * efficiency raisins.)  Ugggh!
112  */
113 static I32
114 DD_dump(SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
115         AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
116         SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
117         I32 deepcopy, I32 quotekeys, SV *bless)
118 {
119     char tmpbuf[128];
120     U32 i;
121     char *c, *r, *realpack, id[128];
122     SV **svp;
123     SV *sv;
124     SV *blesspad = Nullsv;
125     SV *ipad;
126     SV *ival;
127     AV *seenentry;
128     char *iname;
129     STRLEN inamelen, idlen = 0;
130     U32 flags;
131     U32 realtype;
132
133     if (!val)
134         return 0;
135
136     flags = SvFLAGS(val);
137     realtype = SvTYPE(val);
138     
139     if (SvGMAGICAL(val))
140         mg_get(val);
141     if (val == &PL_sv_undef || !SvOK(val)) {
142         sv_catpvn(retval, "undef", 5);
143         return 1;
144     }
145     if (SvROK(val)) {
146
147         if (SvOBJECT(SvRV(val)) && freezer &&
148             SvPOK(freezer) && SvCUR(freezer))
149         {
150             dSP; ENTER; SAVETMPS; PUSHMARK(sp);
151             XPUSHs(val); PUTBACK;
152             i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
153             SPAGAIN;
154             if (SvTRUE(GvSV(PL_errgv)))
155                 warn("WARNING(Freezer method call failed): %s",
156                      SvPVX(GvSV(PL_errgv)));
157             else if (i)
158                 val = newSVsv(POPs);
159             PUTBACK; FREETMPS; LEAVE;
160             if (i)
161                 (void)sv_2mortal(val);
162         }
163         
164         ival = SvRV(val);
165         flags = SvFLAGS(ival);
166         realtype = SvTYPE(ival);
167         (void) sprintf(id, "0x%lx", (unsigned long)ival);
168         idlen = strlen(id);
169         if (SvOBJECT(ival))
170             realpack = HvNAME(SvSTASH(ival));
171         else
172             realpack = Nullch;
173         if ((svp = hv_fetch(seenhv, id, idlen, FALSE)) &&
174             (sv = *svp) && SvROK(sv) &&
175             (seenentry = (AV*)SvRV(sv))) {
176             SV *othername;
177             if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)) {
178                 if (purity && *levelp > 0) {
179                     SV *postentry;
180                     
181                     if (realtype == SVt_PVHV)
182                         sv_catpvn(retval, "{}", 2);
183                     else if (realtype == SVt_PVAV)
184                         sv_catpvn(retval, "[]", 2);
185                     else
186                         sv_catpvn(retval, "''", 2);
187                     postentry = newSVpv(name, namelen);
188                     sv_catpvn(postentry, " = ", 3);
189                     sv_catsv(postentry, othername);
190                     av_push(postav, postentry);
191                 }
192                 else {
193                     if (name[0] == '@' || name[0] == '%') {
194                         if ((SvPVX(othername))[0] == '\\' &&
195                             (SvPVX(othername))[1] == name[0]) {
196                             sv_catpvn(retval, SvPVX(othername)+1, SvCUR(othername)-1);
197                         }
198                         else {
199                             sv_catpvn(retval, name, 1);
200                             sv_catpvn(retval, "{", 1);
201                             sv_catsv(retval, othername);
202                             sv_catpvn(retval, "}", 1);
203                         }
204                     }
205                     else
206                         sv_catsv(retval, othername);
207                 }
208                 return 1;
209             }
210             else {
211                 warn("ref name not found for %s", id);
212                 return 0;
213             }
214         }
215         else {   /* store our name and continue */
216             SV *namesv;
217             if (name[0] == '@' || name[0] == '%') {
218                 namesv = newSVpv("\\", 1);
219                 sv_catpvn(namesv, name, namelen);
220             }
221             else if (realtype == SVt_PVCV && name[0] == '*') {
222                 namesv = newSVpv("\\", 2);
223                 sv_catpvn(namesv, name, namelen);
224                 (SvPVX(namesv))[1] = '&';
225             }
226             else
227                 namesv = newSVpv(name, namelen);
228             seenentry = newAV();
229             av_push(seenentry, namesv);
230             (void)SvREFCNT_inc(val);
231             av_push(seenentry, val);
232             (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
233             SvREFCNT_dec(seenentry);
234         }
235         
236         (*levelp)++;
237         ipad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
238
239         if (realpack) {   /* we have a blessed ref */
240             STRLEN blesslen;
241             char *blessstr = SvPV(bless, blesslen);
242             sv_catpvn(retval, blessstr, blesslen);
243             sv_catpvn(retval, "( ", 2);
244             if (indent >= 2) {
245                 blesspad = apad;
246                 apad = newSVsv(apad);
247                 sv_x(apad, " ", 1, blesslen+2);
248             }
249         }
250
251         if (realtype <= SVt_PVBM || realtype == SVt_PVGV) {  /* scalars */
252             if (realpack && realtype != SVt_PVGV) {          /* blessed */ 
253                 sv_catpvn(retval, "do{\\(my $o = ", 13);
254                 DD_dump(ival, "", 0, retval, seenhv, postav,
255                         levelp, indent, pad, xpad, apad, sep,
256                         freezer, toaster, purity, deepcopy, quotekeys, bless);
257                 sv_catpvn(retval, ")}", 2);
258             }
259             else {
260                 sv_catpvn(retval, "\\", 1);
261                 DD_dump(ival, "", 0, retval, seenhv, postav,
262                         levelp, indent, pad, xpad, apad, sep,
263                         freezer, toaster, purity, deepcopy, quotekeys, bless);
264             }
265         }
266         else if (realtype == SVt_PVAV) {
267             SV *totpad;
268             I32 ix = 0;
269             I32 ixmax = av_len((AV *)ival);
270             
271             SV *ixsv = newSViv(0);
272             /* allowing for a 24 char wide array index */
273             New(0, iname, namelen+28, char);
274             (void)strcpy(iname, name);
275             inamelen = namelen;
276             if (name[0] == '@') {
277                 sv_catpvn(retval, "(", 1);
278                 iname[0] = '$';
279             }
280             else {
281                 sv_catpvn(retval, "[", 1);
282                 if (namelen > 0 && name[namelen-1] != ']' && name[namelen-1] != '}') {
283                     iname[inamelen++] = '-'; iname[inamelen++] = '>';
284                     iname[inamelen] = '\0';
285                 }
286             }
287             if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
288                 (instr(iname+inamelen-8, "{SCALAR}") ||
289                  instr(iname+inamelen-7, "{ARRAY}") ||
290                  instr(iname+inamelen-6, "{HASH}"))) {
291                 iname[inamelen++] = '-'; iname[inamelen++] = '>';
292             }
293             iname[inamelen++] = '['; iname[inamelen] = '\0';
294             totpad = newSVsv(sep);
295             sv_catsv(totpad, pad);
296             sv_catsv(totpad, apad);
297
298             for (ix = 0; ix <= ixmax; ++ix) {
299                 STRLEN ilen;
300                 SV *elem;
301                 svp = av_fetch((AV*)ival, ix, FALSE);
302                 if (svp)
303                     elem = *svp;
304                 else
305                     elem = &PL_sv_undef;
306                 
307                 ilen = inamelen;
308                 sv_setiv(ixsv, ix);
309                 (void) sprintf(iname+ilen, "%ld", ix);
310                 ilen = strlen(iname);
311                 iname[ilen++] = ']'; iname[ilen] = '\0';
312                 if (indent >= 3) {
313                     sv_catsv(retval, totpad);
314                     sv_catsv(retval, ipad);
315                     sv_catpvn(retval, "#", 1);
316                     sv_catsv(retval, ixsv);
317                 }
318                 sv_catsv(retval, totpad);
319                 sv_catsv(retval, ipad);
320                 DD_dump(elem, iname, ilen, retval, seenhv, postav,
321                         levelp, indent, pad, xpad, apad, sep,
322                         freezer, toaster, purity, deepcopy, quotekeys, bless);
323                 if (ix < ixmax)
324                     sv_catpvn(retval, ",", 1);
325             }
326             if (ixmax >= 0) {
327                 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
328                 sv_catsv(retval, totpad);
329                 sv_catsv(retval, opad);
330                 SvREFCNT_dec(opad);
331             }
332             if (name[0] == '@')
333                 sv_catpvn(retval, ")", 1);
334             else
335                 sv_catpvn(retval, "]", 1);
336             SvREFCNT_dec(ixsv);
337             SvREFCNT_dec(totpad);
338             Safefree(iname);
339         }
340         else if (realtype == SVt_PVHV) {
341             SV *totpad, *newapad;
342             SV *iname, *sname;
343             HE *entry;
344             char *key;
345             I32 klen;
346             SV *hval;
347             
348             iname = newSVpv(name, namelen);
349             if (name[0] == '%') {
350                 sv_catpvn(retval, "(", 1);
351                 (SvPVX(iname))[0] = '$';
352             }
353             else {
354                 sv_catpvn(retval, "{", 1);
355                 if (namelen > 0 && name[namelen-1] != ']' && name[namelen-1] != '}') {
356                     sv_catpvn(iname, "->", 2);
357                 }
358             }
359             if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
360                 (instr(name+namelen-8, "{SCALAR}") ||
361                  instr(name+namelen-7, "{ARRAY}") ||
362                  instr(name+namelen-6, "{HASH}"))) {
363                 sv_catpvn(iname, "->", 2);
364             }
365             sv_catpvn(iname, "{", 1);
366             totpad = newSVsv(sep);
367             sv_catsv(totpad, pad);
368             sv_catsv(totpad, apad);
369             
370             (void)hv_iterinit((HV*)ival);
371             i = 0;
372             while ((entry = hv_iternext((HV*)ival)))  {
373                 char *nkey;
374                 I32 nticks = 0;
375                 
376                 if (i)
377                     sv_catpvn(retval, ",", 1);
378                 i++;
379                 key = hv_iterkey(entry, &klen);
380                 hval = hv_iterval((HV*)ival, entry);
381
382                 if (quotekeys || needs_quote(key)) {
383                     nticks = num_q(key);
384                     New(0, nkey, klen+nticks+3, char);
385                     nkey[0] = '\'';
386                     if (nticks)
387                         klen += esc_q(nkey+1, key, klen);
388                     else
389                         (void)Copy(key, nkey+1, klen, char);
390                     nkey[++klen] = '\'';
391                     nkey[++klen] = '\0';
392                 }
393                 else {
394                     New(0, nkey, klen, char);
395                     (void)Copy(key, nkey, klen, char);
396                 }
397                 
398                 sname = newSVsv(iname);
399                 sv_catpvn(sname, nkey, klen);
400                 sv_catpvn(sname, "}", 1);
401
402                 sv_catsv(retval, totpad);
403                 sv_catsv(retval, ipad);
404                 sv_catpvn(retval, nkey, klen);
405                 sv_catpvn(retval, " => ", 4);
406                 if (indent >= 2) {
407                     char *extra;
408                     I32 elen = 0;
409                     newapad = newSVsv(apad);
410                     New(0, extra, klen+4+1, char);
411                     while (elen < (klen+4))
412                         extra[elen++] = ' ';
413                     extra[elen] = '\0';
414                     sv_catpvn(newapad, extra, elen);
415                     Safefree(extra);
416                 }
417                 else
418                     newapad = apad;
419
420                 DD_dump(hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
421                         postav, levelp, indent, pad, xpad, newapad, sep,
422                         freezer, toaster, purity, deepcopy, quotekeys, bless);
423                 SvREFCNT_dec(sname);
424                 Safefree(nkey);
425                 if (indent >= 2)
426                     SvREFCNT_dec(newapad);
427             }
428             if (i) {
429                 SV *opad = sv_x(Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
430                 sv_catsv(retval, totpad);
431                 sv_catsv(retval, opad);
432                 SvREFCNT_dec(opad);
433             }
434             if (name[0] == '%')
435                 sv_catpvn(retval, ")", 1);
436             else
437                 sv_catpvn(retval, "}", 1);
438             SvREFCNT_dec(iname);
439             SvREFCNT_dec(totpad);
440         }
441         else if (realtype == SVt_PVCV) {
442             sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
443             if (purity)
444                 warn("Encountered CODE ref, using dummy placeholder");
445         }
446         else {
447             warn("cannot handle ref type %ld", realtype);
448         }
449
450         if (realpack) {  /* free blessed allocs */
451             if (indent >= 2) {
452                 SvREFCNT_dec(apad);
453                 apad = blesspad;
454             }
455             sv_catpvn(retval, ", '", 3);
456             sv_catpvn(retval, realpack, strlen(realpack));
457             sv_catpvn(retval, "' )", 3);
458             if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
459                 sv_catpvn(retval, "->", 2);
460                 sv_catsv(retval, toaster);
461                 sv_catpvn(retval, "()", 2);
462             }
463         }
464         SvREFCNT_dec(ipad);
465         (*levelp)--;
466     }
467     else {
468         STRLEN i;
469         
470         if (namelen) {
471             (void) sprintf(id, "0x%lx", (unsigned long)val);
472             if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
473                 (sv = *svp) && SvROK(sv) &&
474                 (seenentry = (AV*)SvRV(sv))) {
475                 SV *othername;
476                 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)) {
477                     sv_catsv(retval, othername);
478                     return 1;
479                 }
480             }
481             else {
482                 SV *namesv;
483                 namesv = newSVpv("\\", 1);
484                 sv_catpvn(namesv, name, namelen);
485                 seenentry = newAV();
486                 av_push(seenentry, namesv);
487                 (void)SvREFCNT_inc(val);
488                 av_push(seenentry, val);
489                 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
490                 SvREFCNT_dec(seenentry);
491             }
492         }
493         
494         if (SvIOK(val)) {
495             STRLEN len;
496             i = SvIV(val);
497             (void) sprintf(tmpbuf, "%d", i);
498             len = strlen(tmpbuf);
499             sv_catpvn(retval, tmpbuf, len);
500             return 1;
501         }
502         else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
503             c = SvPV(val, i);
504             ++c; --i;                   /* just get the name */
505             if (i >= 6 && strncmp(c, "main::", 6) == 0) {
506                 c += 4;
507                 i -= 4;
508             }
509             if (needs_quote(c)) {
510                 sv_grow(retval, SvCUR(retval)+6+2*i);
511                 r = SvPVX(retval)+SvCUR(retval);
512                 r[0] = '*'; r[1] = '{'; r[2] = '\'';
513                 i += esc_q(r+3, c, i);
514                 i += 3;
515                 r[i++] = '\''; r[i++] = '}';
516                 r[i] = '\0';
517             }
518             else {
519                 sv_grow(retval, SvCUR(retval)+i+2);
520                 r = SvPVX(retval)+SvCUR(retval);
521                 r[0] = '*'; strcpy(r+1, c);
522                 i++;
523             }
524
525             if (purity) {
526                 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
527                 static STRLEN sizes[] = { 8, 7, 6 };
528                 SV *e;
529                 SV *nname = newSVpv("", 0);
530                 SV *newapad = newSVpv("", 0);
531                 GV *gv = (GV*)val;
532                 I32 j;
533                 
534                 for (j=0; j<3; j++) {
535                     e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
536                     if (e) {
537                         I32 nlevel = 0;
538                         SV *postentry = newSVpv(r,i);
539                         
540                         sv_setsv(nname, postentry);
541                         sv_catpvn(nname, entries[j], sizes[j]);
542                         sv_catpvn(postentry, " = ", 3);
543                         av_push(postav, postentry);
544                         e = newRV(e);
545                         
546                         SvCUR(newapad) = 0;
547                         if (indent >= 2)
548                             (void)sv_x(newapad, " ", 1, SvCUR(postentry));
549                         
550                         DD_dump(e, SvPVX(nname), SvCUR(nname), postentry,
551                                 seenhv, postav, &nlevel, indent, pad, xpad,
552                                 newapad, sep, freezer, toaster, purity,
553                                 deepcopy, quotekeys, bless);
554                         SvREFCNT_dec(e);
555                     }
556                 }
557                 
558                 SvREFCNT_dec(newapad);
559                 SvREFCNT_dec(nname);
560             }
561         }
562         else {
563             c = SvPV(val, i);
564             sv_grow(retval, SvCUR(retval)+3+2*i);
565             r = SvPVX(retval)+SvCUR(retval);
566             r[0] = '\'';
567             i += esc_q(r+1, c, i);
568             ++i;
569             r[i++] = '\'';
570             r[i] = '\0';
571         }
572         SvCUR_set(retval, SvCUR(retval)+i);
573     }
574
575     if (deepcopy && idlen)
576         (void)hv_delete(seenhv, id, idlen, G_DISCARD);
577         
578     return 1;
579 }
580
581
582 MODULE = Data::Dumper           PACKAGE = Data::Dumper         PREFIX = Data_Dumper_
583
584 #
585 # This is the exact equivalent of Dump.  Well, almost. The things that are
586 # different as of now (due to Laziness):
587 #   * doesnt do double-quotes yet.
588 #
589
590 void
591 Data_Dumper_Dumpxs(href, ...)
592         SV      *href;
593         PROTOTYPE: $;$$
594         PPCODE:
595         {
596             HV *hv;
597             SV *retval, *valstr;
598             HV *seenhv = Nullhv;
599             AV *postav, *todumpav, *namesav;
600             I32 level = 0;
601             I32 indent, terse, useqq, i, imax, postlen;
602             SV **svp;
603             SV *val, *name, *pad, *xpad, *apad, *sep, *tmp, *varname;
604             SV *freezer, *toaster, *bless;
605             I32 purity, deepcopy, quotekeys;
606             char tmpbuf[1024];
607             I32 gimme = GIMME;
608
609             if (!SvROK(href)) {         /* call new to get an object first */
610                 SV *valarray;
611                 SV *namearray;
612
613                 if (items == 3) {
614                     valarray = ST(1);
615                     namearray = ST(2);
616                 }
617                 else
618                     croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, NAME_ARY_REF)");
619                 
620                 ENTER;
621                 SAVETMPS;
622                 
623                 PUSHMARK(sp);
624                 XPUSHs(href);
625                 XPUSHs(sv_2mortal(newSVsv(valarray)));
626                 XPUSHs(sv_2mortal(newSVsv(namearray)));
627                 PUTBACK;
628                 i = perl_call_method("new", G_SCALAR);
629                 SPAGAIN;
630                 if (i)
631                     href = newSVsv(POPs);
632
633                 PUTBACK;
634                 FREETMPS;
635                 LEAVE;
636                 if (i)
637                     (void)sv_2mortal(href);
638             }
639
640             todumpav = namesav = Nullav;
641             seenhv = Nullhv;
642             val = pad = xpad = apad = sep = tmp = varname
643                 = freezer = toaster = bless = &PL_sv_undef;
644             name = sv_newmortal();
645             indent = 2;
646             terse = useqq = purity = deepcopy = 0;
647             quotekeys = 1;
648             
649             retval = newSVpv("", 0);
650             if (SvROK(href)
651                 && (hv = (HV*)SvRV((SV*)href))
652                 && SvTYPE(hv) == SVt_PVHV)              {
653
654                 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
655                     seenhv = (HV*)SvRV(*svp);
656                 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
657                     todumpav = (AV*)SvRV(*svp);
658                 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
659                     namesav = (AV*)SvRV(*svp);
660                 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
661                     indent = SvIV(*svp);
662                 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
663                     purity = SvIV(*svp);
664                 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
665                     terse = SvTRUE(*svp);
666                 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
667                     useqq = SvTRUE(*svp);
668                 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
669                     pad = *svp;
670                 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
671                     xpad = *svp;
672                 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
673                     apad = *svp;
674                 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
675                     sep = *svp;
676                 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
677                     varname = *svp;
678                 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
679                     freezer = *svp;
680                 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
681                     toaster = *svp;
682                 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
683                     deepcopy = SvTRUE(*svp);
684                 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
685                     quotekeys = SvTRUE(*svp);
686                 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
687                     bless = *svp;
688                 postav = newAV();
689
690                 if (todumpav)
691                     imax = av_len(todumpav);
692                 else
693                     imax = -1;
694                 valstr = newSVpv("",0);
695                 for (i = 0; i <= imax; ++i) {
696                     SV *newapad;
697                     
698                     av_clear(postav);
699                     if ((svp = av_fetch(todumpav, i, FALSE)))
700                         val = *svp;
701                     else
702                         val = &PL_sv_undef;
703                     if ((svp = av_fetch(namesav, i, TRUE)))
704                         sv_setsv(name, *svp);
705                     else
706                         SvOK_off(name);
707                     
708                     if (SvOK(name)) {
709                         if ((SvPVX(name))[0] == '*') {
710                             if (SvROK(val)) {
711                                 switch (SvTYPE(SvRV(val))) {
712                                 case SVt_PVAV:
713                                     (SvPVX(name))[0] = '@';
714                                     break;
715                                 case SVt_PVHV:
716                                     (SvPVX(name))[0] = '%';
717                                     break;
718                                 case SVt_PVCV:
719                                     (SvPVX(name))[0] = '*';
720                                     break;
721                                 default:
722                                     (SvPVX(name))[0] = '$';
723                                     break;
724                                 }
725                             }
726                             else
727                                 (SvPVX(name))[0] = '$';
728                         }
729                         else if ((SvPVX(name))[0] != '$')
730                             sv_insert(name, 0, 0, "$", 1);
731                     }
732                     else {
733                         STRLEN nchars = 0;
734                         sv_setpvn(name, "$", 1);
735                         sv_catsv(name, varname);
736                         (void) sprintf(tmpbuf, "%ld", i+1);
737                         nchars = strlen(tmpbuf);
738                         sv_catpvn(name, tmpbuf, nchars);
739                     }
740                     
741                     if (indent >= 2) {
742                         SV *tmpsv = sv_x(Nullsv, " ", 1, SvCUR(name)+3);
743                         newapad = newSVsv(apad);
744                         sv_catsv(newapad, tmpsv);
745                         SvREFCNT_dec(tmpsv);
746                     }
747                     else
748                         newapad = apad;
749                     
750                     DD_dump(val, SvPVX(name), SvCUR(name), valstr, seenhv,
751                             postav, &level, indent, pad, xpad, newapad, sep,
752                             freezer, toaster, purity, deepcopy, quotekeys,
753                             bless);
754                     
755                     if (indent >= 2)
756                         SvREFCNT_dec(newapad);
757
758                     postlen = av_len(postav);
759                     if (postlen >= 0 || !terse) {
760                         sv_insert(valstr, 0, 0, " = ", 3);
761                         sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
762                         sv_catpvn(valstr, ";", 1);
763                     }
764                     sv_catsv(retval, pad);
765                     sv_catsv(retval, valstr);
766                     sv_catsv(retval, sep);
767                     if (postlen >= 0) {
768                         I32 i;
769                         sv_catsv(retval, pad);
770                         for (i = 0; i <= postlen; ++i) {
771                             SV *elem;
772                             svp = av_fetch(postav, i, FALSE);
773                             if (svp && (elem = *svp)) {
774                                 sv_catsv(retval, elem);
775                                 if (i < postlen) {
776                                     sv_catpvn(retval, ";", 1);
777                                     sv_catsv(retval, sep);
778                                     sv_catsv(retval, pad);
779                                 }
780                             }
781                         }
782                         sv_catpvn(retval, ";", 1);
783                             sv_catsv(retval, sep);
784                     }
785                     sv_setpvn(valstr, "", 0);
786                     if (gimme == G_ARRAY) {
787                         XPUSHs(sv_2mortal(retval));
788                         if (i < imax)   /* not the last time thro ? */
789                             retval = newSVpv("",0);
790                     }
791                 }
792                 SvREFCNT_dec(postav);
793                 SvREFCNT_dec(valstr);
794             }
795             else
796                 croak("Call to new() method failed to return HASH ref");
797             if (gimme == G_SCALAR)
798                 XPUSHs(sv_2mortal(retval));
799         }