NAME
Venus::Role::Fromable - Fromable Role
ABSTRACT
Fromable Role for Perl 5
SYNOPSIS
package Person;
use Venus::Class 'attr', 'with';
with 'Venus::Role::Fromable';
attr 'fname';
attr 'lname';
sub from_name {
my ($self, $name) = @_;
my ($fname, $lname) = split / /, $name;
return {
fname => $fname,
lname => $lname,
};
}
package main;
my $person = Person->from(name => 'Elliot Alderson');
# bless({fname => 'Elliot', lname => 'Alderson'}, 'Person')
DESCRIPTION
This package modifies the consuming package and provides methods for dispatching to constructor argument builders.
METHODS
This package provides the following methods:
from
from(any @values) (object)
The from method takes a key and value(s) and dispatches to the corresponding argument builder named in the form of from_${name} which should return arguments required by the constructor. The constructor will be called with the arguments returned from the argument builder and a class instance will be returned. If the key is omitted, the data type of the first value will be used as the key (or name), i.e. if the daya type of the first value is a string this method will attempt to dispatch to a builder named from_string.
Since 4.15
- from example 1
-
# given: synopsis; $person = Person->from(name => 'Elliot Alderson'); # bless({fname => 'Elliot', lname => 'Alderson'}, 'Person') # $person->fname; # "Elliot" # $person->lname; # "Alderson"
- from example 2
-
# given: synopsis; $person = Person->from('', 'Elliot Alderson'); # Exception! "No name provided to \"from\" via package \"Person\""
- from example 3
-
# given: synopsis; $person = Person->from(undef, 'Elliot Alderson'); # Exception! "No name provided to \"from\" via package \"Person\""
- from example 4
-
# given: synopsis; $person = Person->from('fullname', 'Elliot Alderson'); # Exception! "Unable to locate class method \"from_fullname\" via package \"Person\""
- from example 5
-
# given: synopsis; $person = Person->from('Elliot Alderson'); # Exception! "Unable to locate class method \"from_string\" via package \"Person\""
AUTHORS
Awncorp, awncorp@cpan.org
LICENSE
Copyright (C) 2022, Awncorp, awncorp@cpan.org.
This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.