This file is indexed.

/usr/share/perl5/Devel/Cycle.pm is in libdevel-cycle-perl 1.11-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package Devel::Cycle;
# $Id: Cycle.pm,v 1.15 2009/08/24 12:51:02 lstein Exp $

use 5.006001;
use strict;
use Carp 'croak','carp';
use warnings;

use Scalar::Util qw(isweak blessed refaddr reftype);

my $SHORT_NAME = 'A';
my %SHORT_NAMES;


require Exporter;

our @ISA = qw(Exporter);
our @EXPORT = qw(find_cycle find_weakened_cycle);
our @EXPORT_OK = qw($FORMATTING);
our $VERSION = '1.11';
our $FORMATTING = 'roasted';
our $QUIET   = 0;

my %import_args = (-quiet =>1,
		   -raw   =>1,
		   -cooked =>1,
		   -roasted=>1);

BEGIN {
  require constant;
  constant->import( HAVE_PADWALKER =>
		    eval {
		      require PadWalker;
		      $PadWalker::VERSION >= 1.0;
		    });
}

sub import {
  my $self = shift;
  my @args = @_;
  my %args = map {$_=>1} @args;
  $QUIET++    if exists $args{-quiet};
  $FORMATTING = 'roasted' if exists $args{-roasted};
  $FORMATTING = 'raw'     if exists $args{-raw};
  $FORMATTING = 'cooked'  if exists $args{-cooked};
  $self->export_to_level(1,$self,grep {!exists $import_args{$_}} @_);
}

sub find_weakened_cycle {
  my $ref      = shift;
  my $callback = shift;
  unless ($callback) {
    my $counter = 0;
    $callback = sub {
      _do_report(++$counter,shift)
    }
  }
  _find_cycle($ref,{},$callback,1,{},());
}

sub find_cycle {
  my $ref      = shift;
  my $callback = shift;
  unless ($callback) {
    my $counter = 0;
    $callback = sub {
      _do_report(++$counter,shift)
    }
  }
  _find_cycle($ref,{},$callback,0,{},());
}

sub _find_cycle {
  my $current   = shift;
  my $seenit    = shift;
  my $callback  = shift;
  my $inc_weak_refs = shift;
  my $complain = shift;
  my @report  = @_;

  return unless ref $current;

  # note: it seems like you could just do:
  #
  #    return if isweak($current);
  #
  # but strangely the weak flag doesn't seem to survive the copying,
  # so the test has to happen directly on the reference in the data
  # structure being scanned.

  if ($seenit->{refaddr $current}) {
    $callback->(\@report);
    return;
  }
  $seenit->{refaddr $current}++;

  _find_cycle_dispatch($current,{%$seenit},$callback,$inc_weak_refs,$complain,@report);
}

sub _find_cycle_dispatch {
  my $type = _get_type($_[0]);

  if (!defined $type) {
    my $ref = reftype $_[0];
    our %already_warned;
    if (!$already_warned{$ref}++) {
	warn "Unhandled type: $ref";
    }
    return;
  }
  my $sub = do { no strict 'refs'; \&{"_find_cycle_$type"} };
  $sub->(@_);
}

sub _find_cycle_SCALAR {
  my $current   = shift;
  my $seenit    = shift;
  my $callback  = shift;
  my $inc_weak_refs = shift;
  my $complain  = shift;
  my @report  = @_;

  return if !$inc_weak_refs && isweak($$current);
  _find_cycle($$current,{%$seenit},$callback,$inc_weak_refs,$complain,
              (@report,['SCALAR',undef,$current => $$current,$inc_weak_refs?isweak($$current):()]));
}

sub _find_cycle_ARRAY {
  my $current   = shift;
  my $seenit    = shift;
  my $callback  = shift;
  my $inc_weak_refs = shift;
  my $complain  = shift;
  my @report  = @_;

  for (my $i=0; $i<@$current; $i++) {
    next if !$inc_weak_refs && isweak($current->[$i]);
    _find_cycle($current->[$i],{%$seenit},$callback,$inc_weak_refs,$complain,
                (@report,['ARRAY',$i,$current => $current->[$i],$inc_weak_refs?isweak($current->[$i]):()]));
    }
}

sub _find_cycle_HASH {
  my $current   = shift;
  my $seenit    = shift;
  my $callback  = shift;
  my $inc_weak_refs = shift;
  my $complain  = shift;
  my @report  = @_;

  for my $key (sort keys %$current) {
    next if !$inc_weak_refs && isweak($current->{$key});
    _find_cycle($current->{$key},{%$seenit},$callback,$inc_weak_refs,$complain,
                (@report,['HASH',$key,$current => $current->{$key},$inc_weak_refs?isweak($current->{$key}):()]));
    }
}

sub _find_cycle_CODE {
  my $current   = shift;
  my $seenit    = shift;
  my $callback  = shift;
  my $inc_weak_refs = shift;
  my $complain  = shift;
  my @report  = @_;

  unless (HAVE_PADWALKER) {
    if (!$complain->{$current} && !$QUIET) {
      carp "A code closure was detected in but we cannot check it unless the PadWalker module is installed";
    }

    return;
  }

  my $closed_vars = PadWalker::closed_over( $current );
  foreach my $varname ( sort keys %$closed_vars ) {
    my $value = $closed_vars->{$varname};
    _find_cycle_dispatch($value,{%$seenit},$callback,$inc_weak_refs,$complain,
                         (@report,['CODE',$varname,$current => $value]));
  }
}

sub _do_report {
  my $counter = shift;
  my $path    = shift;
  print "Cycle ($counter):\n";
  foreach (@$path) {
    my ($type,$index,$ref,$value,$is_weak) = @$_;
    printf("\t%30s => %-30s\n",($is_weak ? 'w-> ' : '')._format_reference($type,$index,$ref,0),_format_reference(undef,undef,$value,1));
  }
  print "\n";
}

sub _format_reference {
  my ($type,$index,$ref,$deref) = @_;
  $type ||= _get_type($ref);
  return $ref unless $type;
  my $suffix  = defined $index ? _format_index($type,$index) : '';
  if ($FORMATTING eq 'raw') {
    return $ref.$suffix;
  }

  else {
    my $package  = blessed($ref);
    my $prefix   = $package ? ($FORMATTING eq 'roasted' ? "${package}::" : "${package}="  ) : '';
    my $sygil    = $deref ? '\\' : '';
    my $shortname = ($SHORT_NAMES{$ref} ||= $SHORT_NAME++);
    return $sygil . ($sygil ? '$' : '$$'). $prefix . $shortname . $suffix if $type eq 'SCALAR';
    return $sygil . ($sygil ? '@' : '$') . $prefix . $shortname . $suffix  if $type eq 'ARRAY';
    return $sygil . ($sygil ? '%' : '$') . $prefix . $shortname . $suffix  if $type eq 'HASH';
    return $sygil . ($sygil ? '&' : '$') . $prefix . $shortname . $suffix if $type eq 'CODE';
  }
}

# why not Scalar::Util::reftype?
sub _get_type {
  my $thingy = shift;
  return unless ref $thingy;
  return 'SCALAR' if UNIVERSAL::isa($thingy,'SCALAR') || UNIVERSAL::isa($thingy,'REF') || UNIVERSAL::isa($thingy,'Regexp');
  return 'ARRAY'  if UNIVERSAL::isa($thingy,'ARRAY');
  return 'HASH'   if UNIVERSAL::isa($thingy,'HASH');
  return 'CODE'   if UNIVERSAL::isa($thingy,'CODE');
  undef;
}

sub _format_index {
  my ($type,$index) = @_;
  return "->[$index]" if $type eq 'ARRAY';
  return "->{'$index'}" if $type eq 'HASH';
  return " variable $index" if $type eq 'CODE';
  return;
}

1;
__END__

=head1 NAME

Devel::Cycle - Find memory cycles in objects

=head1 SYNOPSIS

  #!/usr/bin/perl
  use Devel::Cycle;
  my $test = {fred   => [qw(a b c d e)],
	    ethel  => [qw(1 2 3 4 5)],
	    george => {martha => 23,
		       agnes  => 19}
	   };
  $test->{george}{phyllis} = $test;
  $test->{fred}[3]      = $test->{george};
  $test->{george}{mary} = $test->{fred};
  find_cycle($test);
  exit 0;

  # output:

  Cycle (1):
	                $A->{'george'} => \%B
	               $B->{'phyllis'} => \%A

  Cycle (2):
	                $A->{'george'} => \%B
	                  $B->{'mary'} => \@A
	                       $A->[3] => \%B

  Cycle (3):
	                  $A->{'fred'} => \@A
	                       $A->[3] => \%B
	               $B->{'phyllis'} => \%A

  Cycle (4):
	                  $A->{'fred'} => \@A
	                       $A->[3] => \%B
	                  $B->{'mary'} => \@A
  
  # you can also check weakened references
  weaken($test->{george}->{phyllis});
  find_weakened_cycle($test);
  exit 0;

  # output:
  
  Cycle (1):
                        $A->{'george'} => \%B                           
                          $B->{'mary'} => \@C                           
                               $C->[3] => \%B                           

  Cycle (2):
                        $A->{'george'} => \%B                           
                   w-> $B->{'phyllis'} => \%A                           

  Cycle (3):
                          $A->{'fred'} => \@C                           
                               $C->[3] => \%B                           
                          $B->{'mary'} => \@C                           

  Cycle (4):
                          $A->{'fred'} => \@C                           
                               $C->[3] => \%B                           
                   w-> $B->{'phyllis'} => \%A                       

=head1 DESCRIPTION

This is a simple developer's tool for finding circular references in
objects and other types of references.  Because of Perl's
reference-count based memory management, circular references will
cause memory leaks.

=head2 EXPORT

The find_cycle() and find_weakened_cycle() subroutine are exported by default.

=over 4

=item find_cycle($object_reference,[$callback])

The find_cycle() function will traverse the object reference and print
a report to STDOUT identifying any memory cycles it finds.

If an optional callback code reference is provided, then this callback
will be invoked on each cycle that is found.  The callback will be
passed an array reference pointing to a list of lists with the
following format:

 $arg = [ ['REFTYPE',$index,$reference,$reference_value],
          ['REFTYPE',$index,$reference,$reference_value],
          ['REFTYPE',$index,$reference,$reference_value],
           ...
        ]

Each element in the array reference describes one edge in the memory
cycle.  'REFTYPE' describes the type of the reference and is one of
'SCALAR','ARRAY' or 'HASH'.  $index is the index affected by the
reference, and is undef for a scalar, an integer for an array
reference, or a hash key for a hash.  $reference is the memory
reference, and $reference_value is its dereferenced value.  For
example, if the edge is an ARRAY, then the following relationship
holds:

   $reference->[$index] eq $reference_value

The first element of the array reference is the $object_reference that
you passed to find_cycle() and may not be directly involved in the
cycle.

If a reference is a weak ref produced using Scalar::Util's weaken()
function then it won't contribute to cycles.

=item find_weakened_cycle($object_reference,[$callback])

The find_weakened_cycle() function will traverse the object reference and print
a report to STDOUT identifying any memory cycles it finds, I<including> any weakened
cycles produced using Scalar::Util's weaken().

If an optional callback code reference is provided, then this callback
will be invoked on each cycle that is found.  The callback will be
passed an array reference pointing to a list of lists with the
following format:

 $arg = [ ['REFTYPE',$index,$reference,$reference_value,$is_weakened],
          ['REFTYPE',$index,$reference,$reference_value,$is_weakened],
          ['REFTYPE',$index,$reference,$reference_value,$is_weakened],
           ...
        ]

Each element in the array reference describes one edge in the memory
cycle.  'REFTYPE' describes the type of the reference and is one of
'SCALAR','ARRAY' or 'HASH'.  $index is the index affected by the
reference, and is undef for a scalar, an integer for an array
reference, or a hash key for a hash.  $reference is the memory
reference, and $reference_value is its dereferenced value. $is_weakened
is a boolean specifying if the reference is weakened or not. For
example, if the edge is an ARRAY, then the following relationship
holds:

   $reference->[$index] eq $reference_value

The first element of the array reference is the $object_reference that
you passed to find_cycle() and may not be directly involved in the
cycle.

=back

=head2 Cycle Report Formats

The default callback prints out a trace of each cycle it finds.  You
can control the format of the trace by setting the package variable
$Devel::Cycle::FORMATTING to one of "raw," "cooked," or "roasted".

The "raw" format prints out anonymous memory references using standard
Perl memory location nomenclature.  For example, a "Foo::Bar" object
that points to an ordinary hash will appear in the trace like this:

	Foo::Bar=HASH(0x8124394)->{'phyllis'} => HASH(0x81b4a90)

The "cooked" format (the default), uses short names for anonymous
memory locations, beginning with "A" and moving upward with the magic
++ operator.  This leads to a much more readable display:

        $Foo::Bar=B->{'phyllis'} => \%A

The "roasted" format is similar to the "cooked" format, except that
object references are formatted slightly differently:

	$Foo::Bar::B->{'phyllis'} => \%A

If a reference is a weakened ref, then it will have a 'w->' prepended to
it, like this:

	w-> $Foo::Bar::B->{'phyllis'} => \%A

For your convenience, $Devel::Cycle::FORMATTING can be imported:

       use Devel::Cycle qw(:DEFAULT $FORMATTING);
       $FORMATTING = 'raw';

Alternatively, you can control the formatting at compile time by
passing one of the options -raw, -cooked, or -roasted to "use" as
illustrated here:

  use Devel::Cycle -raw;

=head2 Code references (closures)

If the PadWalker module is installed, Devel::Cycle will also report
cycles in code closures. If PadWalker is not installed and
Devel::Cycle detects a CODE reference in one of the data structures,
it will warn (once per data structure) that it cannot inspect the CODE
unless PadWalker is available. You can turn this warning off by
passing -quiet to Devel::Cycle at compile time:

 use Devel::Cycle -quiet;

=head1 SEE ALSO

L<Test::Memory::Cycle>
L<Devel::Leak>
L<Scalar::Util>

=head1 AUTHOR

Lincoln Stein, E<lt>lstein@cshl.eduE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2003 by Lincoln Stein

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.2 or,
at your option, any later version of Perl 5 you may have available.


=cut