.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "FindRef 3pm" .TH FindRef 3pm "2014-06-03" "perl v5.20.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Devel::FindRef \- where is that reference to my variable hiding? .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Devel::FindRef; \& \& print Devel::FindRef::track \e$some_variable; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Tracking down reference problems (e.g. you expect some object to be destroyed, but there are still references to it that keep it alive) can be very hard. Fortunately, perl keeps track of all its values, so tracking references \*(L"backwards\*(R" is usually possible. .PP The \f(CW\*(C`track\*(C'\fR function can help track down some of those references back to the variables containing them. .PP For example, for this fragment: .PP .Vb 1 \& package Test; \& \& use Devel::FindRef; \& use Scalar::Util; \& \& our $var = "hi\en"; \& my $global_my = \e$var; \& our %global_hash = (ukukey => \e$var); \& our $global_hashref = { ukukey2 => \e$var }; \& \& sub testsub { \& my $testsub_local = $global_hashref; \& print Devel::FindRef::track \e$var; \& } \& \& my $closure = sub { \& my $closure_var = \e$_[0]; \& Scalar::Util::weaken (my $weak_ref = \e$var); \& testsub; \& }; \& \& $closure\->($var); .Ve .PP The output is as follows (or similar to this, in case I forget to update the manpage after some changes): .PP .Vb 10 \& SCALAR(0x7cc888) [refcount 6] is \& +\- referenced by REF(0x8abcc8) [refcount 1], which is \& | the lexical \*(Aq$closure_var\*(Aq in CODE(0x8abc50) [refcount 4], which is \& | +\- the closure created at tst:18. \& | +\- referenced by REF(0x7d3c58) [refcount 1], which is \& | | the lexical \*(Aq$closure\*(Aq in CODE(0x7ae530) [refcount 2], which is \& | | +\- the containing scope for CODE(0x8ab430) [refcount 3], which is \& | | | the global &Test::testsub. \& | | +\- the main body of the program. \& | +\- the lexical \*(Aq&\*(Aq in CODE(0x7ae530) [refcount 2], which was seen before. \& +\- referenced by REF(0x7cc7c8) [refcount 1], which is \& | the lexical \*(Aq$global_my\*(Aq in CODE(0x7ae530) [refcount 2], which was seen before. \& +\- the global $Test::var. \& +\- referenced by REF(0x7cc558) [refcount 1], which is \& | the member \*(Aqukukey2\*(Aq of HASH(0x7ae140) [refcount 2], which is \& | +\- referenced by REF(0x8abad0) [refcount 1], which is \& | | the lexical \*(Aq$testsub_local\*(Aq in CODE(0x8ab430) [refcount 3], which was seen before. \& | +\- referenced by REF(0x8ab4f0) [refcount 1], which is \& | the global $Test::global_hashref. \& +\- referenced by REF(0x7ae518) [refcount 1], which is \& | the member \*(Aqukukey\*(Aq of HASH(0x7d3bb0) [refcount 1], which is \& | the global %Test::global_hash. \& +\- referenced by REF(0x7ae2f0) [refcount 1], which is \& a temporary on the stack. .Ve .PP It is a bit convoluted to read, but basically it says that the value stored in \f(CW$var\fR is referenced by: .ie n .IP "\- the lexical $closure_var (0x8abcc8), which is inside an instantiated closure, which in turn is used quite a bit." 4 .el .IP "\- the lexical \f(CW$closure_var\fR (0x8abcc8), which is inside an instantiated closure, which in turn is used quite a bit." 4 .IX Item "- the lexical $closure_var (0x8abcc8), which is inside an instantiated closure, which in turn is used quite a bit." .PD 0 .ie n .IP "\- the package-level lexical $global_my." 4 .el .IP "\- the package-level lexical \f(CW$global_my\fR." 4 .IX Item "- the package-level lexical $global_my." .ie n .IP "\- the global package variable named $Test::var." 4 .el .IP "\- the global package variable named \f(CW$Test::var\fR." 4 .IX Item "- the global package variable named $Test::var." .ie n .IP "\- the hash element ""ukukey2"", in the hash in the my variable $testsub_local in the sub ""Test::testsub"" and also in the hash ""$referenced by Test::hash2""." 4 .el .IP "\- the hash element \f(CWukukey2\fR, in the hash in the my variable \f(CW$testsub_local\fR in the sub \f(CWTest::testsub\fR and also in the hash \f(CW$referenced by Test::hash2\fR." 4 .IX Item "- the hash element ukukey2, in the hash in the my variable $testsub_local in the sub Test::testsub and also in the hash $referenced by Test::hash2." .ie n .IP "\- the hash element with key ""ukukey"" in the hash stored in %Test::hash." 4 .el .IP "\- the hash element with key \f(CWukukey\fR in the hash stored in \f(CW%Test::hash\fR." 4 .IX Item "- the hash element with key ukukey in the hash stored in %Test::hash." .ie n .IP "\- some anonymous mortalised reference on the stack (which is caused by calling ""track"" with the expression ""\e$var"", which creates the reference)." 4 .el .IP "\- some anonymous mortalised reference on the stack (which is caused by calling \f(CWtrack\fR with the expression \f(CW\e$var\fR, which creates the reference)." 4 .IX Item "- some anonymous mortalised reference on the stack (which is caused by calling track with the expression $var, which creates the reference)." .PD .PP And all these account for six reference counts. .SH "EXPORTS" .IX Header "EXPORTS" None. .SH "FUNCTIONS" .IX Header "FUNCTIONS" .ie n .IP "$string = Devel::FindRef::track $ref[, $depth]" 4 .el .IP "\f(CW$string\fR = Devel::FindRef::track \f(CW$ref\fR[, \f(CW$depth\fR]" 4 .IX Item "$string = Devel::FindRef::track $ref[, $depth]" Track the perl value pointed to by \f(CW$ref\fR up to a depth of \f(CW$depth\fR and return a descriptive string. \f(CW$ref\fR can point at any perl value, be it anonymous sub, hash, array, scalar etc. .Sp This is the function you most likely want to use when tracking down references. .ie n .IP "@references = Devel::FindRef::find $ref" 4 .el .IP "\f(CW@references\fR = Devel::FindRef::find \f(CW$ref\fR" 4 .IX Item "@references = Devel::FindRef::find $ref" Return arrayrefs that contain [$message, \f(CW$ref\fR] pairs. The message describes what kind of reference was found and the \f(CW$ref\fR is the reference itself, which can be omitted if \f(CW\*(C`find\*(C'\fR decided to end the search. The returned references are all weak references. .Sp The \f(CW\*(C`track\*(C'\fR function uses this to find references to the value you are interested in and recurses on the returned references. .ie n .IP "$ref = Devel::FindRef::ptr2ref $integer" 4 .el .IP "\f(CW$ref\fR = Devel::FindRef::ptr2ref \f(CW$integer\fR" 4 .IX Item "$ref = Devel::FindRef::ptr2ref $integer" Sometimes you know (from debugging output) the address of a perl value you are interested in (e.g. \f(CW\*(C`HASH(0x176ff70)\*(C'\fR). This function can be used to turn the address into a reference to that value. It is quite safe to call on valid addresses, but extremely dangerous to call on invalid ones. \fINo checks whatsoever will be done\fR, so don't use this unless you really know the value is the address of a valid perl value. .Sp .Vb 2 \& # we know that HASH(0x176ff70) exists, so turn it into a hashref: \& my $ref_to_hash = Devel::FindRef::ptr2ref 0x176ff70; .Ve .ie n .IP "$ptr = Devel::FindRef::ref2ptr $reference" 4 .el .IP "\f(CW$ptr\fR = Devel::FindRef::ref2ptr \f(CW$reference\fR" 4 .IX Item "$ptr = Devel::FindRef::ref2ptr $reference" The opposite of \f(CW\*(C`ptr2ref\*(C'\fR, above: returns the internal address of the value pointed to by the passed reference. This function is safe to call on anything, and returns the same value that a normal reference would if used in a numeric context. .SH "ENVIRONMENT VARIABLES" .IX Header "ENVIRONMENT VARIABLES" You can set the environment variable \f(CW\*(C`PERL_DEVEL_FINDREF_DEPTH\*(C'\fR to an integer to override the default depth in \f(CW\*(C`track\*(C'\fR. If a call explicitly specifies a depth, it is not overridden. .SH "AUTHOR" .IX Header "AUTHOR" Marc Lehmann . .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2007, 2008, 2009, 2013 by Marc Lehmann. .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.