in reply to Unlimited chaining (is there a way to detect this?)

First, the idea that an instance method might actually return a different instance of the same class is actually useful. For example, I use this technique in my File::Finder.

Second, what do you mean 'detect it'? To me, your question reads like: "Oh, when I loop like this, it loops forever! How do I stop it:"

while (1) { ... }
The way to stop it is to stop doing it. You're the programmer. You're in charge. Don't do stupid things.

Replies are listed 'Best First'.
Re^2: Unlimited chaining (is there a way to detect this?)
by KurtSchwind (Chaplain) on Jan 06, 2008 at 02:39 UTC

    I'm not sure you can say that.

    If he provides a module for others to use, he's not necessarily the one doing the weird looping call and so he might want to detect it. So the question is valid, from my perspective. What's the best method for detecting this looped method calling?

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
      What's the best method for detecting this looped method calling?

      Code reviews.

        Hey that sounds great. Only one issue.

        Picture this scenario: You create a module and put it up on CPAN. How do you propose to get all of your subsequent users to submit to a code review? I realize that the core of this isn't the problem for the author of a module. People will do all sorts of inane and dumb things with downloaded modules. So is the best method for detecting this a code review?

        --
        I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
Re^2: Unlimited chaining (is there a way to detect this?)
by Burak (Chaplain) on Jan 06, 2008 at 20:33 UTC
    Second, what do you mean 'detect it'?
    Well... To be able to display a warning (or die) like: "Deep recursion on method foo ... " after a certain count. If that makes sense? It was just a thought :) actually re-designing the code like this solves this issue (yes, I've stopped doing it):
    package NewBase; use strict; sub new { my $class = shift; my $self = {}; bless $self, $class; $self; } # other methods package BaseClass; use strict; use base qw(NewBase); sub bar { return Foo->new; } package Foo; use strict; use base qw(NewBase);
    However, the situation about this method chaining seemed interesting to me and I opened a discussion about it.