in reply to Re^2: Method chain object with easy syntax
in thread Method chain object with easy syntax
the scalar itself is fetched only once
Is that guarenteed? If it is I almost withdraw my opinion. I say almost, because I still think it's unnecessary complicated (in that it uses a global as loop variable). If you change
to the not completely unusual constructfor my $pair (@$chain) {
for whatever reason it'll demonstrate why I think this is playing with fire -- you have an extra thing to keep in mind two years later when you patch the code. As you say yourselfwhile (@$chain) { my $pair = shift @$chain;
The important point is that the contents of the array it points to at start doesn't changeThese's a general concensus that using globals like this is A Bad Thing, even if it's not such an obvious case. Not only do you have to remember that directly inside the loop not change @$chain, but you can't get the idea to assign to @$chain in &__Set_MethodChain__. That's a lot of unnecessary conditions just to not copy a presumably small array. If you copy the array you don't have anything extra to worry about and that might save yourself from future troubles. Indeed, the code works, I just get a bad feeling when I see it. :-)
if you want to build a chain of methods without the length of it known in advance, then the sub solution can be more difficult
You can easily compose new chains using other "sub chains", if that's what you're talking about.
Maybe I misunderstood?#!/usr/bin/perl -wl AUTOLOAD { print $::AUTOLOAD =~ /.*::(.*)/s; $_[0]; } my $chain1 = sub { $_[0]->m2->m3 }; my $chain2 = sub { $_[0]->m1->$chain1->m4->m5 }; main::->$chain2; __END__ m1 m2 m3 m4 m5
ihb
See perltoc if you don't know which perldoc to read!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Method chain object with easy syntax
by ambrus (Abbot) on Apr 20, 2005 at 20:05 UTC |