in reply to Re^3: Method chain object with easy syntax
in thread Method chain object with easy syntax

I agree that iterating on @$chain isn't very clean code, because $chain is a global variable. You can of course make a copy of the array @$chain in the __Call_MethodChain__ function. The change is not difficult, the new sub is this:

sub __Call_MethodChain__ { my $r = $_[0]; my @chain = @$chain; for my $pair (@chain) { my($method, $args) = @$pair{"method", "args"}; $r = $r->$method(@$args); } $r; }

Your point with the while loop is especially valid, because my code doesn't propagate context for a method chain call. The simplest version (the one with blessed subs) doesn't have this bug. The simplest way to fix this is a looped shift like this:

sub __Call_MethodChain__ { my $r = $_[0]; my @chain = @$chain; for (;;) { my $pair = shift @chain; my($method, $args) = @$pair{"method", "args"}; 0 == @chain and return $r->$method(@$args); $r = $r->$method(@$args) ; } }

You are right, the straightforward solution can even handle dynamic building of method chains well, and it also doesn't have the bug I've mentioned above. I can still also say that this was just an experiment I made with perl (or even just some filthy trick to get some noderep).