| 1 | package Attribute::Boolean; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use 5.14.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Attribute::Boolean - Mark scalars as pure booleans | ||||||||||
| 11 | |||||||||||
| 12 | =cut | ||||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | use Attribute::Handlers; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use Variable::Magic qw( wizard cast ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 2 | ▶ | use parent 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use version; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | use Attribute::Boolean::Value; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | =head1 VERSION | ||||||||||
| 22 | |||||||||||
| 23 | Version v1.0.8 | ||||||||||
| 24 | |||||||||||
| 25 | =cut | ||||||||||
| 26 | |||||||||||
| 27 | 2 | our $VERSION = version->declare('v1.0.8'); | |||||||||
| 28 | # Don't forget the version in the pod above. | ||||||||||
| 29 | |||||||||||
| 30 | =head1 SYNOPSYS | ||||||||||
| 31 | |||||||||||
| 32 | This allows you to flag a variable as a boolean. | ||||||||||
| 33 | In numeric context, it will have the value 0 or 1. | ||||||||||
| 34 | In string context is will have the value "false" or "true". | ||||||||||
| 35 | In JSON, it will correctly return false or true values. | ||||||||||
| 36 | |||||||||||
| 37 | my $bool : Boolean; | ||||||||||
| 38 | print $bool; # "false" | ||||||||||
| 39 | $bool = (1 + 2 == 3); | ||||||||||
| 40 | print $bool; # "true" | ||||||||||
| 41 | print $bool ? "yes" : "no"; # "yes" | ||||||||||
| 42 | $bool = false; | ||||||||||
| 43 | print $bool ? "yes" : "no"; # "no" | ||||||||||
| 44 | |||||||||||
| 45 | =head1 EXPORT | ||||||||||
| 46 | |||||||||||
| 47 | This exports constants true and false. | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | 2 | our @EXPORT = qw{ true false }; | |||||||||
| 52 | |||||||||||
| 53 | 1 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 54 | # add this package to callers @ISA, as attributes only work via inheritance | ||||||||||
| 55 | 1 | my $class = shift; | |||||||||
| 56 | 1 | my $caller = caller; | |||||||||
| 57 | { | ||||||||||
| 58 | 2 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 59 | 1 | push @{ "${ caller }::ISA" }, __PACKAGE__; | |||||||||
| 60 | } | ||||||||||
| 61 | 1 | $class->export_to_level(1, $class, @_); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | =head1 USAGE | ||||||||||
| 65 | |||||||||||
| 66 | An attribute can be declared boolean as follows: | ||||||||||
| 67 | |||||||||||
| 68 | my $bool : Boolean; | ||||||||||
| 69 | |||||||||||
| 70 | or | ||||||||||
| 71 | |||||||||||
| 72 | my $bool : Boolean = true; | ||||||||||
| 73 | |||||||||||
| 74 | If any perl B<true> value is assigned, the variable is true; if a | ||||||||||
| 75 | perl B<false> value is assigned, the variable is false. | ||||||||||
| 76 | |||||||||||
| 77 | =head2 true | ||||||||||
| 78 | |||||||||||
| 79 | This returns 1 in numeric context, "true" in string context. | ||||||||||
| 80 | |||||||||||
| 81 | =head2 false | ||||||||||
| 82 | |||||||||||
| 83 | This returns 0 in numeric context, "false" in string context. | ||||||||||
| 84 | |||||||||||
| 85 | =head2 TO_JSON | ||||||||||
| 86 | |||||||||||
| 87 | Provided that convert_blessed is set on the JSON (or JSON::XS) object, | ||||||||||
| 88 | the variable will correctly convert to JSON true or false. | ||||||||||
| 89 | |||||||||||
| 90 | my $json = new JSON; | ||||||||||
| 91 | $json->pretty->convert_blessed; | ||||||||||
| 92 | my $bool : Boolean; | ||||||||||
| 93 | my %hash = ( | ||||||||||
| 94 | value => $bool, | ||||||||||
| 95 | me => true, | ||||||||||
| 96 | ); | ||||||||||
| 97 | print $json->encode(\%hash); # { | ||||||||||
| 98 | # "value" : false, | ||||||||||
| 99 | # "me" : true | ||||||||||
| 100 | # } | ||||||||||
| 101 | |||||||||||
| 102 | =cut | ||||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | sub Boolean : ATTR(SCALAR) | ||||||||
|
Subroutine
Boolean: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 105 | { | ||||||||||
| 106 | 0 | my ($class, $symbol, $ref, $name, undef, $phase) = @_; | |||||||||
| 107 | cast $$ref, wizard( | ||||||||||
| 108 | 0 | ▶ | 'set' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 109 | 0 | my $ref = shift; | |||||||||
| 110 | 0 | ▶ | $$ref = $$ref ? true : false; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | }, | ||||||||||
| 112 | 0 | ▶ | 'get' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 113 | 0 | my $ref = shift; | |||||||||
| 114 | 0 | ▶ | $$ref = $$ref ? true : false; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | }, | ||||||||||
| 116 | 0 | ); | |||||||||
| 117 | 2 | ▶ | } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 118 | |||||||||||
| 119 | =head1 AUTHOR | ||||||||||
| 120 | |||||||||||
| 121 | Cliff Stanford, C<< <cpan@may.be> >> | ||||||||||
| 122 | |||||||||||
| 123 | =head1 BUGS | ||||||||||
| 124 | |||||||||||
| 125 | Please report any bugs or feature requests to C<bug-attribute-boolean+ at rt.cpan.org>, or through | ||||||||||
| 126 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=attribute-boolean+>. I will be notified, and then you'll | ||||||||||
| 127 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 128 | |||||||||||
| 129 | |||||||||||
| 130 | |||||||||||
| 131 | |||||||||||
| 132 | =head1 SUPPORT | ||||||||||
| 133 | |||||||||||
| 134 | You can find documentation for this module with the perldoc command. | ||||||||||
| 135 | |||||||||||
| 136 | perldoc Attribute::Boolean | ||||||||||
| 137 | |||||||||||
| 138 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 139 | |||||||||||
| 140 | Alan Haggai Alavi C<< <alanhaggai@alanhaggai.org> >> for his | ||||||||||
| 141 | L<Scalar::Boolean> module which was the inspiration | ||||||||||
| 142 | for this module. | ||||||||||
| 143 | |||||||||||
| 144 | =head1 LICENCE AND COPYRIGHT | ||||||||||
| 145 | |||||||||||
| 146 | Copyright 2014 Cliff Stanford. | ||||||||||
| 147 | |||||||||||
| 148 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 149 | under the same terms as Perl itself. | ||||||||||
| 150 | |||||||||||
| 151 | =cut | ||||||||||
| 152 | |||||||||||
| 153 | 2 | 1; # End of Attribute::Boolean | |||||||||
| 154 | |||||||||||