An OO style solution: (It prints out all the bad calling chains it found, for each function.)
package func; use strict; sub new { my $self = {}; shift; $self->{CALLED} = []; $self->{MARK} = 0; $self->{NAME} = shift; bless $self; return $self; } sub call { my ($self, $called) = @_; push @{$self->{CALLED}}, $called; } sub mark { my $result = 0; my ($self, $mark, $current_chain) = @_; if ($self->{MARK} != $mark) { $self->{MARK} = $mark; my $called; foreach $called (@{$self->{CALLED}}) { push @{$current_chain}, $called->{NAME}; $result |= $called->mark($mark, $current_chain); } } else { $result = 1; print(join("->", @{$current_chain})."\n"); } pop @{$current_chain}; $self->{MARK} = 0; return $result; } 1; func.pl: use func; use Data::Dumper; use strict; my %funcs; while (<DATA>) { chomp; my ($calling, $called) = split(' '); if (!exists($funcs{$calling})) { $funcs{$calling} = new func($calling); } if (!exists($funcs{$called})) { $funcs{$called} = new func($called); } $funcs{$calling}->call($funcs{$called}); } my $mark = 0; my $func; print "Bad calling chains:\n"; foreach $func (keys %funcs) { $mark ++; my $current_chain = []; push @{$current_chain}, $func; $funcs{$func}->mark($mark, $current_chain); } print "End of bad calling chains\n"; __DATA__ aaa bbb aaa ccc bbb ccc ccc bbb ddd aaa ddd eee testing result Bad calling chains: bbb->ccc->bbb aaa->bbb->ccc->bbb aaa->ccc->bbb->ccc ccc->bbb->ccc ddd->aaa->bbb->ccc->bbb ddd->aaa->ccc->bbb->ccc End of bad calling chains

In reply to Re: Detecting Recursion by pg
in thread Detecting Recursion by YuckFoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.