in reply to Re^3: RFC: Tool::Box
in thread RFC: Tool::Box
This is true if ave is called like print ave(5, 10, -1, 7.3); as the anonymous sub is executed and the value 5.325 is returned - that's the way it was designed. If on the other hand, the code is called like my $ave = ave();, there most certainly is a closure returned - it closes over both $cnt and $tot. I am guessing the ternary use of @_ made you think I was trying to close over something in @_? Perhaps it would have been more clear (and better) if I had not modified the original code and re-written it as:sub ave { my $tot; my $cnt; my $find_ave = sub { $cnt += @_; $tot += $_ for @_; return $cnt ? $tot / $cnt : undef; }; return @_ ? $find_ave->( @_ ) : $find_ave; }
sub ave { my ($cnt , $tot); if ( @_ ) { $cnt += @_; $tot += $_ for @_; return $cnt ? $tot / $cnt : undef; } else { return sub { $cnt += @_; $tot += $_ for @_; return $cnt ? $tot / $cnt : undef; }; } }
Another item - you make the same mistake nearly every unique'ing function makes...
Thanks. While the POD doesn't make any promises like that, I think it would be better off that way.
Cheers - L~R
Updated: I was grumpy when I first replied as I had just woken up. Modified tone slightly to be less combative.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: RFC: Tool::Box
by dragonchild (Archbishop) on Nov 22, 2004 at 13:29 UTC | |
by Limbic~Region (Chancellor) on Nov 22, 2004 at 13:37 UTC | |
by diotalevi (Canon) on Nov 22, 2004 at 13:48 UTC | |
by Limbic~Region (Chancellor) on Nov 22, 2004 at 13:54 UTC | |
by dragonchild (Archbishop) on Nov 22, 2004 at 14:24 UTC | |
by diotalevi (Canon) on Nov 22, 2004 at 15:10 UTC | |
by Limbic~Region (Chancellor) on Nov 22, 2004 at 14:56 UTC |