Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I think that including a test script into the same file as a module (and its pod) is a good idea (I said as much in RFC: Runnable test code integrated into Modules.), and now do this regularly in my own modules. If that test code happened to be useful standalone, then it is serving two possible uses which seems like a good thing to me.

A short example of doing this is a module I originally wrote to see if I could profile regexes Re: Profiling regular expressions. It now looks like this.

E:\Perl\site\lib\My>type #! perl -slw package My::Filter; use Filter::Simple; use Benchmark::Timer; our $t = Benchmark::Timer->new(); FILTER_ONLY regex => sub { $_ = "(?{{ \$My::Filter::t->start('$_$/') }})" . "(?:$_)" . "(?{{ \$My::Filter::t->stop('$_$/') }})"; }, ; sub report{ return $t->report() } return 1 if caller(); use strict; use My::Filter; my $stuff = 'abcdefghijklmnopqrstuvwxyz'; for (1..1000) { if ( $stuff =~ m[pqr] ) { $stuff =~ s/(\G(?:.{3})+?)(?<=...)(.)(.)/$1$3$2/g; } $_ = $stuff; my $OK = 1 if m[pqr]; } print $stuff; print '=' x 20, 'Timing of regexs in ', $0, '=' x 20; print My::Filter::report();

It can be run standalone like this

E:\Perl\site\lib\My>perl Subroutine report redefined at E:/Perl/site/lib/My/ line 16. abcdefghijklmnopqrstuvwxyz ====================Timing of regexs in 2000 trials of pqr (260.000ms total), 130us/trial 5000 trials of (\G(?:.{3})+?)(?<=...)(.)(.) (640.000ms total), 128us/trial

Or used from a script like this.

C:\test>perl -MMy::Filter -e"$s='abc'; print 'ok' if $s =~ m[b]; print + My::Filter::report();" ok1 trial of b (0s total) C:\test>

The only downside is having to switch to the directory where the module lives or specify the full path to in order to invoke it standalone, but there are several ways around that.

Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

In reply to Re: Possible to have a module act as a standalone?? by BrowserUk
in thread Possible to have a module act as a standalone?? by snafu

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (3)
    As of 2019-07-21 05:11 GMT
    Find Nodes?
      Voting Booth?
      If you were the first to set foot on the Moon, what would be your epigram?

      Results (7 votes). Check out past polls.