Few months ago I had a talk with
gloom,
he was trying to convince me that JAVA was
NEEDED to handle large projects...
I disagree (of course ;-) to this point of view, I believe that JAVA helps a lot,
but a good coding discipline can make it posible to code a large project in Perl.
For my part, I still prefer to see people code properly beccause they understand
what they do
rather than beccause it's the only way allowed by the language.
We exchanged our ideas and
Gloom made a point by talking about
type checking.
As far as we know, there were no EASY way to do type checking in Perl,
and it's often a useful feature
(at least in the first stages of the program coding/testing)
However, deep in my heart, I KNEW that it should be possible to do it EASILY in Perl.
The ideas bounced in my minds for some weeks until I remind the Perl's Filter modules...
Although I've never used them, I remember I've seen amazing things made possible via the Filter's Modules...
So I installed the
Filter::Simple modules, read the doc, and one hour later I came up with the 2 following proggies.
They were at first only made as a private 'proof of concept' for
gloom, but I now wonder if it couldn't be more.
That's why I'm asking to the monastery :
- Do you think this module could be useful ?
Not to only check the type the java way, but to make any useful check at the debugging/testing phase.
- What checks could be added to this Filter to further enhance the coding (adding some other checks in addition to those in the strict module)
- Is there people interested in making this module evolve.
Anyway, as it's my filter's firt use, and according to the little time I spent on it,
I probably made huge design errors, please feel free to correct me or give hints/advices...
I've also got a lot of other questions :
- Shouldn't we allow to pass an hash containing additional testing code to the filter
( to add some flexibility)
- What should be the behaviour when a check fails ?
Is a warn enough ? Shoud we die ? What additional infos should be reported...
- How could we make the syntax for the type checking less ugly (while remaining legal when the filter isn't used...)
- Do you have a better name than psychorigid for this module ?
(I bet you have...)
- ...
File : psychorigid.pm
package psychorigid;
use Filter::Simple;
use diagnostics;
sub ctype {
my @ctypes = split /;/,shift;
my $code;
my $argnum=0;
#
# Few tests added, we should probably add A LOT more for a real modul
+e.
#
my %CODE = (
'byte'=>' warn "arg !! ($_[!!]) IS NOT a byte" if $_[!!]!~/^-?\d{1,3
+}$/ || ($_[!!]>127) || ($_[!!]<-128);',
'short'=>' warn "arg !! ($_[!!]) IS NOT a short" if $_[!!]!~/^-?\d{1
+,5}$/ || ($_[!!]>32767) || ($_[!!]<-32768);',
'int'=>' warn "arg !! ($_[!!]) IS NOT an int" if $_[!!]!~/^-?\d{1,10
+}$/ || ($_[!!]>2147483647) || ($_[!!]<-2147483648);',
'char'=>' warn "arg !! ($_[!!]) IS NOT a char" if $_[!!]!~/^\w$/;',
);
foreach my $ctype (@ctypes) {
my $val=$CODE{$ctype};
$val=~s/!!/$argnum/g;
$argnum++;
$code.=$val."\n";
}
return $code;
}
sub check {
my $func_decl = shift;
my $check_code = ctype(shift);
return "$func_decl {\n$check_code"
}
FILTER {
s/(sub\s+.+?)\s*{\s*#\s*!([^!]*)!/check($1,$2)/e;
#
# Uncomment the following line to see the changes applied by the filt
+er
#
#print "\n$_\n";
};
1;
File test.pl
#!/usr/bin/perl
#
# The following line can be commented and the code
# will still be valid.
#
use psychorigid; # Add additional tests like some psychorigid languag
+e ;-)
sub toto { # !char;int!
print "ok dans toto\n";
}
toto('s','12245666666');
I
"
Only
Bad
Coders
Code
Badly
In
Perl" (OBC2BIP)