LanX has asked for the wisdom of the Perl Monks concerning the following question:

Oh All-knowing, All-seeing Trash Heap! :)

It is possible to use a TYPE when declaring a variable  my TYPE $a; ...

... but is it also possible to introspect this "type", kind of a $a->istype("TYPE") or $a->gettype() ?

my mentions fields but all I understood is that it's somehow deprecated since 5.8. (wasn't it meant for immutable hashes ...?)

What's confusing me is that neither ref nor reftype nor Devel::Peek show any signs of this typing, so it must be stored in fields somehow...

Bonus points if you can tell me:

Is it even possible to create compile time checks of types?

EDIT

I remember being capable to create errors after such "typing", but can't reproduce.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re: Checking TYPEs after my declaration
by dave_the_m (Monsignor) on Aug 18, 2016 at 18:42 UTC
    As far as I'm aware, there isn't currently a supported introspection tool for determining a lexical var's type.

    As far as I'm aware, fields isn't deprecated.

    As far as I'm aware, the only compile-time checking that the perl core does for lexical is types is that the package corresponding to the type must already exist, and that in conjunction with the compile-time use of fields, it checks constant hash lookups of a typed lexical reference. For example:

    package Foo; use fields qw(a b c); sub new { fields::new(@_); } package main; my Foo $r = Foo->new(); $r->{d} = 1; # compile-time error $key = 'd'; $r->{$key} = 1; # run-time error

    Dave.

Re: Checking TYPEs after my declaration
by dcmertens (Scribe) on Aug 19, 2016 at 11:07 UTC
    I am exceedingly interested in using this for my own purposes. I found the perlapi function that lets you figure this out. The type is, as I suspected, associated with the pad entry, not the variable. You can get it with XS via PadnameTYPE. It should be noted, though, that this function is not explicitly documented (i.e. not available in perlapi.pod), so it could change. But at least now you know how to get at it. :-)
      Oh great!

      Would be interesting to know if it's also callable from Perl via B :)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: Checking TYPEs after my declaration
by dcmertens (Scribe) on Dec 22, 2016 at 21:08 UTC