scratchpad
neilwatson
<p>I encourage wisdom seekers to present sample data and use [mod://Test::More] in the example code of their question. Let's look at some examples.</p>
<h3>How do I make the regex match?</h3>
<code>
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
my $data = "Some string here";
my $regex = qr/ fancy regex here /mxis;
like( $data, $regex, "Matching my regex" );
done_testing;
</code>
<p>Your code fails, but readers can read this code and run it and make changes that will make it pass.</p>
<h3>Why does my sub return an error?</h3>
<code>
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
sub mysub {
return;
}
ok( mysub(), "Should return true" );
done_testing;
</code>
<h3>Presenting Larger sample data as if you were reading a file line by line.</h3>
<p>Use __DATA__.</p>
<code>
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
my $wanted_matches = 2;
my $actual_matches = 0;
my $regex = qr/ fancy regex here /mxis;
while ( my $line = <DATA> ) {
chomp $line;
if ( $line =~ $regex ){
$actual_matches++;
}
}
ok( $wanted_matches == $actual_matches, "Correct number of matches" );
done_testing;
__DATA__
line one.....
line two.....
....
line ten.....
</code>