GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Types::Self(3) User Contributed Perl Documentation Types::Self(3)

Types::Self - provides a "Self" type constraint, referring to the caller class or role

  {
    package Cow;
    use Moo;
  }
  
  {
    package Horse;
    use Moo;
    use Types::Self;
    use Types::Standard qw( Str );
    
    has name   => ( is => 'ro', isa => Str  );
    has mother => ( is => 'ro', isa => Self );
    has father => ( is => 'ro', isa => Self );
  }
  
  my $alice = Horse->new( name => 'Alice' );
  my $bob   = Horse->new( name => 'Bob' );
  
  # Okay
  my $baby = Horse->new(
    name   => 'Baby',
    mother => $alice,
    father => $bob,
  );
  
  # Dies
  my $baby = Horse->new(
    name   => 'Baby',
    mother => Cow->new,
    father => $bob,
  );

This module exports a "Self" type constraint which consrtains values to be blessed objects in the same class as the package it was imported into, or blessed objects which consume the role it was imported into. It should do the right thing with inheritance.

Using "Self" in a class means the same as "InstanceOf[__PACKAGE__]". (See InstanceOf in Types::Standard.)

Using "Self" in a role means the same as "ConsumerOf[__PACKAGE__]". (See ConsumerOf in Types::Standard.)

This module also exports "is_Self", which returns a boolean.

  package Marriage;
  use Moo::Role;
  use Types::Self qw( is_Self );
  
  has spouse => ( is => 'rwp', init_arg => undef );
  
  sub marry {
    my ( $me, $maybe_spouse ) = @_;
    if ( is_Self( $maybe_spouse ) ) {
      $me->_set_spouse( $maybe_spouse );
      $maybe_spouse->_set_spouse( $me );
    }
    else {
      warn "Cannot marry this!";
    }
    return $me;
  }

is_Self( $var ) can also be written as "Self->check( $var )".

The module also exports "assert_Self" which acts like "is_Self" but instead of returning a boolean, either lives or dies. This can be useful is you need to check that the first argument to a function is a blessed object.

  sub connect {
    my ( $self ) = ( shift );
    assert_Self $self;  # dies if called as a class method
    $self->{connected} = 1;
    return $self;
  }

assert_Self( $var ) can also be written as Self->( $var ).

The module also exports "to_Self" which will attempt to coerce other types to the Self type.

to_Self( $var ) can also be written as "Self->coerce( $var )".

An easy way of adding coercions to your Self type for the benefit of "to_Self". Other classes which use "InstanceOf[$YourClass]" will also get these coercions.

Accepts a list of type+code pairs. The code can be a scalarref naming a method to call to coerce a value, a coderef to call to coerce the value (operating on $_), or a string of Perl code to call to coerce the value (operating on $_).

  package MyClass;
  use Moo;
  use Types::Self -all;
  use Types::Standard qw( HashRef ArrayRef ScalarRef );
  coercions_for_Self(
    HashRef,   \'new',
    ArrayRef,  \'from_array',
    ScalarRef, sub { ... },
  );
  sub from_array {
    my ( $class, $arrayref ) = ( shift, @_ );
    ...;
  }

Only "Self" is exported by default.

Other functions need to be requested:

  use Types::Self -all;

Functions can be renamed:

  use Types::Self
    'Self'    => { -as => 'ThisClass' },
    'is_Self' => { -as => 'is_ThisClass' };

Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=Types-Self>.

Types::Standard, Type::Tiny::Manual.

Toby Inkster <tobyink@cpan.org>.

This software is copyright (c) 2022 by Toby Inkster.

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

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

2022-06-07 perl v5.40.2

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.