All,
I have this loose idea in my head about taking code re-use down to the snippet level. Presumably, individuals already do this but don't feel it warrants a new module on CPAN. Since there is no end to beginners in the language asking how to do X where the stock reply is to do Y, I was thinking maybe we should do just that. The following is a rough proof of concept:
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 guarant +eed my $uniq = Uniq(); while ( <DATA> ) { chomp; $uniq->( $_ ); } my $unique_lines = $uniq->(); =head1 AUTHOR Joshua Gatcomb, <Limbic_Region_2000@Yahoo.com> =head1 ACKNOWLEDGEMENTS Various people from PerlMonks (L<http://www.perlmonks.org>) provided invaluable input. =head1 BUGS Functions that expect numeric arguments are not verifying they are num +eric =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<perl>(1) =cut

Yes, I know that some of these functions already exist in List::Util. I chose them because the standard wheel doesn't always fit your bicycle (values are not all available at once). I got the idea from one of tmoertel's posts. I want to stress that this post is about the idea in general and not this specific proof of concept.

So is this a good idea or not? I was thinking along the lines of going through the FAQs and providing ready made solutions (such as getting the intersection of two arrays). I can see it going both ways: if beginner's don't learn fundementals where will the hand-holding end? If it is a good idea, what should be included? Besides going through the FAQs I was also thinking about going through Snippets.

Cheers - L~R

Updated 2004-11-16: I changed the all cap function names, as it is something I normally don't do anyway, after a couple of comments against them.


In reply to RFC: Tool::Box by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.