package Tool::Box; @ISA = qw(Exporter); @EXPORT = qw(); @EXPORT_OK = qw( Ave Max Min Uniq Stats ); %EXPORT_TAGS = ( ':ALL' => [ qw(Ave Max Min Uniq Stats) ] ); $VERSION = '0.01'; use strict; use warnings; sub Max { my $max; my $find_max = sub { for ( @_ ) { $max = $_ if ! defined $max || $_ > $max; } return $max; }; $find_max->( @_ ); return $find_max; } sub Min { my $min; my $find_min = sub { for ( @_ ) { $min = $_ if ! defined $min || $_ < $min; } return $min; }; $find_min->( @_ ); return $find_min; } sub Ave { my $tot; my $cnt; my $find_ave = sub { $cnt += @_; $tot += $_ for @_; return $cnt ? $tot / $cnt : undef; }; $find_ave->( @_ ); return $find_ave; } sub Uniq { my %uniq; my $find_uniq = sub { @uniq{ @_ } = (); if ( defined wantarray ) { return wantarray ? keys %uniq : scalar keys %uniq; } }; $find_uniq->( @_ ); return $find_uniq; } sub Stats { my $stat = {}; my ($cnt, $max, $min, $tot); $stat->{ADD} = sub { $cnt += @_; for ( @_ ) { $tot += $_; $max = $_ if ! defined $max || $_ > $max; $min = $_ if ! defined $min || $_ < $min; } }; $stat->{MAX} = sub { $max }; $stat->{MIN} = sub { $min }; $stat->{AVE} = sub { $cnt ? $tot / $cnt : undef }; $stat->{TOT} = sub { $tot }; $stat->{ADD}->( @_ ); return $stat; } 42; __END__ =head1 NAME Tool::Box - A hodge podge of useful functions =head1 VERSION Version 0.01 =head1 SYNOPSIS use Tool::Box; use Tool::Box qw(Max Min); use Tool::Box ':ALL'; =head1 DESCRIPTION This module is a collection of commonly used functions to do what you want how you want. =head1 EXPORTS None by default =head1 FUNCTIONS =head2 Stats Function generator that allows you to keep track of max/min/ave/tot use Tool::Box qw(Stats); my $stat = Stats(1 .. 10); print $stat->{TOT}(); # 55 print $stat->{AVE}(); # 5.5 print $stat->{MIN}(); # 1 print $stat->{MAX}(); # 10 for (8 .. 12) { $stat->{ADD}( $_ ); } print $stat->{MAX}(); # 12 print $stat->{TOT}(); # 105 =head2 Ave Function generator that allows you to keep track of average value use Tool::Box qw(Ave); my $ave = Ave(1 .. 10)->(); print $ave; # 5.5 $ave = Ave(); for (4 .. 9) { $ave->( $_ ); } print $ave->(); # 6.5 $ave = Ave(20 .. 30); print $ave->(); # 25 =head2 Max Function generator that allows you to keep track of maximum value use Tool::Box qw(Max); my $max = Max(1 .. 10)->(); print $max; # 10 $max = Max(); for (4 .. 9) { $max->( $_ ); } print $max->(); # 9 $max = Max(20 .. 30); print $max->(); # 30 =head2 Min Function generator that allows you to keep track of minimum value use Tool::Box qw(Min); my $min = Min(1 .. 10)->(); print $min; # 1 $min = Min(); for (4 .. 9) { $min->( $_ ); } print $min->(); # 4 $min = Min(20 .. 30); print $min->(); # 20 =head2 Uniq Function generator that allows you to keep track of unique things use Tool::Box qw(Uniq); my $count = Uniq(1,1,2,3,1,7)->(); print $count; # 4 my @u_nums = Uniq(1,1,2,3,1,7)->(); print join ' ' , @u_nums; # 1 2 3 7, though the order is not guaranteed my $uniq = Uniq(); while ( ) { chomp; $uniq->( $_ ); } my $unique_lines = $uniq->(); =head1 AUTHOR Joshua Gatcomb, =head1 ACKNOWLEDGEMENTS Various people from PerlMonks (L) provided invaluable input. =head1 BUGS Functions that expect numeric arguments are not verifying they are numeric =head1 TO DO =head1 COPYRIGHT Copyright (c) 2004 Joshua Gatcomb. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L(1) =cut