📄 peek.pm
字号:
The output: SV = RV(0xf041c) REFCNT = 1 FLAGS = (ROK) RV = 0xbab08 SV = IV(0xbe860) REFCNT = 2 FLAGS = (IOK,pIOK) IV = 42Starting from the top, this says C<$b> is an SV. The scalar is an RV, areference. It has the C<ROK> flag set, meaning it is a reference. BecauseROK is set we have an RV item rather than an IV or PV. Notice that Dumpfollows the reference and shows us what C<$b> was referencing. We see thesame C<$a> that we found in the previous example.Note that the value of C<RV> coincides with the numbers we see when westringify $b. The addresses inside RV() and IV() are addresses ofC<X***> structure which holds the current state of an C<SV>. Thisaddress may change during lifetime of an SV.=head2 A reference to an arrayThis shows what a reference to an array looks like. use Devel::Peek; $a = [42]; Dump $a;The output: 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 0xb5658 SV = IV(0xbe860) REFCNT = 1 FLAGS = (IOK,pIOK) IV = 42This says C<$a> is an SV and that it is an RV. That RV points toanother SV which is a PVAV, an array. The array has one element,element zero, which is another SV. The field C<FILL> above indicatesthe last element in the array, similar to C<$#$a>.If C<$a> pointed to an array of two elements then we would see thefollowing. use Devel::Peek 'Dump'; $a = [42,24]; Dump $a;The output: 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 0xb5658 SV = IV(0xbe860) REFCNT = 1 FLAGS = (IOK,pIOK) IV = 42 Elt No. 1 0xb5680 SV = IV(0xbe818) REFCNT = 1 FLAGS = (IOK,pIOK) IV = 24Note that C<Dump> will not report I<all> the elements in the array,only several first (depending on how deep it already went into thereport tree).=head2 A reference to a hashThe following shows the raw form of a reference to a hash. use Devel::Peek; $a = {hello=>42}; Dump $a;The output: SV = RV(0x8177858) at 0x816a618 REFCNT = 1 FLAGS = (ROK) RV = 0x814fc10 SV = PVHV(0x8167768) at 0x814fc10 REFCNT = 1 FLAGS = (SHAREKEYS) IV = 1 NV = 0 ARRAY = 0x816c5b8 (0:7, 1:1) hash quality = 100.0% KEYS = 1 FILL = 1 MAX = 7 RITER = -1 EITER = 0x0 Elt "hello" HASH = 0xc8fd181b SV = IV(0x816c030) at 0x814fcf4 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 42This shows C<$a> is a reference pointing to an SV. That SV is a PVHV, ahash. Fields RITER and EITER are used by C<L<each>>.The "quality" of a hash is defined as the total number of comparisons neededto access every element once, relative to the expected number needed for arandom hash. The value can go over 100%.The total number of comparisons is equal to the sum of the squares of thenumber of entries in each bucket. For a random hash of C<<n>> keys intoC<<k>> buckets, the expected value is: n + n(n-1)/2k=head2 Dumping a large array or hashThe C<Dump()> function, by default, dumps up to 4 elements from atoplevel array or hash. This number can be increased by supplying asecond argument to the function. use Devel::Peek; $a = [10,11,12,13,14]; Dump $a;Notice that C<Dump()> prints only elements 10 through 13 in the above code.The following code will print all of the elements. use Devel::Peek 'Dump'; $a = [10,11,12,13,14]; Dump $a, 5;=head2 A reference to an SV which holds a C pointerThis is what you really need to know as an XS programmer, of course. Whenan XSUB returns a pointer to a C structure that pointer is stored in an SVand a reference to that SV is placed on the XSUB stack. So the output froman XSUB which uses something like the T_PTROBJ map might look something likethis: 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"This shows that we have an SV which is an RV. That RV points at anotherSV. In this case that second SV is a PVMG, a blessed scalar. Because it isblessed it has the C<OBJECT> flag set. Note that an SV which holds a Cpointer also has the C<IOK> flag set. The C<STASH> is set to the packagename which this SV was blessed into.The output from an XSUB which uses something like the T_PTRREF map, whichdoesn't bless the object, might look something like this: SV = RV(0xf381c) REFCNT = 1 FLAGS = (ROK) RV = 0xb8ad8 SV = PVMG(0xbb3c8) REFCNT = 1 FLAGS = (IOK,pIOK) IV = 729160 NV = 0 PV = 0=head2 A reference to a subroutineLooks like this: SV = RV(0x798ec) REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1d453c SV = PVCV(0x1c768c) REFCNT = 2 FLAGS = () IV = 0 NV = 0 COMP_STASH = 0x31068 "main" START = 0xb20e0 ROOT = 0xbece0 XSUB = 0x0 XSUBANY = 0 GVGV::GV = 0x1d44e8 "MY" :: "top_targets" FILE = "(eval 5)" DEPTH = 0 PADLIST = 0x1c9338This shows that =over 4=item *the subroutine is not an XSUB (since C<START> and C<ROOT> arenon-zero, and C<XSUB> is zero);=item *that it was compiled in the package C<main>;=item *under the name C<MY::top_targets>; =item *inside a 5th eval in the program;=item *it is not currently executed (see C<DEPTH>);=item *it has no prototype (C<PROTOTYPE> field is missing).=back=head1 EXPORTSC<Dump>, C<mstat>, C<DeadCode>, C<DumpArray>, C<DumpWithOP> andC<DumpProg>, C<fill_mstats>, C<mstats_fillhash>, C<mstats2hash> bydefault. Additionally available C<SvREFCNT>, C<SvREFCNT_inc> andC<SvREFCNT_dec>.=head1 BUGSReaders have been known to skip important parts of L<perlguts>, causing muchfrustration for all.=head1 AUTHORIlya Zakharevich ilya@math.ohio-state.eduCopyright (c) 1995-98 Ilya Zakharevich. All rights reserved.This program is free software; you can redistribute it and/ormodify it under the same terms as Perl itself.Author of this software makes no claim whatsoever about suitability,reliability, edability, editability or usability of this product, andshould not be kept liable for any damage resulting from the use ofit. If you can use it, you are in luck, if not, I should not be keptresponsible. Keep a handy copy of your backup tape at hand.=head1 SEE ALSOL<perlguts>, and L<perlguts>, again.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -