September 22, 1999 Strong Typing Slide #37

Compile-Time Type Checking

Octopus.pm:

           package Octopus;
           use fields qw(tentacles hearts favorite_food);
           sub new { ... }

main.pl:

           use Octopus;
           my $fenchurch = new Octopus;
           my Octopus $junko = new Octopus;         # Note type declaration!
           $fenchurch->{tentacles} = 8;  
           $junko->{tentacles} = 8;  
           # This will generate a *run-time* error:
           $fenchurch->{noses} = 1;

No such array field at ...

           # This will generate a *compile-time* error:
           $junko->{noses} = 1;

No such field "noses" in variable $var of type Octopus at ...


Next Copyright © 1999 M-J. Dominus