File Coverage

blib/lib/Return/Set.pm
Criterion Covered Total %
statement 34 34 100.0
branch 8 8 100.0
condition 5 6 83.3
subroutine 8 8 100.0
pod 1 1 100.0
total 56 57 98.2


line stmt bran cond sub pod time code
1             package Return::Set;
2              
3 2     2   363447 use strict;
  2         4  
  2         69  
4 2     2   9 use warnings;
  2         2  
  2         86  
5 2     2   30 use 5.010;
  2         6  
6              
7 2     2   844 use parent 'Exporter';
  2         617  
  2         10  
8              
9 2     2   190 use Carp qw(croak);
  2         4  
  2         117  
10 2     2   936 use Params::Get 0.13;
  2         24598  
  2         119  
11 2     2   1504 use Params::Validate::Strict 0.10 qw(validate_strict);
  2         99110  
  2         636  
12              
13             our @EXPORT_OK = qw(set_return);
14              
15             =head1 NAME
16              
17             Return::Set - Return a value optionally validated against a strict schema
18              
19             =head1 VERSION
20              
21             Version 0.03
22              
23             =cut
24              
25             our $VERSION = '0.03';
26              
27             =head1 SYNOPSIS
28              
29             use Return::Set qw(set_return);
30              
31             my $value = set_return($value); # Just returns $value
32              
33             my $value = set_return($value, { type => 'integer' }); # Validates $value is an integer
34              
35             =head1 DESCRIPTION
36              
37             Exports a single function, C, which returns a given value.
38             If a validation schema is provided, the value is validated using
39             L.
40             If validation fails, it croaks.
41              
42             When used hand-in-hand with L you should be able to formally specify the input and output sets for a method.
43              
44             =head1 METHODS
45              
46             =head2 set_return($value, $schema)
47              
48             Returns C<$value>.
49             If C<$schema> is provided, it validates the value against it.
50             Croaks if validation fails.
51              
52             =cut
53              
54             sub set_return {
55 22     22 1 418940 my $value;
56             my $schema;
57              
58 22 100 100     153 if((scalar(@_) == 1) && !ref($_[0])) {
59 4         58 return $_[0];
60             }
61              
62 18 100       58 if(scalar(@_) == 2) {
63 8         15 $value = $_[0];
64 8         18 $schema = $_[1];
65             } else {
66 10         47 my $params = Params::Get::get_params('output', \@_);
67 8   66     377 $value = $params->{'value'} // $params->{'output'};
68 8         22 $schema = $params->{'schema'};
69             }
70              
71 16 100       41 if(defined($schema)) {
72 13 100       30 eval {
73 13         119 validate_strict(args => { 'value' => $value }, schema => { 'value' => $schema });
74 10         1939 1;
75             } or croak "Validation failed: $@";
76             }
77              
78 13         100 return $value;
79             }
80              
81             =head1 AUTHOR
82              
83             Nigel Horne, C<< >>
84              
85             =head1 SEE ALSO
86              
87             =over 4
88              
89             =item * L
90              
91             =item * L
92              
93             =back
94              
95             =head1 SUPPORT
96              
97             This module is provided as-is without any warranty.
98              
99             =head1 LICENCE AND COPYRIGHT
100              
101             Copyright 2025 Nigel Horne.
102              
103             Usage is subject to licence terms.
104              
105             The licence terms of this software are as follows:
106              
107             =over 4
108              
109             =item * Personal single user, single computer use: GPL2
110              
111             =item * All other users (including Commercial, Charity, Educational, Government)
112             must apply in writing for a licence for use from Nigel Horne at the
113             above e-mail.
114              
115             =back
116              
117             =cut
118              
119             1;