perl-PerlX-Maybe - Return a Pair Only If They Are Both Defined

Edit Package perl-PerlX-Maybe

Moose classes (and some other classes) distinguish between an attribute being unset and the attribute being set to undef. Supplying a constructor arguments like this:

my $bob = Person->new(
name => $name,
age => $age,
);

Will result in the 'name' and 'age' attributes possibly being set to undef (if the corresponding '$name' and '$age' variables are not defined), which may violate the Person class' type constraints.

(Note: if you are the _author_ of the class in question, you can solve this using MooseX::UndefTolerant. However, some of us are stuck using non-UndefTolerant classes written by third parties.)

To ensure that the Person constructor does not try to set a name or age at all when they are undefined, ugly looking code like this is often used:

my $bob = Person->new(
defined $name ? (name => $name) : (),
defined $age ? (age => $age) : (),
);

or:

use PerlX::Maybe;

my $bob = Person->new(
(name => $name) x!!(defined $name),
(age => $age) x!!(defined $age),
);

A slightly more elegant solution is the 'maybe' function.

Refresh
Refresh
Source Files
Filename Size Changed
PerlX-Maybe-1.202.tar.gz 0000018346 17.9 KB
cpanspec.yml 0000000669 669 Bytes
perl-PerlX-Maybe.changes 0000001559 1.52 KB
perl-PerlX-Maybe.spec 0000002626 2.56 KB
Revision 7 (latest revision is 8)
Stephan Kulow's avatar Stephan Kulow (coolo) accepted request 964216 from Tina Müller's avatar Tina Müller (tinita) (revision 7)
- updated to 1.202
   see /usr/share/doc/packages/perl-PerlX-Maybe/Changes
  1.202	2022-03-14	Pi Day
   - Fix code example.
     Daniel Böhmer++
Comments 0
openSUSE Build Service is sponsored by