Greetings monks,

I have a possible solution to a problem but I'm not sure that it is the most efficient (or correct!) way to go about it. Here's the problem. I've got some code which looked like this:

my %data_h = (previously calculated data); my @errors; # store errors here my @passed; # list items that passed the tests foreach (@list) { if ($condition{'a'} == 1) { # perform test A on $_ using data from %data_h # if it fails, add an error message and go on to # the next list item } if ($condition{'b'} == 1) { # perform test B on $_ using data from %data_h # if it fails, add an error message and go on to # the next list item } # and so on, with several other tests # if it survives all these tests... push @passed, $_; } if (@passed) { # etc.

I'm sure that it would be better to create a subroutine beforehand which could be customised according to what tests were required. This is my attempt to create such a sub:

# create some dummy subroutines sub test_A { } sub test_B { } # redefine them as real subs if the conditions # are right local *test_A = sub { my $x = shift; # perform test A on $x using data from %data_h # if it fails, add an error message and go on to # the next list item } if $condition{'a'} == 1; local *test_b = sub { my $x = shift; # perform test B on $x using data from %data_h # if it fails, add an error message and go on to # the next list item } if $condition{'b'} == 1; sub do_tests { my $to_test = shift; # depending on what is in the %condition hash, # these will either be a real subroutine or just a dummy test_A($to_test); test_B($to_test); } foreach (@list) { do_tests($_); push @passed, $_; }

This seems kinda clunky, and I also get warnings about the subroutines being redefined. Is there a better way of doing this?

Thanks a lot in advance for any advice!


In reply to subroutines - creating a custom subroutine and 'subroutine redefined' errors by Anonymous Monk

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.