Taking things in backwards order:

After a quick glance at your code there are a few things I'd change. First off, make it OO. That way you don't need to pass back a mess of data that your client is just going to have to pass back in elsewhere. It also makes dealing with PDL/non-PDL handling tidier.

Handle errors using exceptions (die and eval). That makes dealing with errors in an appropriate place much easier for your code and for the caller's code.

With OO code helper functions are just something provided by the object.

A partial implementation of your code as OO might look like:

use strict; use warnings; package Sport::Analytics::SRS; return 1; # madule returns true for successfull load sub new { my ($class, %params) = @_; # parameter validation here. die on failure. # Test for PDL $params{havePDL} = eval { require 'PDL'; PDL->import (); 1; }; return bless \%params, $class; } sub srs_full_matrix { my ($self, %options) = @_; die "The module PDL must be available for srs_full_matrix use." if !$self->{havePDL}; $options{epsilon} ||= 0.001; $options{maxiter} ||= 1000000; $self->{srs} = $self->{mov}->copy (); my $oldsrs = $self->{srs}->copy (); my $delta = 10.0; my $iter = 0; while ($delta > $options{epsilon} and $iter < $options{maxiter}) { my $wt = 1.0 / sumover ($self->{played}); my $prod = $self->{srs} x $self->{played}; my $sos = $wt * $prod; $self->{srs} = $self->{mov} + $sos; $delta = max (abs ($self->{srs} - $oldsrs)); $oldsrs = $self->{srs}->copy (); $iter++; } print "iter = $iter\n" if $options{debug}; print "epsilon = $options{epsilon}\n" if $options{debug}; printf "delta = %7.4f\n", $delta if $options{debug}; my $offset = sum ($self->{srs}); $self->{srs} -= ($offset / $self->{mov}->nelem); return $self->{srs}->slice (":,(0)"); } sub loadData { my ($self, $games) = @_; for (@$games) { my ($visitor, $visit_score, $home_team, $home_score) = split " +\,", $_; my $diff = $home_score - $visit_score; $self->{team}{$visitor}{games_played}++; $self->{team}{$home_team}{games_played}++; $self->{team}{$visitor}{points} -= $diff; $self->{team}{$home_team}{points} += $diff; push @{$self->{team}{$visitor}{played}}, $home_team; push @{$self->{team}{$home_team}{played}}, $visitor; } my $total_team = scalar keys %{$self->{team}}; $self->{played} = zeroes ($total_team, $total_team); $self->{mov} = zeroes ($total_team); my $ii = 0; for (sort keys %{$self->{team}}) { my $team_diff = $self->{team}{$_}{points} / $self->{team}{$_}{games_played +}; $self->{team_map}{$_} = $ii; $self->{mov}->set ($ii, $team_diff); $ii++; } for (keys %{$self->{team}}) { my $i = $self->{team_map}{$_}; for my $opp (@{$self->{team}{$_}{played}}) { my $j = $self->{team_map}{$opp}; my $a = $self->{played}->at ($i, $j); $a++; $self->{played}->set ($i, $j, $a); } } }

so the module might be used like:

use Sport::Analytics::SRS; eval { my $analysis = Sport::Analytics::SRS->new (); $analysis->loadData (); $analysis->fullMatrix (); ... } or do { die "Analysis failed: $@\n"; };
True laziness is hard work

In reply to Re: From test harness to CPAN by GrandFather
in thread From test harness to CPAN by dwm042

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.