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.
In reply to Re^4: RFC: Tool::Box
by Limbic~Region
in thread RFC: Tool::Box
by Limbic~Region
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |