Hi Monks,
I'm Reading Randal's, Perl Object, References & Modules which I just got and am using it to convert my 'stuff a Package keyword at the top and change the .pl to .pm' style package to be CPAN compliant (Great book btw for those who haven't read it yet).
Things are moving along quite nicely and I have a large number of cases to test the things that should work and even the middle as Randal refers to it (using the make test (t/*.t) ). Now I'm trying to test the edges and make sure that the things that should not, do not and I'm looking to bolster the initial code in my interface subroutines such that they are not phased by boundary conditions. This made me realize that my code itself does not check for many boundary conditions and as such I'd like to fix that.

The three types of cases I want to cover are;
1) Insufficient arguments
2) Too many arguments
3) Bad argument types

What I'm looking for is the most minimal code such that you can achieve something like this; i.e.

sub Func1 { do we have a num_var; do we have a name_var; do we have params (optional) is the num_var of number format is the name_var of name format .... other code } Func1($num_var, $name_var, %params);
I know this is a little generic, but lets say most subroutines can take a generic number style argument and others take a generic string type argument I want to make sure thats all they are.
This will no doubt take the form of a reg exp checking its just a string (i.e. no :;"' etc. in it) and similarly for the number.
I'm sure there are *many* well written modules that take care of boundary conditions so what I'm wanting is the 'standard' code for doing these 'generic' tests so to speak i.e. the best practice bit of code for doing this? Do people generally generally just go;
Func1 { is number of args correct? check arg1 is a number and only a number with reg exp? check arg2 is a string and only a string with reg exp? ... other code }
or are there more elegant solutions that are generally used...something like?
sub CheckArgs { get number of args get args to check check we have the right number of args foreach arg check the type with the appropriate reg exp. return status. } sub Func1 { CheckArgs (2, @_, "NUM", "STRING"); .... other code } sub Func2 { CheckArgs (3, @_, "STRING", "NUM", "STRING"); .... other code }

Is the second sort of approach considered to weighty if you are wanting the code to be quite fast or is just checking each argument individually in each subroutine generally the practice as in there are no 'generic' inputs? I would appreciate any insights or code snippits on how others have satisfied boundary conditions.

Regards Paul.
Update: If you want an example subroutine to write something around here is one...
In this case it would just be $name that would need to be checked, and that we have a name...I'm just trying to think generically though.
sub add_output { #Create an unconnected named output my $self = shift; my $graph = $self->{graph}; my $name = shift; my %params = @_; #Check for valid options foreach my $p ( keys %params ) { if ( !exists $valid_userconfigurable_vertex_params->{$p} ) { Carp::confess(sprintf "Unknown Option : $p"); } } #Set the activation function my $activation; if ( exists $params{activation} ) { $activation = $params{activation}; } else { $activation = "SIGMOID"; } #Create the vertex $graph->set_vertex_attributes($name, { id => GetNewVertexNumb +er($self), added_a_vertex => 1, allow_inputs => "Yes", allow_outputs => "No", activation => $activat +ion, value => 0 }); #Connect vertex to the bias vertex $graph->add_edge("BIAS", $name); #An update to the layout is required $graph->set_graph_attribute("update_layout", 1); }

2005-03-30 Janitored by Arunbear - added readmore tags, as per Monastery guidelines


In reply to Testing Edges by thekestrel

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.