| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "Devel::Peek 3perl" |
| 58 |
.TH Devel::Peek 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
Devel::Peek \- A data debugging tool for the XS programmer |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 7 |
| 68 |
\& use Devel::Peek; |
| 69 |
\& Dump( $a ); |
| 70 |
\& Dump( $a, 5 ); |
| 71 |
\& Dump( @a ); |
| 72 |
\& Dump( %h ); |
| 73 |
\& DumpArray( 5, $a, $b, ... ); |
| 74 |
\& mstat "Point 5"; |
| 75 |
\& |
| 76 |
\& use Devel::Peek \*(Aq:opd=st\*(Aq; |
| 77 |
.Ve |
| 78 |
.SH DESCRIPTION |
| 79 |
.IX Header "DESCRIPTION" |
| 80 |
Devel::Peek contains functions which allows raw Perl datatypes to be |
| 81 |
manipulated from a Perl script. This is used by those who do XS programming |
| 82 |
to check that the data they are sending from C to Perl looks as they think |
| 83 |
it should look. The trick, then, is to know what the raw datatype is |
| 84 |
supposed to look like when it gets to Perl. This document offers some tips |
| 85 |
and hints to describe good and bad raw data. |
| 86 |
.PP |
| 87 |
It is very possible that this document will fall far short of being useful |
| 88 |
to the casual reader. The reader is expected to understand the material in |
| 89 |
the first few sections of perlguts. |
| 90 |
.PP |
| 91 |
Devel::Peek supplies a \f(CWDump()\fR function which can dump a raw Perl |
| 92 |
datatype, and \f(CWmstat("marker")\fR function to report on memory usage |
| 93 |
(if perl is compiled with corresponding option). The function |
| 94 |
\&\fBDeadCode()\fR provides statistics on the data "frozen" into inactive |
| 95 |
\&\f(CW\*(C`CV\*(C'\fR. Devel::Peek also supplies \f(CWSvREFCNT()\fR which can query reference |
| 96 |
counts on SVs. This document will take a passive, and safe, approach |
| 97 |
to data debugging and for that it will describe only the \f(CWDump()\fR |
| 98 |
function. |
| 99 |
.PP |
| 100 |
All output is to STDERR. |
| 101 |
.PP |
| 102 |
The \f(CWDump()\fR function takes one or two arguments: something to dump, and |
| 103 |
an optional limit for recursion and array elements (default is 4). The |
| 104 |
first argument is evaluated in rvalue scalar context, with exceptions for |
| 105 |
\&\f(CW@array\fR and \f(CW%hash\fR, which dump the array or hash itself. So \f(CW\*(C`Dump @array\*(C'\fR |
| 106 |
works, as does \f(CW\*(C`Dump $foo\*(C'\fR. And \f(CW\*(C`Dump pos\*(C'\fR will call \f(CW\*(C`pos\*(C'\fR in rvalue |
| 107 |
context, whereas \f(CW\*(C`Dump ${\epos}\*(C'\fR will call it in lvalue context. |
| 108 |
.PP |
| 109 |
Function \f(CWDumpArray()\fR allows dumping of multiple values (useful when you |
| 110 |
need to analyze returns of functions). |
| 111 |
.PP |
| 112 |
The global variable \f(CW$Devel::Peek::pv_limit\fR can be set to limit the |
| 113 |
number of character printed in various string values. Setting it to 0 |
| 114 |
means no limit. |
| 115 |
.PP |
| 116 |
If \f(CW\*(C`use Devel::Peek\*(C'\fR directive has a \f(CW\*(C`:opd=FLAGS\*(C'\fR argument, |
| 117 |
this switches on debugging of opcode dispatch. \f(CW\*(C`FLAGS\*(C'\fR should be a |
| 118 |
combination of \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`t\*(C'\fR, and \f(CW\*(C`P\*(C'\fR (see |
| 119 |
\&\fB\-D\fR flags in perlrun). |
| 120 |
.PP |
| 121 |
\&\f(CW\*(C`:opd\*(C'\fR is a shortcut for \f(CW\*(C`:opd=st\*(C'\fR. |
| 122 |
.SS "Runtime debugging" |
| 123 |
.IX Subsection "Runtime debugging" |
| 124 |
\&\f(CWCvGV($cv)\fR return one of the globs associated to a subroutine reference \f(CW$cv\fR. |
| 125 |
.PP |
| 126 |
\&\fBdebug_flags()\fR returns a string representation of \f(CW$^D\fR (similar to |
| 127 |
what is allowed for \fB\-D\fR flag). When called with a numeric argument, |
| 128 |
sets $^D to the corresponding value. When called with an argument of |
| 129 |
the form \f(CW"flags\-flags"\fR, set on/off bits of \f(CW$^D\fR corresponding to |
| 130 |
letters before/after \f(CW\*(C`\-\*(C'\fR. (The returned value is for \f(CW$^D\fR before |
| 131 |
the modification.) |
| 132 |
.PP |
| 133 |
\&\fBrunops_debug()\fR returns true if the current \fIopcode dispatcher\fR is the |
| 134 |
debugging one. When called with an argument, switches to debugging or |
| 135 |
non-debugging dispatcher depending on the argument (active for |
| 136 |
newly-entered subs/etc only). (The returned value is for the dispatcher before the modification.) |
| 137 |
.SS "Memory footprint debugging" |
| 138 |
.IX Subsection "Memory footprint debugging" |
| 139 |
When perl is compiled with support for memory footprint debugging |
| 140 |
(default with Perl's \fBmalloc()\fR), Devel::Peek provides an access to this API. |
| 141 |
.PP |
| 142 |
Use \fBmstat()\fR function to emit a memory state statistic to the terminal. |
| 143 |
For more information on the format of output of \fBmstat()\fR see |
| 144 |
"Using \f(CW$ENV\fR{PERL_DEBUG_MSTATS}" in perldebguts. |
| 145 |
.PP |
| 146 |
Three additional functions allow access to this statistic from Perl. |
| 147 |
First, use \f(CWmstats_fillhash(%hash)\fR to get the information contained |
| 148 |
in the output of \fBmstat()\fR into \f(CW%hash\fR. The field of this hash are |
| 149 |
.PP |
| 150 |
.Vb 3 |
| 151 |
\& minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks |
| 152 |
\& start_slack topbucket topbucket_ev topbucket_odd total total_chain |
| 153 |
\& total_sbrk totfree |
| 154 |
.Ve |
| 155 |
.PP |
| 156 |
Two additional fields \f(CW\*(C`free\*(C'\fR, \f(CW\*(C`used\*(C'\fR contain array references which |
| 157 |
provide per-bucket count of free and used chunks. Two other fields |
| 158 |
\&\f(CW\*(C`mem_size\*(C'\fR, \f(CW\*(C`available_size\*(C'\fR contain array references which provide |
| 159 |
the information about the allocated size and usable size of chunks in |
| 160 |
each bucket. Again, see "Using \f(CW$ENV\fR{PERL_DEBUG_MSTATS}" in perldebguts |
| 161 |
for details. |
| 162 |
.PP |
| 163 |
Keep in mind that only the first several "odd-numbered" buckets are |
| 164 |
used, so the information on size of the "odd-numbered" buckets which are |
| 165 |
not used is probably meaningless. |
| 166 |
.PP |
| 167 |
The information in |
| 168 |
.PP |
| 169 |
.Vb 1 |
| 170 |
\& mem_size available_size minbucket nbuckets |
| 171 |
.Ve |
| 172 |
.PP |
| 173 |
is the property of a particular build of perl, and does not depend on |
| 174 |
the current process. If you do not provide the optional argument to |
| 175 |
the functions \fBmstats_fillhash()\fR, \fBfill_mstats()\fR, \fBmstats2hash()\fR, then |
| 176 |
the information in fields \f(CW\*(C`mem_size\*(C'\fR, \f(CW\*(C`available_size\*(C'\fR is not |
| 177 |
updated. |
| 178 |
.PP |
| 179 |
\&\f(CWfill_mstats($buf)\fR is a much cheaper call (both speedwise and |
| 180 |
memory-wise) which collects the statistic into \f(CW$buf\fR in |
| 181 |
machine-readable form. At a later moment you may need to call |
| 182 |
\&\f(CW\*(C`mstats2hash($buf, %hash)\*(C'\fR to use this information to fill \f(CW%hash\fR. |
| 183 |
.PP |
| 184 |
All three APIs \f(CWfill_mstats($buf)\fR, \f(CWmstats_fillhash(%hash)\fR, and |
| 185 |
\&\f(CW\*(C`mstats2hash($buf, %hash)\*(C'\fR are designed to allocate no memory if used |
| 186 |
\&\fIthe second time\fR on the same \f(CW$buf\fR and/or \f(CW%hash\fR. |
| 187 |
.PP |
| 188 |
So, if you want to collect memory info in a cycle, you may call |
| 189 |
.PP |
| 190 |
.Vb 3 |
| 191 |
\& $#buf = 999; |
| 192 |
\& fill_mstats($_) for @buf; |
| 193 |
\& mstats_fillhash(%report, 1); # Static info too |
| 194 |
\& |
| 195 |
\& foreach (@buf) { |
| 196 |
\& # Do something... |
| 197 |
\& fill_mstats $_; # Collect statistic |
| 198 |
\& } |
| 199 |
\& foreach (@buf) { |
| 200 |
\& mstats2hash($_, %report); # Preserve static info |
| 201 |
\& # Do something with %report |
| 202 |
\& } |
| 203 |
.Ve |
| 204 |
.SH EXAMPLES |
| 205 |
.IX Header "EXAMPLES" |
| 206 |
The following examples don't attempt to show everything as that would be a |
| 207 |
monumental task, and, frankly, we don't want this manpage to be an internals |
| 208 |
document for Perl. The examples do demonstrate some basics of the raw Perl |
| 209 |
datatypes, and should suffice to get most determined people on their way. |
| 210 |
There are no guidewires or safety nets, nor blazed trails, so be prepared to |
| 211 |
travel alone from this point and on and, if at all possible, don't fall into |
| 212 |
the quicksand (it's bad for business). |
| 213 |
.PP |
| 214 |
Oh, one final bit of advice: take perlguts with you. When you return we |
| 215 |
expect to see it well-thumbed. |
| 216 |
.SS "A simple scalar string" |
| 217 |
.IX Subsection "A simple scalar string" |
| 218 |
Let's begin by looking a simple scalar which is holding a string. |
| 219 |
.PP |
| 220 |
.Vb 3 |
| 221 |
\& use Devel::Peek; |
| 222 |
\& $a = 42; $a = "hello"; |
| 223 |
\& Dump $a; |
| 224 |
.Ve |
| 225 |
.PP |
| 226 |
The output: |
| 227 |
.PP |
| 228 |
.Vb 7 |
| 229 |
\& SV = PVIV(0xbc288) at 0xbe9a8 |
| 230 |
\& REFCNT = 1 |
| 231 |
\& FLAGS = (POK,pPOK) |
| 232 |
\& IV = 42 |
| 233 |
\& PV = 0xb2048 "hello"\e0 |
| 234 |
\& CUR = 5 |
| 235 |
\& LEN = 8 |
| 236 |
.Ve |
| 237 |
.PP |
| 238 |
This says \f(CW$a\fR is an SV, a scalar. The scalar type is a PVIV, which is |
| 239 |
capable of holding an integer (IV) and/or a string (PV) value. The scalar's |
| 240 |
head is allocated at address 0xbe9a8, while the body is at 0xbc288. |
| 241 |
Its reference count is 1. It has the \f(CW\*(C`POK\*(C'\fR flag set, meaning its |
| 242 |
current PV field is valid. Because POK is set we look at the PV item |
| 243 |
to see what is in the scalar. The \e0 at the end indicate that this |
| 244 |
PV is properly NUL-terminated. |
| 245 |
Note that the IV field still contains its old numeric value, but because |
| 246 |
FLAGS doesn't have IOK set, we must ignore the IV item. |
| 247 |
CUR indicates the number of characters in the PV. LEN indicates the |
| 248 |
number of bytes allocated for the PV (at least one more than CUR, because |
| 249 |
LEN includes an extra byte for the end-of-string marker, then usually |
| 250 |
rounded up to some efficient allocation unit). |
| 251 |
.SS "A simple scalar number" |
| 252 |
.IX Subsection "A simple scalar number" |
| 253 |
If the scalar contains a number the raw SV will be leaner. |
| 254 |
.PP |
| 255 |
.Vb 3 |
| 256 |
\& use Devel::Peek; |
| 257 |
\& $a = 42; |
| 258 |
\& Dump $a; |
| 259 |
.Ve |
| 260 |
.PP |
| 261 |
The output: |
| 262 |
.PP |
| 263 |
.Vb 4 |
| 264 |
\& SV = IV(0xbc818) at 0xbe9a8 |
| 265 |
\& REFCNT = 1 |
| 266 |
\& FLAGS = (IOK,pIOK) |
| 267 |
\& IV = 42 |
| 268 |
.Ve |
| 269 |
.PP |
| 270 |
This says \f(CW$a\fR is an SV, a scalar. The scalar is an IV, a number. Its |
| 271 |
reference count is 1. It has the \f(CW\*(C`IOK\*(C'\fR flag set, meaning it is currently |
| 272 |
being evaluated as a number. Because IOK is set we look at the IV item to |
| 273 |
see what is in the scalar. |
| 274 |
.SS "A simple scalar with an extra reference" |
| 275 |
.IX Subsection "A simple scalar with an extra reference" |
| 276 |
If the scalar from the previous example had an extra reference: |
| 277 |
.PP |
| 278 |
.Vb 4 |
| 279 |
\& use Devel::Peek; |
| 280 |
\& $a = 42; |
| 281 |
\& $b = \e$a; |
| 282 |
\& Dump $a; |
| 283 |
.Ve |
| 284 |
.PP |
| 285 |
The output: |
| 286 |
.PP |
| 287 |
.Vb 4 |
| 288 |
\& SV = IV(0xbe860) at 0xbe9a8 |
| 289 |
\& REFCNT = 2 |
| 290 |
\& FLAGS = (IOK,pIOK) |
| 291 |
\& IV = 42 |
| 292 |
.Ve |
| 293 |
.PP |
| 294 |
Notice that this example differs from the previous example only in its |
| 295 |
reference count. Compare this to the next example, where we dump \f(CW$b\fR |
| 296 |
instead of \f(CW$a\fR. |
| 297 |
.SS "A reference to a simple scalar" |
| 298 |
.IX Subsection "A reference to a simple scalar" |
| 299 |
This shows what a reference looks like when it references a simple scalar. |
| 300 |
.PP |
| 301 |
.Vb 4 |
| 302 |
\& use Devel::Peek; |
| 303 |
\& $a = 42; |
| 304 |
\& $b = \e$a; |
| 305 |
\& Dump $b; |
| 306 |
.Ve |
| 307 |
.PP |
| 308 |
The output: |
| 309 |
.PP |
| 310 |
.Vb 8 |
| 311 |
\& SV = IV(0xf041c) at 0xbe9a0 |
| 312 |
\& REFCNT = 1 |
| 313 |
\& FLAGS = (ROK) |
| 314 |
\& RV = 0xbab08 |
| 315 |
\& SV = IV(0xbe860) at 0xbe9a8 |
| 316 |
\& REFCNT = 2 |
| 317 |
\& FLAGS = (IOK,pIOK) |
| 318 |
\& IV = 42 |
| 319 |
.Ve |
| 320 |
.PP |
| 321 |
Starting from the top, this says \f(CW$b\fR is an SV. The scalar is an IV, |
| 322 |
which is capable of holding an integer or reference value. |
| 323 |
It has the \f(CW\*(C`ROK\*(C'\fR flag set, meaning it is a reference (rather than an |
| 324 |
integer or string). Notice that Dump |
| 325 |
follows the reference and shows us what \f(CW$b\fR was referencing. We see the |
| 326 |
same \f(CW$a\fR that we found in the previous example. |
| 327 |
.PP |
| 328 |
Note that the value of \f(CW\*(C`RV\*(C'\fR coincides with the numbers we see when we |
| 329 |
stringify \f(CW$b\fR. The addresses inside \fBIV()\fR are addresses of |
| 330 |
\&\f(CW\*(C`X***\*(C'\fR structures which hold the current state of an \f(CW\*(C`SV\*(C'\fR. This |
| 331 |
address may change during lifetime of an SV. |
| 332 |
.SS "A reference to an array" |
| 333 |
.IX Subsection "A reference to an array" |
| 334 |
This shows what a reference to an array looks like. |
| 335 |
.PP |
| 336 |
.Vb 3 |
| 337 |
\& use Devel::Peek; |
| 338 |
\& $a = [42]; |
| 339 |
\& Dump $a; |
| 340 |
.Ve |
| 341 |
.PP |
| 342 |
The output: |
| 343 |
.PP |
| 344 |
.Vb 10 |
| 345 |
\& SV = IV(0xc85998) at 0xc859a8 |
| 346 |
\& REFCNT = 1 |
| 347 |
\& FLAGS = (ROK) |
| 348 |
\& RV = 0xc70de8 |
| 349 |
\& SV = PVAV(0xc71e10) at 0xc70de8 |
| 350 |
\& REFCNT = 1 |
| 351 |
\& FLAGS = () |
| 352 |
\& ARRAY = 0xc7e820 |
| 353 |
\& FILL = 0 |
| 354 |
\& MAX = 0 |
| 355 |
\& FLAGS = (REAL) |
| 356 |
\& Elt No. 0 |
| 357 |
\& SV = IV(0xc70f88) at 0xc70f98 |
| 358 |
\& REFCNT = 1 |
| 359 |
\& FLAGS = (IOK,pIOK) |
| 360 |
\& IV = 42 |
| 361 |
.Ve |
| 362 |
.PP |
| 363 |
This says \f(CW$a\fR is a reference (ROK), which points to |
| 364 |
another SV which is a PVAV, an array. The array has one element, |
| 365 |
element zero, which is another SV. The field \f(CW\*(C`FILL\*(C'\fR above indicates |
| 366 |
the last element in the array, similar to \f(CW\*(C`$#$a\*(C'\fR. |
| 367 |
.PP |
| 368 |
If \f(CW$a\fR pointed to an array of two elements then we would see the |
| 369 |
following. |
| 370 |
.PP |
| 371 |
.Vb 3 |
| 372 |
\& use Devel::Peek \*(AqDump\*(Aq; |
| 373 |
\& $a = [42,24]; |
| 374 |
\& Dump $a; |
| 375 |
.Ve |
| 376 |
.PP |
| 377 |
The output: |
| 378 |
.PP |
| 379 |
.Vb 10 |
| 380 |
\& SV = IV(0x158c998) at 0x158c9a8 |
| 381 |
\& REFCNT = 1 |
| 382 |
\& FLAGS = (ROK) |
| 383 |
\& RV = 0x1577de8 |
| 384 |
\& SV = PVAV(0x1578e10) at 0x1577de8 |
| 385 |
\& REFCNT = 1 |
| 386 |
\& FLAGS = () |
| 387 |
\& ARRAY = 0x1585820 |
| 388 |
\& FILL = 1 |
| 389 |
\& MAX = 1 |
| 390 |
\& FLAGS = (REAL) |
| 391 |
\& Elt No. 0 |
| 392 |
\& SV = IV(0x1577f88) at 0x1577f98 |
| 393 |
\& REFCNT = 1 |
| 394 |
\& FLAGS = (IOK,pIOK) |
| 395 |
\& IV = 42 |
| 396 |
\& Elt No. 1 |
| 397 |
\& SV = IV(0x158be88) at 0x158be98 |
| 398 |
\& REFCNT = 1 |
| 399 |
\& FLAGS = (IOK,pIOK) |
| 400 |
\& IV = 24 |
| 401 |
.Ve |
| 402 |
.PP |
| 403 |
Note that \f(CW\*(C`Dump\*(C'\fR will not report \fIall\fR the elements in the array, |
| 404 |
only several first (depending on how deep it already went into the |
| 405 |
report tree). |
| 406 |
.SS "A reference to a hash" |
| 407 |
.IX Subsection "A reference to a hash" |
| 408 |
The following shows the raw form of a reference to a hash. |
| 409 |
.PP |
| 410 |
.Vb 3 |
| 411 |
\& use Devel::Peek; |
| 412 |
\& $a = {hello=>42}; |
| 413 |
\& Dump $a; |
| 414 |
.Ve |
| 415 |
.PP |
| 416 |
The output: |
| 417 |
.PP |
| 418 |
.Vb 10 |
| 419 |
\& SV = IV(0x55cb50b50fb0) at 0x55cb50b50fc0 |
| 420 |
\& REFCNT = 1 |
| 421 |
\& FLAGS = (ROK) |
| 422 |
\& RV = 0x55cb50b2b758 |
| 423 |
\& SV = PVHV(0x55cb50b319c0) at 0x55cb50b2b758 |
| 424 |
\& REFCNT = 1 |
| 425 |
\& FLAGS = (SHAREKEYS) |
| 426 |
\& ARRAY = 0x55cb50b941a0 (0:7, 1:1) |
| 427 |
\& hash quality = 100.0% |
| 428 |
\& KEYS = 1 |
| 429 |
\& FILL = 1 |
| 430 |
\& MAX = 7 |
| 431 |
\& Elt "hello" HASH = 0x3128ece4 |
| 432 |
\& SV = IV(0x55cb50b464f8) at 0x55cb50b46508 |
| 433 |
\& REFCNT = 1 |
| 434 |
\& FLAGS = (IOK,pIOK) |
| 435 |
\& IV = 42 |
| 436 |
.Ve |
| 437 |
.PP |
| 438 |
This shows \f(CW$a\fR is a reference pointing to an SV. That SV is a PVHV, a hash. |
| 439 |
.PP |
| 440 |
The "quality" of a hash is defined as the total number of comparisons needed |
| 441 |
to access every element once, relative to the expected number needed for a |
| 442 |
random hash. The value can go over 100%. |
| 443 |
.PP |
| 444 |
The total number of comparisons is equal to the sum of the squares of the |
| 445 |
number of entries in each bucket. For a random hash of \f(CW\*(C`<n\*(C'\fR> keys into |
| 446 |
\&\f(CW\*(C`<k\*(C'\fR> buckets, the expected value is: |
| 447 |
.PP |
| 448 |
.Vb 1 |
| 449 |
\& n + n(n\-1)/2k |
| 450 |
.Ve |
| 451 |
.SS "Dumping a large array or hash" |
| 452 |
.IX Subsection "Dumping a large array or hash" |
| 453 |
The \f(CWDump()\fR function, by default, dumps up to 4 elements from a |
| 454 |
toplevel array or hash. This number can be increased by supplying a |
| 455 |
second argument to the function. |
| 456 |
.PP |
| 457 |
.Vb 3 |
| 458 |
\& use Devel::Peek; |
| 459 |
\& $a = [10,11,12,13,14]; |
| 460 |
\& Dump $a; |
| 461 |
.Ve |
| 462 |
.PP |
| 463 |
Notice that \f(CWDump()\fR prints only elements 10 through 13 in the above code. |
| 464 |
The following code will print all of the elements. |
| 465 |
.PP |
| 466 |
.Vb 3 |
| 467 |
\& use Devel::Peek \*(AqDump\*(Aq; |
| 468 |
\& $a = [10,11,12,13,14]; |
| 469 |
\& Dump $a, 5; |
| 470 |
.Ve |
| 471 |
.SS "A reference to an SV which holds a C pointer" |
| 472 |
.IX Subsection "A reference to an SV which holds a C pointer" |
| 473 |
This is what you really need to know as an XS programmer, of course. When |
| 474 |
an XSUB returns a pointer to a C structure that pointer is stored in an SV |
| 475 |
and a reference to that SV is placed on the XSUB stack. So the output from |
| 476 |
an XSUB which uses something like the T_PTROBJ map might look something like |
| 477 |
this: |
| 478 |
.PP |
| 479 |
.Vb 11 |
| 480 |
\& SV = IV(0xf381c) at 0xc859a8 |
| 481 |
\& REFCNT = 1 |
| 482 |
\& FLAGS = (ROK) |
| 483 |
\& RV = 0xb8ad8 |
| 484 |
\& SV = PVMG(0xbb3c8) at 0xc859a0 |
| 485 |
\& REFCNT = 1 |
| 486 |
\& FLAGS = (OBJECT,IOK,pIOK) |
| 487 |
\& IV = 729160 |
| 488 |
\& NV = 0 |
| 489 |
\& PV = 0 |
| 490 |
\& STASH = 0xc1d10 "CookBookB::Opaque" |
| 491 |
.Ve |
| 492 |
.PP |
| 493 |
This shows that we have an SV which is a reference, which points at another |
| 494 |
SV. In this case that second SV is a PVMG, a blessed scalar. Because it is |
| 495 |
blessed it has the \f(CW\*(C`OBJECT\*(C'\fR flag set. Note that an SV which holds a C |
| 496 |
pointer also has the \f(CW\*(C`IOK\*(C'\fR flag set. The \f(CW\*(C`STASH\*(C'\fR is set to the package |
| 497 |
name which this SV was blessed into. |
| 498 |
.PP |
| 499 |
The output from an XSUB which uses something like the T_PTRREF map, which |
| 500 |
doesn't bless the object, might look something like this: |
| 501 |
.PP |
| 502 |
.Vb 10 |
| 503 |
\& SV = IV(0xf381c) at 0xc859a8 |
| 504 |
\& REFCNT = 1 |
| 505 |
\& FLAGS = (ROK) |
| 506 |
\& RV = 0xb8ad8 |
| 507 |
\& SV = PVMG(0xbb3c8) at 0xc859a0 |
| 508 |
\& REFCNT = 1 |
| 509 |
\& FLAGS = (IOK,pIOK) |
| 510 |
\& IV = 729160 |
| 511 |
\& NV = 0 |
| 512 |
\& PV = 0 |
| 513 |
.Ve |
| 514 |
.SS "A reference to a subroutine" |
| 515 |
.IX Subsection "A reference to a subroutine" |
| 516 |
Looks like this: |
| 517 |
.PP |
| 518 |
.Vb 10 |
| 519 |
\& SV = IV(0x24d2dd8) at 0x24d2de8 |
| 520 |
\& REFCNT = 1 |
| 521 |
\& FLAGS = (TEMP,ROK) |
| 522 |
\& RV = 0x24e79d8 |
| 523 |
\& SV = PVCV(0x24e5798) at 0x24e79d8 |
| 524 |
\& REFCNT = 2 |
| 525 |
\& FLAGS = () |
| 526 |
\& COMP_STASH = 0x22c9c50 "main" |
| 527 |
\& START = 0x22eed60 ===> 0 |
| 528 |
\& ROOT = 0x22ee490 |
| 529 |
\& GVGV::GV = 0x22de9d8 "MY" :: "top_targets" |
| 530 |
\& FILE = "(eval 5)" |
| 531 |
\& DEPTH = 0 |
| 532 |
\& FLAGS = 0x0 |
| 533 |
\& OUTSIDE_SEQ = 93 |
| 534 |
\& PADLIST = 0x22e9ed8 |
| 535 |
\& PADNAME = 0x22e9ec0(0x22eed00) PAD = 0x22e9ea8(0x22eecd0) |
| 536 |
\& OUTSIDE = 0x22c9fb0 (MAIN) |
| 537 |
.Ve |
| 538 |
.PP |
| 539 |
This shows that |
| 540 |
.IP \(bu 4 |
| 541 |
the subroutine is not an XSUB (since \f(CW\*(C`START\*(C'\fR and \f(CW\*(C`ROOT\*(C'\fR are |
| 542 |
non-zero, and \f(CW\*(C`XSUB\*(C'\fR is not listed, and is thus null); |
| 543 |
.IP \(bu 4 |
| 544 |
that it was compiled in the package \f(CW\*(C`main\*(C'\fR; |
| 545 |
.IP \(bu 4 |
| 546 |
under the name \f(CW\*(C`MY::top_targets\*(C'\fR; |
| 547 |
.IP \(bu 4 |
| 548 |
inside a 5th eval in the program; |
| 549 |
.IP \(bu 4 |
| 550 |
it is not currently executed (because \f(CW\*(C`DEPTH\*(C'\fR is 0); |
| 551 |
.IP \(bu 4 |
| 552 |
it has no prototype (\f(CW\*(C`PROTOTYPE\*(C'\fR field is missing). |
| 553 |
.SH EXPORTS |
| 554 |
.IX Header "EXPORTS" |
| 555 |
\&\f(CW\*(C`Dump\*(C'\fR, \f(CW\*(C`mstat\*(C'\fR, \f(CW\*(C`DeadCode\*(C'\fR, \f(CW\*(C`DumpArray\*(C'\fR, \f(CW\*(C`DumpWithOP\*(C'\fR and |
| 556 |
\&\f(CW\*(C`DumpProg\*(C'\fR, \f(CW\*(C`fill_mstats\*(C'\fR, \f(CW\*(C`mstats_fillhash\*(C'\fR, \f(CW\*(C`mstats2hash\*(C'\fR by |
| 557 |
default. Additionally available \f(CW\*(C`SvREFCNT\*(C'\fR, \f(CW\*(C`SvREFCNT_inc\*(C'\fR and |
| 558 |
\&\f(CW\*(C`SvREFCNT_dec\*(C'\fR. |
| 559 |
.SH BUGS |
| 560 |
.IX Header "BUGS" |
| 561 |
Readers have been known to skip important parts of perlguts, causing much |
| 562 |
frustration for all. |
| 563 |
.SH AUTHOR |
| 564 |
.IX Header "AUTHOR" |
| 565 |
Ilya Zakharevich [email protected]\-state.edu |
| 566 |
.PP |
| 567 |
Copyright (c) 1995\-98 Ilya Zakharevich. All rights reserved. |
| 568 |
This program is free software; you can redistribute it and/or |
| 569 |
modify it under the same terms as Perl itself. |
| 570 |
.PP |
| 571 |
Author of this software makes no claim whatsoever about suitability, |
| 572 |
reliability, edability, editability or usability of this product, and |
| 573 |
should not be kept liable for any damage resulting from the use of |
| 574 |
it. If you can use it, you are in luck, if not, I should not be kept |
| 575 |
responsible. Keep a handy copy of your backup tape at hand. |
| 576 |
.SH "SEE ALSO" |
| 577 |
.IX Header "SEE ALSO" |
| 578 |
perlguts, and perlguts, again. |