Commits

Toby Inkster committed bbdc307

initial version

Comments (0)

Files changed (12)

+use inc::Module::Package 'RDF:standard';
+

lib/PerlX/Maybe.pm

+package PerlX::Maybe;
+
+use 5.008;
+use strict;
+
+our (@EXPORT, @ISA);
+BEGIN {
+	$PerlX::Maybe::AUTHORITY = 'cpan:TOBYINK';
+	$PerlX::Maybe::VERSION   = '0.001';
+	
+	require Exporter;
+	@ISA       = qw/Exporter/;
+	@EXPORT    = qw/maybe/;
+}
+
+sub maybe ($$@)
+{
+	my ($x, $y, @rest) = @_;
+	my @r = (
+		(defined $y && defined $x ? ($x, $y) : ()),
+		@rest,
+		);
+	return @r;
+}
+
+__FILE__
+__END__
+
+=head1 NAME
+
+PerlX::Maybe - return a pair only if they are both defined
+
+=head1 SYNOPSIS
+
+You once wrote:
+
+ my $bob = Person->new(
+    defined $name ? (name => $name) : (),
+    defined $age ? (age => $age) : (),
+    );
+
+Now you can write:
+
+ my $bob = Person->new(
+    maybe name => $name,
+    maybe age  => $age,
+    );
+
+=head1 DESCRIPTION
+
+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 C<name> and C<age> attributes possibly being set to
+undef (if the corresponding C<$name> and C<$age> variables are not defined),
+which may violate the Person class' type constraints.
+
+(Note: if you are the I<author> of the class in question, you can solve
+this using L<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) : (),
+    );
+
+A slightly more elegant solution is the C<maybe> function:
+
+=head2 C<< maybe $x => $y, @rest >>
+
+This function checks that C<< $x >> and C<< $y >> are both defined. If they
+are, it returns them both as a list; otherwise it returns the empty list.
+
+If C<< @rest >> is provided, it is unconditionally appended to the end of
+whatever list is returned.
+
+The combination of these behaviours allows the following very sugary syntax
+to "just work".
+
+ my $bob = Person->new(
+         name      => $name,
+         address   => $addr,
+   maybe phone     => $tel,
+   maybe email     => $email,
+         unique_id => $id,
+   );
+
+This function is exported by default.
+
+=head1 BUGS
+
+Please report any bugs to
+L<http://rt.cpan.org/Dist/Display.html?Queue=PerlX-Maybe>.
+
+=head1 SEE ALSO
+
+L<Syntax::Feature::Maybe>.
+
+L<MooseX::UndefTolerant>, L<PerlX::Perform>, L<Exporter>.
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+This software is copyright (c) 2012 by Toby Inkster.
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
+
+=head1 DISCLAIMER OF WARRANTIES
+
+THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+

lib/Syntax/Feature/Maybe.pm

+package Syntax::Feature::Maybe;
+
+use 5.008;
+use strict;
+use PerlX::Maybe qw//;
+
+BEGIN {
+	$Syntax::Feature::Maybe::AUTHORITY = 'cpan:TOBYINK';
+	$Syntax::Feature::Maybe::VERSION   = '0.001';
+}
+
+sub install
+{	
+	my ($class, %args) = @_;
+	my $into = delete $args{into};
+	
+	foreach my $f (qw/maybe/)
+	{
+		no strict 'refs';
+		*{"$into\::$f"} = \&{"PerlX::Maybe::$f"};
+	}
+}
+
+__FILE__
+__END__
+
+=head1 NAME
+
+Syntax::Feature::Maybe - use syntax qw/maybe/
+
+=head1 DESCRIPTION
+
+Tiny shim between L<PerlX::Maybe> and L<syntax>.
+
+=head1 BUGS
+
+Please report any bugs to
+L<http://rt.cpan.org/Dist/Display.html?Queue=PerlX-Maybe>.
+
+=head1 SEE ALSO
+
+L<PerlX::Maybe>, L<syntax>.
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+This software is copyright (c) 2012 by Toby Inkster.
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
+
+=head1 DISCLAIMER OF WARRANTIES
+
+THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+
+# This file acts as the project's changelog.
+
+@prefix :        <http://usefulinc.com/ns/doap#> .
+@prefix dcs:     <http://ontologi.es/doap-changeset#> .
+@prefix dc:      <http://purl.org/dc/terms/> .
+@prefix dist:    <http://purl.org/NET/cpan-uri/dist/PerlX-Maybe/> .
+@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
+
+dist:project :release dist:v_0-001 .
+dist:v_0-001
+	a               :Version ;
+	dc:issued       "2012-02-15"^^xsd:date ;
+	:revision       "0.001"^^xsd:string ;
+	:file-release   <http://backpan.cpan.org/authors/id/T/TO/TOBYINK/PerlX-Maybe-0.001.tar.gz> ;
+	rdfs:label      "Initial release" .
+
+# This file contains general metadata about the project.
+
+@prefix :        <http://usefulinc.com/ns/doap#> .
+@prefix dc:      <http://purl.org/dc/terms/> .
+@prefix foaf:    <http://xmlns.com/foaf/0.1/> .
+@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
+
+<http://purl.org/NET/cpan-uri/dist/PerlX-Maybe/project>
+	a               :Project ;
+	:programming-language "Perl" ;
+	:name           "PerlX-Maybe" ;
+	:shortdesc      "return a given list only if they are all defined" ;
+	:homepage       <https://metacpan.org/release/PerlX-Maybe> ;
+	:download-page  <https://metacpan.org/release/PerlX-Maybe> ;
+	:repository     [ a :HgRepository ; :browse <https://bitbucket.org/tobyink/p5-perlx-maybe> ] ;
+	:bug-database   <http://rt.cpan.org/Dist/Display.html?Queue=PerlX-Maybe> ;
+	:created        "2012-02-15"^^xsd:date ;
+	:license        <http://dev.perl.org/licenses/> ;
+	:developer      [ a foaf:Person ; foaf:name "Toby Inkster" ; foaf:mbox <mailto:tobyink@cpan.org> ] .
+
+<http://dev.perl.org/licenses/>
+	dc:title        "the same terms as the perl 5 programming language system itself" .

meta/makefile.ttl

+# This file provides instructions for packaging.
+
+@prefix : <http://purl.org/NET/cpan-uri/terms#> .
+
+<http://purl.org/NET/cpan-uri/dist/PerlX-Maybe/project>
+	:perl_version_from _:main ;
+	:version_from _:main ;
+	:readme_from _:main ;
+	:test_requires "Test::More 0.61" ;
+	:requires "strict","Exporter".
+
+_:main <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName> "lib/PerlX/Maybe.pm" .
+
+use Test::More tests => 1;
+BEGIN { use_ok('PerlX::Maybe') };
+
+use Test::More tests => 3;
+use PerlX::Maybe;
+
+is_deeply(
+	[ maybe foo => undef, maybe bar => 0, maybe baz => 1, undef ],
+	[ bar => 0, baz => 1, undef ],
+	);
+
+is_deeply(
+	[ 3, maybe foo => undef, 4, maybe bar => 0, 5, maybe baz => 1 ],
+	[ 3, 4, bar => 0, 5, baz => 1 ],
+	);
+
+is_deeply(
+	[ 3, maybe foo => {quux=>1}, undef, 4, maybe bar => 0, 5, maybe baz => 1 ],
+	[ 3, foo => {quux=>1}, undef, 4, bar => 0, 5, baz => 1 ],
+	);
+
+use Test::More;
+eval "use Test::Pod 1.00";
+plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
+all_pod_files_ok();
+

xt/02pod_coverage.t

+use Test::More;
+use Test::Pod::Coverage;
+
+my @modules = qw(PerlX::Maybe);
+pod_coverage_ok($_, "$_ is covered")
+	foreach @modules;
+done_testing(scalar @modules);
+

xt/03meta_uptodate.t

+use Test::More tests => 1;
+use Test::RDF::DOAP::Version;
+doap_version_ok('PerlX-Maybe', 'PerlX::Maybe');
+
+use Test::EOL;
+all_perl_files_ok();