CINXE.COM
Devel::Peek - A data debugging tool for the XS programmer - Perldoc Browser
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Devel::Peek - A data debugging tool for the XS programmer - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/Devel::Peek"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.41.5</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/Devel::Peek">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/Devel::Peek">5.40.1</a> <a class="dropdown-item" href="/5.40.0/Devel::Peek">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/Devel::Peek">5.38.3</a> <a class="dropdown-item" href="/5.38.2/Devel::Peek">5.38.2</a> <a class="dropdown-item" href="/5.38.1/Devel::Peek">5.38.1</a> <a class="dropdown-item" href="/5.38.0/Devel::Peek">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/Devel::Peek">5.36.3</a> <a class="dropdown-item" href="/5.36.2/Devel::Peek">5.36.2</a> <a class="dropdown-item" href="/5.36.1/Devel::Peek">5.36.1</a> <a class="dropdown-item" href="/5.36.0/Devel::Peek">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/Devel::Peek">5.34.3</a> <a class="dropdown-item" href="/5.34.2/Devel::Peek">5.34.2</a> <a class="dropdown-item" href="/5.34.1/Devel::Peek">5.34.1</a> <a class="dropdown-item" href="/5.34.0/Devel::Peek">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/Devel::Peek">5.32.1</a> <a class="dropdown-item" href="/5.32.0/Devel::Peek">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/Devel::Peek">5.30.3</a> <a class="dropdown-item" href="/5.30.2/Devel::Peek">5.30.2</a> <a class="dropdown-item" href="/5.30.1/Devel::Peek">5.30.1</a> <a class="dropdown-item" href="/5.30.0/Devel::Peek">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/Devel::Peek">5.28.3</a> <a class="dropdown-item" href="/5.28.2/Devel::Peek">5.28.2</a> <a class="dropdown-item" href="/5.28.1/Devel::Peek">5.28.1</a> <a class="dropdown-item" href="/5.28.0/Devel::Peek">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/Devel::Peek">5.26.3</a> <a class="dropdown-item" href="/5.26.2/Devel::Peek">5.26.2</a> <a class="dropdown-item" href="/5.26.1/Devel::Peek">5.26.1</a> <a class="dropdown-item" href="/5.26.0/Devel::Peek">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/Devel::Peek">5.24.4</a> <a class="dropdown-item" href="/5.24.3/Devel::Peek">5.24.3</a> <a class="dropdown-item" href="/5.24.2/Devel::Peek">5.24.2</a> <a class="dropdown-item" href="/5.24.1/Devel::Peek">5.24.1</a> <a class="dropdown-item" href="/5.24.0/Devel::Peek">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/Devel::Peek">5.22.4</a> <a class="dropdown-item" href="/5.22.3/Devel::Peek">5.22.3</a> <a class="dropdown-item" href="/5.22.2/Devel::Peek">5.22.2</a> <a class="dropdown-item" href="/5.22.1/Devel::Peek">5.22.1</a> <a class="dropdown-item" href="/5.22.0/Devel::Peek">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/Devel::Peek">5.20.3</a> <a class="dropdown-item" href="/5.20.2/Devel::Peek">5.20.2</a> <a class="dropdown-item" href="/5.20.1/Devel::Peek">5.20.1</a> <a class="dropdown-item" href="/5.20.0/Devel::Peek">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/Devel::Peek">5.18.4</a> <a class="dropdown-item" href="/5.18.3/Devel::Peek">5.18.3</a> <a class="dropdown-item" href="/5.18.2/Devel::Peek">5.18.2</a> <a class="dropdown-item" href="/5.18.1/Devel::Peek">5.18.1</a> <a class="dropdown-item" href="/5.18.0/Devel::Peek">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/Devel::Peek">5.16.3</a> <a class="dropdown-item" href="/5.16.2/Devel::Peek">5.16.2</a> <a class="dropdown-item" href="/5.16.1/Devel::Peek">5.16.1</a> <a class="dropdown-item" href="/5.16.0/Devel::Peek">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/Devel::Peek">5.14.4</a> <a class="dropdown-item" href="/5.14.3/Devel::Peek">5.14.3</a> <a class="dropdown-item" href="/5.14.2/Devel::Peek">5.14.2</a> <a class="dropdown-item" href="/5.14.1/Devel::Peek">5.14.1</a> <a class="dropdown-item" href="/5.14.0/Devel::Peek">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/Devel::Peek">5.12.5</a> <a class="dropdown-item" href="/5.12.4/Devel::Peek">5.12.4</a> <a class="dropdown-item" href="/5.12.3/Devel::Peek">5.12.3</a> <a class="dropdown-item" href="/5.12.2/Devel::Peek">5.12.2</a> <a class="dropdown-item" href="/5.12.1/Devel::Peek">5.12.1</a> <a class="dropdown-item" href="/5.12.0/Devel::Peek">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/Devel::Peek">5.10.1</a> <a class="dropdown-item" href="/5.10.0/Devel::Peek">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/Devel::Peek">5.8.9</a> <a class="dropdown-item" href="/5.8.8/Devel::Peek">5.8.8</a> <a class="dropdown-item" href="/5.8.7/Devel::Peek">5.8.7</a> <a class="dropdown-item" href="/5.8.6/Devel::Peek">5.8.6</a> <a class="dropdown-item" href="/5.8.5/Devel::Peek">5.8.5</a> <a class="dropdown-item" href="/5.8.4/Devel::Peek">5.8.4</a> <a class="dropdown-item" href="/5.8.3/Devel::Peek">5.8.3</a> <a class="dropdown-item" href="/5.8.2/Devel::Peek">5.8.2</a> <a class="dropdown-item" href="/5.8.1/Devel::Peek">5.8.1</a> <a class="dropdown-item" href="/5.8.0/Devel::Peek">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/Devel::Peek">5.6.2</a> <a class="dropdown-item" href="/5.6.1/Devel::Peek">5.6.1</a> <a class="dropdown-item" href="/5.6.0/Devel::Peek">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/Devel::Peek">5.005_04</a> <a class="dropdown-item" href="/5.005_03/Devel::Peek">5.005_03</a> <a class="dropdown-item" href="/5.005_02/Devel::Peek">5.005_02</a> <a class="dropdown-item" href="/5.005_01/Devel::Peek">5.005_01</a> <a class="dropdown-item" href="/5.005/Devel::Peek">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/Devel::Peek">blead</a> <a class="dropdown-item" href="/5.41.10/Devel::Peek">5.41.10</a> <a class="dropdown-item" href="/5.41.9/Devel::Peek">5.41.9</a> <a class="dropdown-item" href="/5.41.8/Devel::Peek">5.41.8</a> <a class="dropdown-item" href="/5.41.7/Devel::Peek">5.41.7</a> <a class="dropdown-item" href="/5.41.6/Devel::Peek">5.41.6</a> <a class="dropdown-item active" href="/5.41.5/Devel::Peek">5.41.5</a> <a class="dropdown-item" href="/5.41.4/Devel::Peek">5.41.4</a> <a class="dropdown-item" href="/5.41.3/Devel::Peek">5.41.3</a> <a class="dropdown-item" href="/5.41.2/Devel::Peek">5.41.2</a> <a class="dropdown-item" href="/5.41.1/Devel::Peek">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/Devel::Peek">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/Devel::Peek">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/Devel::Peek">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/Devel::Peek">5.39.10</a> <a class="dropdown-item" href="/5.39.9/Devel::Peek">5.39.9</a> <a class="dropdown-item" href="/5.39.8/Devel::Peek">5.39.8</a> <a class="dropdown-item" href="/5.39.7/Devel::Peek">5.39.7</a> <a class="dropdown-item" href="/5.39.6/Devel::Peek">5.39.6</a> <a class="dropdown-item" href="/5.39.5/Devel::Peek">5.39.5</a> <a class="dropdown-item" href="/5.39.4/Devel::Peek">5.39.4</a> <a class="dropdown-item" href="/5.39.3/Devel::Peek">5.39.3</a> <a class="dropdown-item" href="/5.39.2/Devel::Peek">5.39.2</a> <a class="dropdown-item" href="/5.39.1/Devel::Peek">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/Devel::Peek">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.41.5/perl">Perl</a> <a class="dropdown-item" href="/5.41.5/perlintro">Intro</a> <a class="dropdown-item" href="/5.41.5/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.41.5/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.41.5/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.41.5/perlop">Operators</a> <a class="dropdown-item" href="/5.41.5/functions">Functions</a> <a class="dropdown-item" href="/5.41.5/variables">Variables</a> <a class="dropdown-item" href="/5.41.5/modules">Modules</a> <a class="dropdown-item" href="/5.41.5/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.41.5/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.41.5/perlcommunity">Community</a> <a class="dropdown-item" href="/5.41.5/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.41.5/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.41.5/Devel::Peek">Devel::Peek</a> <div id="more"> (<a href="/5.41.5/Devel::Peek.txt">source</a>, <a href="https://metacpan.org/pod/Devel::Peek">CPAN</a>) </div> <div id="moduleversion">version 1.36</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.41.5. This is a development version of Perl. </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> <ul> <li> <a class="text-decoration-none" href="#Runtime-debugging">Runtime debugging</a> </li> <li> <a class="text-decoration-none" href="#Memory-footprint-debugging">Memory footprint debugging</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#EXAMPLES">EXAMPLES</a> <ul> <li> <a class="text-decoration-none" href="#A-simple-scalar-string">A simple scalar string</a> </li> <li> <a class="text-decoration-none" href="#A-simple-scalar-number">A simple scalar number</a> </li> <li> <a class="text-decoration-none" href="#A-simple-scalar-with-an-extra-reference">A simple scalar with an extra reference</a> </li> <li> <a class="text-decoration-none" href="#A-reference-to-a-simple-scalar">A reference to a simple scalar</a> </li> <li> <a class="text-decoration-none" href="#A-reference-to-an-array">A reference to an array</a> </li> <li> <a class="text-decoration-none" href="#A-reference-to-a-hash">A reference to a hash</a> </li> <li> <a class="text-decoration-none" href="#Dumping-a-large-array-or-hash">Dumping a large array or hash</a> </li> <li> <a class="text-decoration-none" href="#A-reference-to-an-SV-which-holds-a-C-pointer">A reference to an SV which holds a C pointer</a> </li> <li> <a class="text-decoration-none" href="#A-reference-to-a-subroutine">A reference to a subroutine</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#EXPORTS">EXPORTS</a> </li> <li> <a class="text-decoration-none" href="#BUGS">BUGS</a> </li> <li> <a class="text-decoration-none" href="#AUTHOR">AUTHOR</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>Devel::Peek - A data debugging tool for the XS programmer</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code> use Devel::Peek; Dump( $a ); Dump( $a, 5 ); Dump( @a ); Dump( %h ); DumpArray( 5, $a, $b, ... ); mstat "Point 5"; use Devel::Peek ':opd=st';</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>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.</p> <p>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 <a href="/5.41.5/perlguts">perlguts</a>.</p> <p>Devel::Peek supplies a <code>Dump()</code> function which can dump a raw Perl datatype, and <code>mstat("marker")</code> function to report on memory usage (if perl is compiled with corresponding option). The function DeadCode() provides statistics on the data "frozen" into inactive <code>CV</code>. Devel::Peek also supplies <code>SvREFCNT()</code> which can query 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()</code> function.</p> <p>All output is to STDERR.</p> <p>The <code>Dump()</code> function takes one or two arguments: something to dump, and an optional limit for recursion and array elements (default is 4). The first argument is evaluated in rvalue scalar context, with exceptions for @array and %hash, which dump the array or hash itself. So <code>Dump @array</code> works, as does <code>Dump $foo</code>. And <code>Dump pos</code> will call <code>pos</code> in rvalue context, whereas <code>Dump ${\pos}</code> will call it in lvalue context.</p> <p>Function <code>DumpArray()</code> allows dumping of multiple values (useful when you need to analyze returns of functions).</p> <p>The global variable $Devel::Peek::pv_limit can be set to limit the number of character printed in various string values. Setting it to 0 means no limit.</p> <p>If <code>use Devel::Peek</code> directive has a <code>:opd=FLAGS</code> argument, this switches on debugging of opcode dispatch. <code>FLAGS</code> should be a combination of <code>s</code>, <code>t</code>, and <code>P</code> (see <a href="/5.41.5/perlrun#-Dletters"><b>-D</b> flags in perlrun</a>).</p> <p><code>:opd</code> is a shortcut for <code>:opd=st</code>.</p> <h2 id="Runtime-debugging"><a class="permalink" href="#Runtime-debugging">#</a><a id="Runtime"></a>Runtime debugging</h2> <p><code>CvGV($cv)</code> return one of the globs associated to a subroutine reference $cv.</p> <p>debug_flags() returns a string representation of <code>$^D</code> (similar to what is allowed for <b>-D</b> flag). When called with a numeric argument, sets $^D to the corresponding value. When called with an argument of the form <code>"flags-flags"</code>, set on/off bits of <code>$^D</code> corresponding to letters before/after <code>-</code>. (The returned value is for <code>$^D</code> before the modification.)</p> <p>runops_debug() returns true if the current <i>opcode dispatcher</i> is the debugging one. When called with an argument, switches to debugging or non-debugging dispatcher depending on the argument (active for newly-entered subs/etc only). (The returned value is for the dispatcher before the modification.)</p> <h2 id="Memory-footprint-debugging"><a class="permalink" href="#Memory-footprint-debugging">#</a><a id="Memory"></a>Memory footprint debugging</h2> <p>When perl is compiled with support for memory footprint debugging (default with Perl's malloc()), Devel::Peek provides an access to this API.</p> <p>Use mstat() function to emit a memory state statistic to the terminal. For more information on the format of output of mstat() see <a href="/5.41.5/perldebguts#Using-%24ENV%7BPERL_DEBUG_MSTATS%7D">"Using $ENV{PERL_DEBUG_MSTATS}" in perldebguts</a>.</p> <p>Three additional functions allow access to this statistic from Perl. First, use <code>mstats_fillhash(%hash)</code> to get the information contained in the output of mstat() into %hash. The field of this hash are</p> <pre><code class="plaintext">minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks start_slack topbucket topbucket_ev topbucket_odd total total_chain total_sbrk totfree</code></pre> <p>Two additional fields <code>free</code>, <code>used</code> contain array references which provide per-bucket count of free and used chunks. Two other fields <code>mem_size</code>, <code>available_size</code> contain array references which provide the information about the allocated size and usable size of chunks in each bucket. Again, see <a href="/5.41.5/perldebguts#Using-%24ENV%7BPERL_DEBUG_MSTATS%7D">"Using $ENV{PERL_DEBUG_MSTATS}" in perldebguts</a> for details.</p> <p>Keep in mind that only the first several "odd-numbered" buckets are used, so the information on size of the "odd-numbered" buckets which are not used is probably meaningless.</p> <p>The information in</p> <pre><code class="plaintext">mem_size available_size minbucket nbuckets</code></pre> <p>is the property of a particular build of perl, and does not depend on the current process. If you do not provide the optional argument to the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then the information in fields <code>mem_size</code>, <code>available_size</code> is not updated.</p> <p><code>fill_mstats($buf)</code> is a much cheaper call (both speedwise and memory-wise) which collects the statistic into $buf in machine-readable form. At a later moment you may need to call <code>mstats2hash($buf, %hash)</code> to use this information to fill %hash.</p> <p>All three APIs <code>fill_mstats($buf)</code>, <code>mstats_fillhash(%hash)</code>, and <code>mstats2hash($buf, %hash)</code> are designed to allocate no memory if used <i>the second time</i> on the same $buf and/or %hash.</p> <p>So, if you want to collect memory info in a cycle, you may call</p> <pre><code>$#buf = 999; fill_mstats($_) for @buf; mstats_fillhash(%report, 1); # Static info too foreach (@buf) { # Do something... fill_mstats $_; # Collect statistic } foreach (@buf) { mstats2hash($_, %report); # Preserve static info # Do something with %report }</code></pre> <h1 id="EXAMPLES"><a class="permalink" href="#EXAMPLES">#</a>EXAMPLES</h1> <p>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).</p> <p>Oh, one final bit of advice: take <a href="/5.41.5/perlguts">perlguts</a> with you. When you return we expect to see it well-thumbed.</p> <h2 id="A-simple-scalar-string"><a class="permalink" href="#A-simple-scalar-string">#</a><a id="A"></a>A simple scalar string</h2> <p>Let's begin by looking a simple scalar which is holding a string.</p> <pre><code>use Devel::Peek; $a = 42; $a = "hello"; Dump $a;</code></pre> <p>The output:</p> <pre><code class="plaintext">SV = PVIV(0xbc288) at 0xbe9a8 REFCNT = 1 FLAGS = (POK,pPOK) IV = 42 PV = 0xb2048 "hello"\0 CUR = 5 LEN = 8</code></pre> <p>This says <code>$a</code> is an SV, a scalar. The scalar type is a PVIV, which is capable of holding an integer (IV) and/or a string (PV) value. The scalar's head is allocated at address 0xbe9a8, while the body is at 0xbc288. Its reference count is 1. It has the <code>POK</code> 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. The \0 at the end indicate that this PV is properly NUL-terminated. Note that the IV field still contains its old numeric value, but because FLAGS doesn't have IOK set, we must ignore the IV item. CUR indicates the number of characters in the PV. LEN indicates the number of bytes allocated for the PV (at least one more than CUR, because LEN includes an extra byte for the end-of-string marker, then usually rounded up to some efficient allocation unit).</p> <h2 id="A-simple-scalar-number"><a class="permalink" href="#A-simple-scalar-number">#</a><a id="A1"></a>A simple scalar number</h2> <p>If the scalar contains a number the raw SV will be leaner.</p> <pre><code>use Devel::Peek; $a = 42; Dump $a;</code></pre> <p>The output:</p> <pre><code class="plaintext">SV = IV(0xbc818) at 0xbe9a8 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 42</code></pre> <p>This says <code>$a</code> is an SV, a scalar. The scalar is an IV, a number. Its reference count is 1. It has the <code>IOK</code> 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.</p> <h2 id="A-simple-scalar-with-an-extra-reference"><a class="permalink" href="#A-simple-scalar-with-an-extra-reference">#</a><a id="A2"></a>A simple scalar with an extra reference</h2> <p>If the scalar from the previous example had an extra reference:</p> <pre><code>use Devel::Peek; $a = 42; $b = \$a; Dump $a;</code></pre> <p>The output:</p> <pre><code class="plaintext">SV = IV(0xbe860) at 0xbe9a8 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 42</code></pre> <p>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</code> instead of <code>$a</code>.</p> <h2 id="A-reference-to-a-simple-scalar"><a class="permalink" href="#A-reference-to-a-simple-scalar">#</a><a id="A3"></a>A reference to a simple scalar</h2> <p>This shows what a reference looks like when it references a simple scalar.</p> <pre><code>use Devel::Peek; $a = 42; $b = \$a; Dump $b;</code></pre> <p>The output:</p> <pre><code class="plaintext">SV = IV(0xf041c) at 0xbe9a0 REFCNT = 1 FLAGS = (ROK) RV = 0xbab08 SV = IV(0xbe860) at 0xbe9a8 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 42</code></pre> <p>Starting from the top, this says <code>$b</code> is an SV. The scalar is an IV, which is capable of holding an integer or reference value. It has the <code>ROK</code> flag set, meaning it is a reference (rather than an integer or string). Notice that Dump follows the reference and shows us what <code>$b</code> was referencing. We see the same <code>$a</code> that we found in the previous example.</p> <p>Note that the value of <code>RV</code> coincides with the numbers we see when we stringify $b. The addresses inside IV() are addresses of <code>X***</code> structures which hold the current state of an <code>SV</code>. This address may change during lifetime of an SV.</p> <h2 id="A-reference-to-an-array"><a class="permalink" href="#A-reference-to-an-array">#</a><a id="A4"></a>A reference to an array</h2> <p>This shows what a reference to an array looks like.</p> <pre><code>use Devel::Peek; $a = [42]; Dump $a;</code></pre> <p>The output:</p> <pre><code class="plaintext">SV = IV(0xc85998) at 0xc859a8 REFCNT = 1 FLAGS = (ROK) RV = 0xc70de8 SV = PVAV(0xc71e10) at 0xc70de8 REFCNT = 1 FLAGS = () ARRAY = 0xc7e820 FILL = 0 MAX = 0 FLAGS = (REAL) Elt No. 0 SV = IV(0xc70f88) at 0xc70f98 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 42</code></pre> <p>This says <code>$a</code> is a reference (ROK), which 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</code> above indicates the last element in the array, similar to <code>$#$a</code>.</p> <p>If <code>$a</code> pointed to an array of two elements then we would see the following.</p> <pre><code>use Devel::Peek 'Dump'; $a = [42,24]; Dump $a;</code></pre> <p>The output:</p> <pre><code class="plaintext">SV = IV(0x158c998) at 0x158c9a8 REFCNT = 1 FLAGS = (ROK) RV = 0x1577de8 SV = PVAV(0x1578e10) at 0x1577de8 REFCNT = 1 FLAGS = () ARRAY = 0x1585820 FILL = 1 MAX = 1 FLAGS = (REAL) Elt No. 0 SV = IV(0x1577f88) at 0x1577f98 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 42 Elt No. 1 SV = IV(0x158be88) at 0x158be98 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 24</code></pre> <p>Note that <code>Dump</code> will not report <i>all</i> the elements in the array, only several first (depending on how deep it already went into the report tree).</p> <h2 id="A-reference-to-a-hash"><a class="permalink" href="#A-reference-to-a-hash">#</a><a id="A5"></a>A reference to a hash</h2> <p>The following shows the raw form of a reference to a hash.</p> <pre><code>use Devel::Peek; $a = {hello=>42}; Dump $a;</code></pre> <p>The output:</p> <pre><code class="plaintext">SV = IV(0x55cb50b50fb0) at 0x55cb50b50fc0 REFCNT = 1 FLAGS = (ROK) RV = 0x55cb50b2b758 SV = PVHV(0x55cb50b319c0) at 0x55cb50b2b758 REFCNT = 1 FLAGS = (SHAREKEYS) ARRAY = 0x55cb50b941a0 (0:7, 1:1) hash quality = 100.0% KEYS = 1 FILL = 1 MAX = 7 Elt "hello" HASH = 0x3128ece4 SV = IV(0x55cb50b464f8) at 0x55cb50b46508 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 42</code></pre> <p>This shows <code>$a</code> is a reference pointing to an SV. That SV is a PVHV, a hash.</p> <p>The "quality" of a hash is defined as the total number of comparisons needed to access every element once, relative to the expected number needed for a random hash. The value can go over 100%.</p> <p>The total number of comparisons is equal to the sum of the squares of the number of entries in each bucket. For a random hash of <code><n</code>> keys into <code><k</code>> buckets, the expected value is:</p> <pre><code class="plaintext">n + n(n-1)/2k</code></pre> <h2 id="Dumping-a-large-array-or-hash"><a class="permalink" href="#Dumping-a-large-array-or-hash">#</a><a id="Dumping"></a>Dumping a large array or hash</h2> <p>The <code>Dump()</code> 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.</p> <pre><code>use Devel::Peek; $a = [10,11,12,13,14]; Dump $a;</code></pre> <p>Notice that <code>Dump()</code> prints only elements 10 through 13 in the above code. The following code will print all of the elements.</p> <pre><code>use Devel::Peek 'Dump'; $a = [10,11,12,13,14]; Dump $a, 5;</code></pre> <h2 id="A-reference-to-an-SV-which-holds-a-C-pointer"><a class="permalink" href="#A-reference-to-an-SV-which-holds-a-C-pointer">#</a><a id="A6"></a>A reference to an SV which holds a C pointer</h2> <p>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:</p> <pre><code class="plaintext">SV = IV(0xf381c) at 0xc859a8 REFCNT = 1 FLAGS = (ROK) RV = 0xb8ad8 SV = PVMG(0xbb3c8) at 0xc859a0 REFCNT = 1 FLAGS = (OBJECT,IOK,pIOK) IV = 729160 NV = 0 PV = 0 STASH = 0xc1d10 "CookBookB::Opaque"</code></pre> <p>This shows that we have an SV which is a reference, which points at another SV. In this case that second SV is a PVMG, a blessed scalar. Because it is blessed it has the <code>OBJECT</code> flag set. Note that an SV which holds a C pointer also has the <code>IOK</code> flag set. The <code>STASH</code> is set to the package name which this SV was blessed into.</p> <p>The output from an XSUB which uses something like the T_PTRREF map, which doesn't bless the object, might look something like this:</p> <pre><code class="plaintext">SV = IV(0xf381c) at 0xc859a8 REFCNT = 1 FLAGS = (ROK) RV = 0xb8ad8 SV = PVMG(0xbb3c8) at 0xc859a0 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 729160 NV = 0 PV = 0</code></pre> <h2 id="A-reference-to-a-subroutine"><a class="permalink" href="#A-reference-to-a-subroutine">#</a><a id="A7"></a>A reference to a subroutine</h2> <p>Looks like this:</p> <pre><code class="plaintext">SV = IV(0x24d2dd8) at 0x24d2de8 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x24e79d8 SV = PVCV(0x24e5798) at 0x24e79d8 REFCNT = 2 FLAGS = () COMP_STASH = 0x22c9c50 "main" START = 0x22eed60 ===> 0 ROOT = 0x22ee490 GVGV::GV = 0x22de9d8 "MY" :: "top_targets" FILE = "(eval 5)" DEPTH = 0 FLAGS = 0x0 OUTSIDE_SEQ = 93 PADLIST = 0x22e9ed8 PADNAME = 0x22e9ec0(0x22eed00) PAD = 0x22e9ea8(0x22eecd0) OUTSIDE = 0x22c9fb0 (MAIN)</code></pre> <p>This shows that</p> <ul> <li><p>the subroutine is not an XSUB (since <code>START</code> and <code>ROOT</code> are non-zero, and <code>XSUB</code> is not listed, and is thus null);</p> </li> <li><p>that it was compiled in the package <code>main</code>;</p> </li> <li><p>under the name <code>MY::top_targets</code>;</p> </li> <li><p>inside a 5th eval in the program;</p> </li> <li><p>it is not currently executed (because <code>DEPTH</code> is 0);</p> </li> <li><p>it has no prototype (<code>PROTOTYPE</code> field is missing).</p> </li> </ul> <h1 id="EXPORTS"><a class="permalink" href="#EXPORTS">#</a>EXPORTS</h1> <p><code>Dump</code>, <code>mstat</code>, <code>DeadCode</code>, <code>DumpArray</code>, <code>DumpWithOP</code> and <code>DumpProg</code>, <code>fill_mstats</code>, <code>mstats_fillhash</code>, <code>mstats2hash</code> by default. Additionally available is <code>SvREFCNT</code>.</p> <h1 id="BUGS"><a class="permalink" href="#BUGS">#</a>BUGS</h1> <p>Readers have been known to skip important parts of <a href="/5.41.5/perlguts">perlguts</a>, causing much frustration for all.</p> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Ilya Zakharevich ilya@math.ohio-state.edu</p> <p>Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p> <p>Author of this software makes no claim whatsoever about suitability, reliability, edability, editability or usability of this product, and should not be kept liable for any damage resulting from the use of it. If you can use it, you are in luck, if not, I should not be kept responsible. Keep a handy copy of your backup tape at hand.</p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="/5.41.5/perlguts">perlguts</a>, and <a href="/5.41.5/perlguts">perlguts</a>, again.</p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>