Now I'm off home to enjoy the marvel that is all-day-breakfast-in-a-can while trying to read (and hopefully understand) Engineering Mathematics (I'm shooting for one out of two
broquaint out
On a more perl-related note (well it at least relates to perl to some degree) I'm finally getting round to learning how to write tests for modules great and small. Much to my pleasant surprise it's not as tricky as I may have been lead to believe. <remark tone="mildly ironic">Testing - I can hardly see what all the fuss is about.</remark>
Many thanks go out to demerphq for giving me the chance to get involved in the development process. So far so good
broquaint out
#!/usr/bin/perl
package MySub;
use strict;
use Regexp::Common;
my $brackets_re = $RE{balanced}{-parens => '{}'};
my $paren_re = $RE{balanced}{-parens => '()'};
my $sub_name_re = qr/[_a-zA-Z](?:\w+)?/;
my $sub_match_re = qr/my\s+sub\s+($sub_name_re)\s*($brackets_re)\s*;?/x;
# my sub foobar { "code" }
# my # 'my'
# \s+ # 1> space
# sub # 'sub'
# \s+ # 1> space
# ($sub_name_re) # '$subname'
# \s* # 0> space
# ($brackets_re) # balanced {}
# \s* # 0> space
#;? # optional ';'
use Filter::Simple;
my @subs;
# FILTER_ONLY code => sub {
FILTER {
my $code = $_;
study $code;
while(my($subname, $subcode) = $code =~/$sub_match_re/s) {
push @subs, {
subname => $subname,
code => $subcode
};
# 'my sub name {}' => 'my $name = sub {};'
$code =~ s/$sub_match_re/my \$$1 = sub $2;\n/s;
# '&name();' => '$name->();'
$code =~ s/
&? # optional &
$subname # '$subname'
\s* # 0> whitespace
( # group $1
$paren_re # balanced parens
)? # optional group $1
\s* # 0> whitespace
; # ';'
/"\$$subname->" . ($1 || '()') . ';'/sex;
}
$_ = $code;
};
qw(package activated);
Although the code is a little rough, it seems to DWIM so far. I haven't done any extensive testing (note to self - learn how to use test suites) but I havent found any problems as of yet. Once it's tidyed up a bit, I might even stick it on CPAN depending on the peoples' need for such an extension.
broquaint out