Types::Mojo - Types related to Mojo
version 0.06
package MyClass; use Moo; use Types::Mojo qw(MojoFile MojoCollection); use Types::Standard qw(Int); has file => ( is => 'rw', isa => MojoFile, coerce => 1 ); has coll => ( is => 'rw', isa => MojoCollection, coerce => 1 ); has ints => ( is => 'rw', isa => MojoCollection[Int] ); 1;
In the script
use MyClass; my $object = MyClass->new( file => __FILE__ ); # will be coerced into a Mojo::File object say $object->file->move_to( '/path/to/new/location' ); my $object2 = MyClass->new( coll => [qw/a b/] ); $object2->coll->each(sub { say $_; });
An object of Mojo::Collection. Can be parameterized with an other Type::Tiny type.
has ints => ( is => 'rw', isa => MojoCollection[Int] );
will accept only a Mojo::Collection of integers.
Mojo::Collection
An object of Mojo::File
A MojoCollection of MojoFiles.
MojoCollection
MojoFile
An object of Mojo::URL. Can be parameterized with a scheme.
has http_url => ( is => 'rw', isa => MojoURL["https?"] ); # s? means plain or secure -> http or https has ftp_url => ( is => 'rw', isa => MojoURL["ftp"] );
An object of Mojo::UserAgent
These coercions are defined.
Array reference to MojoCollection
In a class
package Test; use Moo; use Types::Mojo qw(MojoCollection); has 'collection' => ( is => 'ro', isa => MojoCollection, coerce => 1 ); 1;
use Test; use v5.22; use feature 'postderef'; my $obj = Test->new( collection => [ 1, 2 ], ); my $sqrs = $obj->collection->map( sub { $_ ** 2 } ); say $_ for $sqrs->to_array->@*;
String to MojoFile
package Test; use Moo; use Types::Mojo qw(MojoFile); has 'file' => ( is => 'ro', isa => MojoFile, coerce => 1 ); 1;
use Test; use v5.22; my $obj = Test->new( file => __FILE__, ); say $obj->file->slurp;
MojoCollection of Strings
package Test; use Moo; use Types::Mojo qw(MojoFile); has 'files' => ( is => 'ro', isa => MojoFileList, coerce => 1 ); 1;
use Test; use v5.22; my $obj = Test->new( files => Mojo::Collection->(__FILE__), ); for my $file ( @{ $obj->files->to_array } ) { say $file->basename; }
Array of Strings
use Test; use v5.22; my $obj = Test->new( files => [__FILE__], ); for my $file ( @{ $obj->files->to_array } ) { say $file->basename; }
Array of MojoFile
package Test; use Moo; use Types::Mojo qw(MojoFileList); has 'files' => ( is => 'ro', isa => MojoFileList, coerce => 1 ); 1;
use Test; use v5.22; my $obj = Test->new( files => [Mojo::File->new(__FILE__)], ); for my $file ( @{ $obj->files->to_array } ) { say $file->basename; }
String to MojoURL
package Test; use Moo; use Types::Mojo qw(MojoFile); has 'file' => ( is => 'ro', isa => MojoURL, coerce => 1 ); 1;
use Test; use v5.22; my $obj = Test->new( url => 'http://perl-services.de', ); say $obj->url->host;
Renee Baecker <reneeb@cpan.org>
This software is Copyright (c) 2019 by Renee Baecker.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install Types::Mojo, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Types::Mojo
CPAN shell
perl -MCPAN -e shell install Types::Mojo
For more information on module installation, please visit the detailed CPAN module installation guide.