| 1 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package Boolean::String; | ||
| 5 | |||
| 6 | # ABSTRACT: Strings with boolean values independent of perl's assumptions | ||
| 7 | |||
| 8 | |||
| 9 | 4 | ▶ | use Sub::Exporter -setup => { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | exports => [ qw( true false ) ], | ||
| 11 | groups => { default => [ qw( true false ) ] }, | ||
| 12 | 4 | }; | |
| 13 | |||
| 14 | |||
| 15 | 5 | ▶ | sub true { |
|
Subroutine
true: called CC=1 SCAR=0.0
Pod
documented
| |||
| 16 | 5 | my $string = shift; | |
| 17 | |||
| 18 | 5 | return bless \"$string", 'Boolean::String::True'; | |
| 19 | } | ||
| 20 | |||
| 21 | |||
| 22 | 5 | ▶ | sub false { |
|
Subroutine
false: called CC=1 SCAR=0.0
Pod
documented
| |||
| 23 | 5 | my $string = shift; | |
| 24 | |||
| 25 | 5 | return bless \"$string", 'Boolean::String::False'; | |
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | package # hide from PAUSE | ||
| 30 | Boolean::String::True; | ||
| 31 | |||
| 32 | use overload | ||
| 33 | 2 | ▶ | bool => sub { return 1 }, |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 34 | 3 | ▶ | '""' => sub { return ${ shift() } }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 35 | 4 | fallback => 1, | |
| 36 | 4 | ; | |
| 37 | |||
| 38 | |||
| 39 | package # hide from PAUSE | ||
| 40 | Boolean::String::False; | ||
| 41 | |||
| 42 | use overload | ||
| 43 | 2 | ▶ | bool => sub { return 0 }, |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 44 | 3 | ▶ | '""' => sub { return ${ shift() } }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 45 | 4 | fallback => 1, | |
| 46 | 4 | ; | |
| 47 | |||
| 48 | 4 | 1; | |
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | =pod | ||
| 53 | |||
| 54 | =head1 NAME | ||
| 55 | |||
| 56 | Boolean::String - Strings with boolean values independent of perl's assumptions | ||
| 57 | |||
| 58 | =head1 VERSION | ||
| 59 | |||
| 60 | version 0.01 | ||
| 61 | |||
| 62 | =head1 SYNOPSIS | ||
| 63 | |||
| 64 | use Boolean::String; | ||
| 65 | |||
| 66 | $message = false 'Record not found'; | ||
| 67 | |||
| 68 | $message = true 'Record found'; | ||
| 69 | |||
| 70 | =head1 DESCRIPTION | ||
| 71 | |||
| 72 | Boolean::String allows you to overload a string with a value in boolean context. Normally, perl considers all strings except the empty string to be true. Boolean::String allows you to change this assumption. | ||
| 73 | |||
| 74 | =head1 FUNCTIONS | ||
| 75 | |||
| 76 | =head2 true | ||
| 77 | |||
| 78 | This expects a single string, and returns an object that is true in boolean context and the passed in string in string context. | ||
| 79 | |||
| 80 | $true_string = true '...'; | ||
| 81 | |||
| 82 | =head2 false | ||
| 83 | |||
| 84 | This expects a single string, and returns an object that is false in boolean context and the passed in string in string context. | ||
| 85 | |||
| 86 | $false_string = false '...'; | ||
| 87 | |||
| 88 | =head1 IMPORTING | ||
| 89 | |||
| 90 | The functions are exported by default. Boolean::String uses L<Sub::Exporter|Sub::Exporter> for its import/export business. This makes it easy to change the names of the imported functions, like so: | ||
| 91 | |||
| 92 | # import 'true_because' and 'false_because' | ||
| 93 | use Boolean::String -all => { -suffix => '_because' }; | ||
| 94 | |||
| 95 | # import 'success' and 'failure' | ||
| 96 | use Boolean::String true => { -as => 'success' }, false => { -as => 'failure' }; | ||
| 97 | |||
| 98 | There's a whole slew of flexibility that Sub::Exporter brings to the table, so check it out if your importing needs are more involved than this. | ||
| 99 | |||
| 100 | =head1 SEE ALSO | ||
| 101 | |||
| 102 | =head2 L<Scalar::Util's dualvar|Scalar::Util/dualvar> | ||
| 103 | |||
| 104 | dualvar allows you to have different values for numeric and string contexts. Unfortunately, Boolean::String's functionality cannot be implemented with this (simply setting the numeric value to 0/1), because perl derives a variable's value in boolean context from its value in string context, not numeric context. | ||
| 105 | |||
| 106 | =head2 L<Sub::Exporter's import semantics|Sub::Exporter/CALLING THE EXPORTER> | ||
| 107 | |||
| 108 | Sub::Exporter handles the importing of the functions, so if you want to do something fancy, that's where you can find out how. | ||
| 109 | |||
| 110 | =head1 AUTHOR | ||
| 111 | |||
| 112 | everybody <everybody at cpan.org> | ||
| 113 | |||
| 114 | =head1 COPYRIGHT AND LICENSE | ||
| 115 | |||
| 116 | This software is copyright (c) 2012 by everybody. | ||
| 117 | |||
| 118 | This is free software; you can redistribute it and/or modify it under | ||
| 119 | the same terms as the Perl 5 programming language system itself. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | |||
| 124 | __END__ | ||