@node Devel/Peek, Devel/SelfStubber, Devel/DProf, Module List
@unnumbered A data debugging tool for the XS programmer


@unnumberedsec NAME

Devel::Peek - A data debugging tool for the XS programmer

@unnumberedsec SYNOPSIS

@example
use Devel::Peek @'Dump@';
Dump( $a );
Dump( $a, 5 );
	Devel::Peek::mstat "Point 5";
@end example

@unnumberedsec DESCRIPTION

Devel::Peek contains functions which allows raw Perl datatypes to be
manipulated from a Perl script.  This is used by those who do XS programming
to check that the data they are sending from C to Perl looks as they think
it should look.  The trick, then, is to know what the raw datatype is
supposed to look like when it gets to Perl.  This document offers some tips
and hints to describe good and bad raw data.

It is very possible that this document will fall far short of being useful
to the casual reader.  The reader is expected to understand the material in
the first few sections of @xref{(perl.info)perlguts,Perlguts},.

Devel::Peek supplies a @code{Dump()} function which can dump a raw Perl
datatype, and @code{mstat("marker")} function to report on memory usage
(if perl is compiled with corresponding option).  It also supplies
@code{SvREFCNT()}, @code{SvREFCNT_inc()}, and @code{SvREFCNT_dec()} which can
query, increment, and decrement reference counts on SVs.  This
document will take a passive, and safe, approach to data debugging and
for that it will describe only the @code{Dump()} function.

@unnumberedsec EXAMPLES

The following examples don@'t attempt to show everything as that would be a
monumental task, and, frankly, we don@'t want this manpage to be an internals
document for Perl.  The examples do demonstrate some basics of the raw Perl
datatypes, and should suffice to get most determined people on their way.
There are no guidewires or safety nets, nor blazed trails, so be prepared to
travel alone from this point and on and, if at all possible, don@'t fall into
the quicksand (it@'s bad for business).

Oh, one final bit of advice: take @xref{(perl.info)perlguts,Perlguts}, with you.  When you return we
expect to see it well-thumbed.

@unnumberedsubsec A simple scalar string

Let@'s begin by looking a simple scalar which is holding a string.

@example
use Devel::Peek @'Dump@';
$a = "hello";
Dump $a;
@end example

The output:

@example
SV = PVIV(0xbc288)
  REFCNT = 1
  FLAGS = (POK,pPOK)
  IV = 0
  PV = 0xb2048 "hello"
  CUR = 5
  LEN = 6
@end example

This says @code{$a} is an SV, a scalar.  The scalar is a PVIV, a string.
Its reference count is 1.  It has the @code{POK} flag set, meaning its
current PV field is valid.  Because POK is set we look at the PV item
to see what is in the scalar.  If the FLAGS had been IOK we would look
at the IV item.  CUR indicates the number of characters in the PV.
LEN indicates the number of bytes requested for the PV (one more than
CUR, in this case, because LEN includes an extra byte for the
end-of-string marker).

@unnumberedsubsec A simple scalar number

If the scalar contains a number the raw SV will be leaner.

@example
use Devel::Peek @'Dump@';
$a = 42;
Dump $a;
@end example

The output:

@example
SV = IV(0xbc818)
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 42
@end example

This says @code{$a} is an SV, a scalar.  The scalar is an IV, a number.  Its
reference count is 1.  It has the @code{IOK} flag set, meaning it is currently
being evaluated as a number.  Because IOK is set we look at the IV item to
see what is in the scalar.

@unnumberedsubsec A simple scalar with an extra reference

If the scalar from the previous example had an extra reference:

@example
use Devel::Peek @'Dump@';
$a = 42;
$b = \$a;
Dump $a;
@end example

The output:

@example
SV = IV(0xbe860)
  REFCNT = 2
  FLAGS = (IOK,pIOK)
  IV = 42
@end example

Notice that this example differs from the previous example only in its
reference count.  Compare this to the next example, where we dump @code{$b}
instead of @code{$a}.

@unnumberedsubsec A reference to a simple scalar

This shows what a reference looks like when it references a simple scalar.

@example
use Devel::Peek @'Dump@';
$a = 42;
$b = \$a;
Dump $b;
@end example

The output:

@example
SV = RV(0xf041c)
  REFCNT = 1
  FLAGS = (ROK)
  RV = 0xbab08
SV = IV(0xbe860)
  REFCNT = 2
  FLAGS = (IOK,pIOK)
  IV = 42
@end example

Starting from the top, this says @code{$b} is an SV.  The scalar is an RV, a
reference.  It has the @code{ROK} flag set, meaning it is a reference.  Because
ROK is set we have an RV item rather than an IV or PV.  Notice that Dump
follows the reference and shows us what @code{$b} was referencing.  We see the
same @code{$a} that we found in the previous example.

@unnumberedsubsec A reference to an array

This shows what a reference to an array looks like.

@example
use Devel::Peek @'Dump@';
$a = [42];
Dump $a;
@end example

The output:

@example
SV = RV(0xf041c)
  REFCNT = 1
  FLAGS = (ROK)
  RV = 0xb2850
SV = PVAV(0xbd448)
  REFCNT = 1
  FLAGS = ()
  IV = 0
  NV = 0
  ARRAY = 0xb2048
  ALLOC = 0xb2048
  FILL = 0
  MAX = 0
  ARYLEN = 0x0
  FLAGS = (REAL)
Elt No. 0
SV = IV(0xbe860)
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 42
@end example

This says @code{$a} is an SV and that it is an RV.  That RV points to
another SV which is a PVAV, an array.  The array has one element,
element zero, which is another SV. The field @code{FILL} above indicates
the last element in the array, similar to @code{$#$a}.

If @code{$a} pointed to an array of two elements then we would see the
following.

@example
use Devel::Peek @'Dump@';
$a = [42,24];
Dump $a;
@end example

The output:

@example
SV = RV(0xf041c)
  REFCNT = 1
  FLAGS = (ROK)
  RV = 0xb2850
SV = PVAV(0xbd448)
  REFCNT = 1
  FLAGS = ()
  IV = 0
  NV = 0
  ARRAY = 0xb2048
  ALLOC = 0xb2048
  FILL = 0
  MAX = 0
  ARYLEN = 0x0
  FLAGS = (REAL)
Elt No. 0
SV = IV(0xbe860)
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 42
Elt No. 1
SV = IV(0xbe818)
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 24
@end example

Note that @code{Dump} will not report all the elements in the array,
only several first (depending on how deep it already went into the
report tree).

@unnumberedsubsec A reference to a hash

The following shows the raw form of a reference to a hash.

@example
use Devel::Peek @'Dump@';
$a = @{hello=>42@};
Dump $a;
@end example

The output:

@example
SV = RV(0xf041c)
  REFCNT = 1
  FLAGS = (ROK)
  RV = 0xb2850
SV = PVHV(0xbd448)
  REFCNT = 1
  FLAGS = ()
  IV = 1
  NV = 0
  ARRAY = 0xbd748
  KEYS = 1
  FILL = 1
  MAX = 7
  RITER = -1
  EITER = 0x0
Elt "hello" => 0xbaaf0
SV = IV(0xbe860)
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 42
@end example

This shows @code{$a} is a reference pointing to an SV.  That SV is a PVHV, a
hash.

@unnumberedsubsec Dumping a large array or hash

The @code{Dump()} function, by default, dumps up to 4 elements from a
toplevel array or hash.  This number can be increased by supplying a
second argument to the function.

@example
use Devel::Peek @'Dump@';
$a = [10,11,12,13,14];
Dump $a;
@end example

Notice that @code{Dump()} prints only elements 10 through 13 in the above code.
The following code will print all of the elements.

@example
use Devel::Peek @'Dump@';
$a = [10,11,12,13,14];
Dump $a, 5;
@end example

@unnumberedsubsec A reference to an SV which holds a C pointer

This is what you really need to know as an XS programmer, of course.  When
an XSUB returns a pointer to a C structure that pointer is stored in an SV
and a reference to that SV is placed on the XSUB stack.  So the output from
an XSUB which uses something like the T_PTROBJ map might look something like
this:

@example
SV = RV(0xf381c)
  REFCNT = 1
  FLAGS = (ROK)
  RV = 0xb8ad8
SV = PVMG(0xbb3c8)
  REFCNT = 1
  FLAGS = (OBJECT,IOK,pIOK)
  IV = 729160
  NV = 0
  PV = 0
  STASH = 0xc1d10       "CookBookB::Opaque"
@end example

This shows that we have an SV which is an RV.  That RV points at another
SV.  In this case that second SV is a PVMG, a blessed scalar.  Because it is
blessed it has the OBJECT flag set.  Note that an SV which holds a C
pointer also has the @code{IOK} flag set.  The @code{STASH} is set to the package
name which this SV was blessed into.

The output from an XSUB which uses something like the T_PTRREF map, which
doesn@'t bless the object, might look something like this:

@example
SV = RV(0xf381c)
  REFCNT = 1
  FLAGS = (ROK)
  RV = 0xb8ad8
SV = PVMG(0xbb3c8)
  REFCNT = 1
  FLAGS = (IOK,pIOK)
  IV = 729160
  NV = 0
  PV = 0
@end example

@unnumberedsec BUGS

Readers have been known to skip important parts of @xref{(perl.info)perlguts,Perlguts},, causing much
frustration for all.

@unnumberedsec SEE ALSO

@xref{(perl.info)perlguts,Perlguts},, and @xref{(perl.info)perlguts,Perlguts},, again.

