Reini Urban
and 1 contributors


types - Perl pragma for type checking and optimizations


  use types;
  my int $int;
  my double $float;
  $int = $float; # BOOM compile time Type mismatch error


This pragma does compile time type checking.

It also permits internal compiler optimizations, and adds type attribute definitions.


    my double $foo = "string"; #compile time error
    sub foo (int) { my ($foo) = @_ };
    sub foo (int $foo) { my ($foo) = @_ };
    foo("hi"); #compile time Type mismatch error

    my int $int;
    sub foo { my double $foo; return $foo }
    $int = $foo; # compile time Type mismatch error

    my int @array = (0..10); # optimized internal representation
    $array[2] = '2'; # compile time Type mismatch error

    my string %hash : const = (foo => any, bar => any); # optimized representation
    print $hash{foo}; # faster O(1) lookup
    $hash{new} = 1;   # compile time Type mismatch error


This pragma uses the optimize module to analyze the optree and turns on compile-time type checking, using my,our and sub type declarations.

It is also the base for optimizing compiler passes, for perl CORE (planned) and for B::CC compiled code (done).

Currently we support SCALAR lexicals with the type classes int, double, number, string and user defined classes for subroutine prototypes.

The implicit casting rules are as follows:

    int    < > number
    int      > double
    double < > number
    number   > string

Normal type casting is allowed both up and down the inheritance tree, so in theory user defined classes should work already, requires one to do use base or set @ISA at compile-time in a BEGIN block.

Implemented are only SCALAR types yet, not ARRAY, not HASH.


To weaken strict compile-time type checks we can add attributes. This also allows to get away with less base types. Typically attributes are added later to allow compilation.

Planned type attributes are int, double, string, unsigned, ro and readonly, eventually also register, temporary.

The attributes are perl attributes, and int|double|string are either classes or hints for more allowed types.

  C<my int $i :double;>  declares a NV with SVf_IOK. Same as C<my $i:int:double;>?
  C<my int $i;>          declares an IV. Same as C<my $i:int;>?
  C<my int $i :string;>  declares a PVIV. Same as C<my $i:int:string;>?

  C<my int @array :unsigned = (0..4);> Will be used as c var in faster arithmetic and cmp.
                                        With :readonly or :ro even more.
  C<my string %hash : readonly = (foo => any, bar => any);> declare string keys only
                  and may be generated as read-only perfect hash.

unsigned is valid for int only and declares an UV. This helps e.g. i_opt with bit manip.

ro and readonly throw a compile-time error on write access and may optimize the internal structure of the variable. E.g. In CORE hashes may be optimized to perfect hashes for faster lookup. Array key lookup for int and double will be optimized. In B::CC we don't need to write back the variable to perl (lexical write_back).

register denotes optionally a short and hot life-time. for loop variables are automatically detected as such.

temporary are usually generated internally with nameless lexicals. They are more aggressivly destroyed and ignored.


OK (classes only):

  my int $i;
  my double $d;

NOT YET OK (attributes with my, only our works):

  my int $i :register;
  my $i :int;
  my $const :int:ro;
  my int $uv :unsigned;

  my int @array;
  my string %hash;
  my int $arrayref = [];
  my int $hashref = {};

Return values

Return values are implicitly figured out by the subroutine, this includes both falling of the end or by expliticly calling return, if two return values of the same sub differ you will get an error message.


Arguments are declared with prototype syntax, they can either be named or just typed, if typed only the calling convertions are checked, if named then that named lexical will get that type without the need for explicitly typing it, thus allowing list assignment from @_.




Please report bugs and submit patches using


optimize B::Generate "TYPES" in B::CC typesafety Moose


Artur Bergman, <ABERGMAN@CPAN.ORG> 2002 (type checks) Reini Urban, <RURBAN@CPAN.ORG> 2011 (type attrs and type optim)


Copyright 2002 by Artur Bergman Copyright 2011 by Reini Urban

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.