1PerlX::Maybe(3) User Contributed Perl Documentation PerlX::Maybe(3)
2
3
4
6 PerlX::Maybe - return a pair only if they are both defined
7
9 You once wrote:
10
11 my $bob = Person->new(
12 defined $name ? (name => $name) : (),
13 defined $age ? (age => $age) : (),
14 );
15
16 Now you can write:
17
18 my $bob = Person->new(
19 maybe name => $name,
20 maybe age => $age,
21 );
22
24 Moose classes (and some other classes) distinguish between an attribute
25 being unset and the attribute being set to undef. Supplying a
26 constructor arguments like this:
27
28 my $bob = Person->new(
29 name => $name,
30 age => $age,
31 );
32
33 Will result in the "name" and "age" attributes possibly being set to
34 undef (if the corresponding $name and $age variables are not defined),
35 which may violate the Person class' type constraints.
36
37 (Note: if you are the author of the class in question, you can solve
38 this using MooseX::UndefTolerant. However, some of us are stuck using
39 non-UndefTolerant classes written by third parties.)
40
41 To ensure that the Person constructor does not try to set a name or age
42 at all when they are undefined, ugly looking code like this is often
43 used:
44
45 my $bob = Person->new(
46 defined $name ? (name => $name) : (),
47 defined $age ? (age => $age) : (),
48 );
49
50 or:
51
52 my $bob = Person->new(
53 (name => $name) x!!(defined $name),
54 (age => $age) x!!(defined $age),
55 );
56
57 A slightly more elegant solution is the "maybe" function.
58
59 Functions
60 "maybe $x => $y, @rest"
61 This function checks that $x and $y are both defined. If they are,
62 it returns them both as a list; otherwise it returns the empty
63 list.
64
65 If @rest is provided, it is unconditionally appended to the end of
66 whatever list is returned.
67
68 The combination of these behaviours allows the following very
69 sugary syntax to "just work".
70
71 my $bob = Person->new(
72 name => $name,
73 address => $addr,
74 maybe phone => $tel,
75 maybe email => $email,
76 unique_id => $id,
77 );
78
79 This function is exported by default.
80
81 "provided $condition, $x => $y, @rest"
82 Like "maybe" but allows you to use a custom condition expression:
83
84 my $bob = Person->new(
85 name => $name,
86 address => $addr,
87 provided length($tel), phone => $tel,
88 provided $email =~ /\@/, email => $email,
89 unique_id => $id,
90 );
91
92 This function is not exported by default.
93
94 "PerlX::Maybe::IMPLEMENTATION"
95 Indicates whether the XS backend PerlX::Maybe::XS was loaded.
96
97 XS Backend
98 If you install PerlX::Maybe::XS, a faster XS-based implementation will
99 be used instead of the pure Perl functions. My basic benchmarking
100 experiments seem to show this to be around 30% faster.
101
102 Environment
103 The environment variable "PERLX_MAYBE_IMPLEMENTATION" may be set to
104 "PP" to prevent the XS backend from loading.
105
107 Please report any bugs to
108 <http://rt.cpan.org/Dist/Display.html?Queue=PerlX-Maybe>.
109
111 Syntax::Feature::Maybe, PerlX::Maybe::XS.
112
113 MooseX::UndefTolerant, PerlX::Perform, Exporter.
114
116 Toby Inkster <tobyink@cpan.org>.
117
119 This software is copyright (c) 2012-2013 by Toby Inkster.
120
121 This is free software; you can redistribute it and/or modify it under
122 the same terms as the Perl 5 programming language system itself.
123
125 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
126 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
127 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
128
129
130
131perl v5.28.0 2014-09-23 PerlX::Maybe(3)