Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Class::Struct, Class::Contract and type Safety

by baku (Scribe)
on Feb 14, 2001 at 19:51 UTC ( #58362=note: print w/replies, xml ) Need Help??

in reply to Class::Struct, Class::Contract and type Safety

Perhaps (probably) I don't rightly understand the question... but in my experience, the very fact that Perl is "weakly" typed has been a boon in large projects. For example, one current job I'm working on has over 500 separate small scripts (automated jobs working with various databases to perform specific functions) as well as several multi-thousand-line application "scripts," being maintained by as many as 50 people at any time. Since we don't have to worry about whether a subroutine expects a int or a long int, development has been remarkably easy.

That said, every named subroutine we have does perform checking, not upon the "type" of the data, but upon its "domain." For example:

sub member_bleh { my $self = shift; $self->validate(); # will die() if data is invalid if (@_) { my $value = shift; if ($value < 15 or $value > 143.51) { die ("$value out of range (15 .. 143.51) in member_bleh"); } $self->{member_bleh} = $value; } return $self->{member_bleh}; }

It looks like Class::Contract could be a useful tool to perform this sort of data validation, but relying upon "types" as a validity test (at least primitive types) is a very dangerous thing. OTOH, Perl does allow you to check object references, so if you accept data in the form of an object which contains already-validated data, you do have a "strong typing" system to work with: die "not Right::Class: $ref" unless ( ref $ref && $ref->isa("Right::Class") ); is one such example...

Hope that is on task :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://58362]
[LanX]: interpolation! like in a "string"
[Lady_Aleena]: LanX, I just never thought I would be explaining perl regex to others. It is usually others explaining why a regex isn't working to me.
[Discipulus]: congratulations shmem! (was my last upvote? ;=)

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2017-04-28 09:51 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (520 votes). Check out past polls.