Subject: | FEATURE: flag to get standard reporting into scalar for applications that are already using STDOUT |
While you could make your own callback, this patch seems useful to me in any case. Comes with a free test.
Subject: | report_to_scalar.patch |
diff --git a/lib/Devel/Cycle.pm b/lib/Devel/Cycle.pm
index 68e93c1..a8d51de 100644
--- a/lib/Devel/Cycle.pm
+++ b/lib/Devel/Cycle.pm
@@ -20,11 +20,14 @@ our @EXPORT_OK = qw($FORMATTING);
our $VERSION = '1.12';
our $FORMATTING = 'roasted';
our $QUIET = 0;
+our $REPORT_TO_SCALAR = 0;
my %import_args = (-quiet =>1,
-raw =>1,
-cooked =>1,
- -roasted=>1);
+ -roasted=>1,
+ -report_to_scalar=>0,
+);
BEGIN {
require constant;
@@ -43,31 +46,36 @@ sub import {
$FORMATTING = 'roasted' if exists $args{-roasted};
$FORMATTING = 'raw' if exists $args{-raw};
$FORMATTING = 'cooked' if exists $args{-cooked};
+ $REPORT_TO_SCALAR = 1 if exists $args{-report_to_scalar};
$self->export_to_level(1,$self,grep {!exists $import_args{$_}} @_);
}
sub find_weakened_cycle {
my $ref = shift;
my $callback = shift;
+ my $report;
unless ($callback) {
my $counter = 0;
$callback = sub {
- _do_report(++$counter,shift)
+ $report .= _do_report(++$counter,shift)
}
}
_find_cycle($ref,{},$callback,1,{},());
+ return $report if $REPORT_TO_SCALAR;
}
sub find_cycle {
my $ref = shift;
my $callback = shift;
+ my $report;
unless ($callback) {
my $counter = 0;
$callback = sub {
- _do_report(++$counter,shift)
+ $report .= _do_report(++$counter,shift);
}
}
_find_cycle($ref,{},$callback,0,{},());
+ return $report if $REPORT_TO_SCALAR;
}
sub _find_cycle {
@@ -182,12 +190,19 @@ sub _find_cycle_CODE {
sub _do_report {
my $counter = shift;
my $path = shift;
- print "Cycle ($counter):\n";
+ my $report;
+ $report .= "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));
+ $report .= sprintf("\t%30s => %-30s\n",($is_weak ? 'w-> ' : '')._format_reference($type,$index,$ref,0),_format_reference(undef,undef,$value,1));
+ }
+ $report .=print "\n";
+ if (! $REPORT_TO_SCALAR) {
+ print $report;
+ }
+ else {
+ return $report;
}
- print "\n";
}
sub _format_reference {
@@ -420,6 +435,12 @@ illustrated here:
use Devel::Cycle -raw;
+If you are using Devel::Cycle for an application that's already making use
+of STDOUT you can use the -report_to_scalar flag to return the report
+rather than having it print or having to tie STDOUT.
+
+ use Devel::Cycle -report_to_scalar;
+
=head2 Code references (closures)
If the PadWalker module is installed, Devel::Cycle will also report