Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

psychorigid.pm or how to make Perl better ?

by arhuman (Vicar)
on Oct 21, 2001 at 22:12 UTC ( [id://120392]=perlmeditation: print w/replies, xml ) Need Help??

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)

Replies are listed 'Best First'.
Re (tilly) 1: psychorigid.pm or how to make Perl better ?
by tilly (Archbishop) on Oct 22, 2001 at 02:00 UTC
    My personal attitude is that if you are planning on having a large project, one of the following statements is true:
    1. You are ready for your probable failure.
    2. You have specific reasoning for why what is true for the rest of the industry is not true for you.
    3. You are ignorant. (The most likely option.)
    4. You are an idiot. (The second most likely option.)
    This may sound extreme. I don't believe it is. As I pointed out at RE (tilly) 3 (disaster): Java vs. Perl from the CB, the statistics for failure in software development are so bad it is simply absurd. And the larger the project is, the worse its odds get.

    Now this doesn't mean that there are not occasionally large projects that need to be done, despite the horrible odds. But it does strongly suggest that if you can avoid things that lead to large projects, you should. And while Java has features which are intended to help in large projects, it also takes a lot more Java to do the same thing as a little Perl...

    UPDATE
    In case it wasn't obvious, this is a response to gloom's argument that we "NEED" Java for its features which are meant to make large projects easier. For an alternate response to the same point, see Java Is Untyped. As for the feature, I think that asserts are a very useful strategy. Anything that makes them easier for people to use is good.

Re: psychorigid.pm or how to make Perl better ?
by traveler (Parson) on Oct 22, 2001 at 00:57 UTC
    I may not be understanding what you are talking about here, but what does this provide over Attribute::Types, Scalar::Properties and the interesting module Hook::WrapSub that could be part of a tool to actually check function args and returns at run time?

    Just wondering -- traveler

      I'd suggest Hook::LexWrap over Hook::WrapSub.
      And not just because I wrote Hook::LexWrap. ;-)

      Hook::WrapSub can change the semantics of a wrapped subroutine in subtle ways (in particular, caller returns different values).

        Based on the docs, I'd agree, I'd not heard of Hook::LexWrap: I must have missed it when it came up on the CPAN nodelet. Thanks for filling me in on that. I'll probably play with it soon as it could be very useful to me.

        Thanks, --traveler

      what does this provide over Attribute::Types, Scalar::Properties and the interesting module Hook::WrapSub ?

      Psychorigid wasn't thought to be better !
      To be honest I even didn't know most of the modules you're talking about...
      (I know, shame on me ;-)

      However after browsing through their doc, I think I should make clear why psychorigid is different to my mind :

      Psychorigid goal is to provide a way to check code without modyfing it.
      I mean, there must be no difference between a code using psychorigid and a standard code.
      (Ok, I'm cheating on this, as I'm modifying comments...)

      What I wanted was a module that I could activate at the debugging phase to get extra checks
      and desactivate before sending it to my production server
      (by just adding/removing the 'use psychorigid' line...)

      Anyway, thanx again for the usefull modules list...

      "Only Bad Coders Code Badly In Perl" (OBC2BIP)
Re: psychorigid.pm or how to make Perl better ?
by iakobski (Pilgrim) on Oct 22, 2001 at 17:37 UTC
    Perl does have types, they're just not int, byte, char, etc. Perl's types are far more useful: ARRAY, HASH, SCALAR and REGEXP, etc.

    I agree that having a framework for assertions and encouraging asserting everywhere is good, there should be no need for checking whether a value resolves to a string that matches a regex for a primitive type.

    There is a certain mindset of programmer that needs the boundaries of Java or equivalent. Such people should never be given the freedom of Perl (bitter experience of supporting systems written by this sort of programmer).

    But Perl is Perl - use Perl types and assert for YOUR data design, not some arbitrary sizes set by some hardware.

    -- iakobski

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://120392]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found