Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Exploring Type::Tiny Part 3: Using Type::Tie

by tobyink (Canon)
on Aug 08, 2018 at 17:02 UTC ( [id://1220123]=CUFP: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
       use Types::Standard qw(Int);
       
       tie(my @numbers, Int);
       
       push @numbers, 1, 2, 3;     # ok
       push @numbers, "four";      # dies
    
  2. or download this
       use Types::Standard qw(Int Num);
       
       my $RoundedInt = Int->plus_coercions( Num, 'int $_' ); 
    ...
       push @numbers, 1, 2, 3;     # ok
       push @numbers, 4.2;         # rounded to 4
       push @numbers, "five";      # dies
    
  3. or download this
       use Type::Tie qw();
       use MooseX::Types::Moose qw(Int);
       
       tie(my @numbers, "Type::Tie::ARRAY", Int);
    
  4. or download this
       use Type::Tie qw(ttie);
       use MooseX::Types::Moose qw(Int);
       
       ttie(my @numbers, Int);
    
  5. or download this
       use v5.16;
       
       package Foo {
    ...
       my $foo = Foo->new( numbers => [1, 2, 3] );
       
       push @{ $foo->numbers }, "hi";   # this is allowed
    
  6. or download this
       use v5.16;
       
       package Foo {
    ...
       my $foo = Foo->new( numbers => [1, 2, 3] );
       
       push @{ $foo->numbers }, "hi";   # dies
    
  7. or download this
       use Types::Standard qw(Int);
       use Devel::StrictMode qw(STRICT);
       
    ...
       tie @array_of_ints, Int if STRICT;
       
       ...;   # do stuff here
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1220123]
Approved by marto
Front-paged by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-20 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found