lhp@nereida:~/Lperl/src/testing$ cat -n ciclos.pl 1 #!/usr/local/bin/perl -w 2 use strict; 3 use Devel::Cycle; 4 my $test = { 5 fred => [qw(a b c d e)], 6 ethel => [qw(1 2 3 4 5)], 7 george => {martha => 23, agnes => 19} 8 }; 9 $test->{george}{phyllis} = $test; 10 $test->{fred}[3] = $test->{george}; 11 $test->{george}{mary} = $test->{fred}; 12 find_cycle($test); 13 exit 0; lhp@nereida:~/Lperl/src/tAl ejecutar produce una salida como esta:
lhp@nereida:~/Lperl/src/testing$ ./ciclos.pl Cycle (1): $A->{'fred'} => \@B $B->[3] => \%C $C->{'mary'} => \@B Cycle (2): $A->{'fred'} => \@B $B->[3] => \%C $C->{'phyllis'} => \%A Cycle (3): $A->{'george'} => \%C $C->{'mary'} => \@B $B->[3] => \%C Cycle (4): $A->{'george'} => \%C $C->{'phyllis'} => \%A
El módulo permite comprobar la existencia de ciclos con referencias débiles mediante la función find_weakened_cycle . El programa:
lhp@nereida:~/Lperl/src/testing$ cat -n ciclosweaken.pl 1 #!/usr/local/bin/perl -w 2 use strict; 3 use Scalar::Util qw(weaken); 4 use Devel::Cycle; 5 my $test = { 6 fred => [qw(a b c d e)], 7 ethel => [qw(1 2 3 4 5)], 8 george => {martha => 23, agnes => 19} 9 }; 10 $test->{george}{phyllis} = $test; 11 $test->{fred}[3] = $test->{george}; 12 $test->{george}{mary} = $test->{fred}; 13 weaken($test->{george}->{phyllis}); 14 find_weakened_cycle($test); 15 exit 0;
Produce la salida:
lhp@nereida:~/Lperl/src/testing$ ciclosweaken.pl Cycle (1): $A->{'fred'} => \@B $B->[3] => \%C $C->{'mary'} => \@B Cycle (2): $A->{'fred'} => \@B $B->[3] => \%C w-> $C->{'phyllis'} => \%A Cycle (3): $A->{'george'} => \%C $C->{'mary'} => \@B $B->[3] => \%C Cycle (4): $A->{'george'} => \%C w-> $C->{'phyllis'} => \%A
Casiano Rodríguez León