in reply to Unlimited chaining (is there a way to detect this?)
You could store a flag in each object created by the bar method and check that in bar.
#!/usr/bin/perl -w use strict; package BaseClass; use strict; sub new { my $class = shift; my $self = {}; bless $self, $class; $self; } sub bar { return Foo->new; } package Foo; use strict; use Carp; use base qw(BaseClass); sub baz {"Some data"} sub bar { my $self = shift; warn "bar"; croak "don't chain method calls" if $self->{chained}; my $ret = $self->SUPER::bar(@_); $ret->{chained} = 1; return $ret; } package main; use strict; my $o = Foo->new; print $o->bar->bar->bar->bar->bar->bar->bar->bar->bar->bar->bar->bar-> +bar->bar->baz; __END__ bar at -e line 1. bar at -e line 1. don't chain method calls to bar at -e line 0
Unfortunately that breaks encapsulation, but maybe your baseclass provides an api to do it cleanly.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Unlimited chaining (is there a way to detect this?)
by Burak (Chaplain) on Jan 06, 2008 at 20:38 UTC | |
by naikonta (Curate) on Jan 07, 2008 at 07:32 UTC |