Hi. I'm looking for a universal way to declare a test flag to object oriented code regardless if it's Moose or traditional Perl OO, or whatever, and although rare, even if the blessed reference is an array. I'm pretty sure I'm reinventing the wheel here so the code below is merely to illustrate what is wanted.

Anyway, the idea is that from a test script one could can call this method and it would set a test flag that can later be used in the actual code to condition things or in cases the code needs to know it's being tested. So, the test script would call _test_mode to set the attribute and then the actual code will use _test_mode to condition things (e.g. do foo unless($self->_test_mode)). This could perhaps be added as a simple sub in in modules that need to be test-aware:

sub _test_mode { my $self = shift; use Scalar::Util 'reftype'; my $tkey = '_'.__PACKAGE__.'_TEST_MODE'; if(reftype($self) eq 'HASH'){ if(defined $self->{$tkey}){ return 1; } else{ $self->{$tkey} = 1; } } elsif(reftype($self) eq 'ARRAY'){ if($self->[-1] eq $tkey){ return 1; } else{ push @{$self},$tkey; } } }

There are probably dozens of ways to do this, and more that one published on the CPAN, so I ask for your Wisdom not so much on the code above but rather on recommendations on techniques to accomplish the objective of setting a universal flag in the code so it's aware of being tested, regardless if we are testing a module built with Moose, or an extension to some DBIx::Class:ResultSet.


In reply to Universal test flag by ait

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.