| 1 | #!/usr/bin/perl -c | ||
| 2 | |||
| 3 | package constant::boolean; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | constant::boolean - Define TRUE and FALSE constants. | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use constant::boolean; | ||
| 12 | |||
| 13 | use File::Spec; | ||
| 14 | |||
| 15 | sub is_package_exist { | ||
| 16 | my ($package) = @_; | ||
| 17 | return FALSE unless defined $package; | ||
| 18 | foreach my $inc (@INC) { | ||
| 19 | my $filename = File::Spec->catfile( | ||
| 20 | split( /\//, $inc ), split( /\::/, $package ) | ||
| 21 | ) . '.pm'; | ||
| 22 | return TRUE if -f $filename; | ||
| 23 | }; | ||
| 24 | return FALSE; | ||
| 25 | }; | ||
| 26 | |||
| 27 | no constant::boolean; | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | Defines C<TRUE> and C<FALSE> constants in caller's namespace. You could use | ||
| 32 | simple values like empty string or zero for false, or any non-empty and | ||
| 33 | non-zero string value as true, but the C<TRUE> and C<FALSE> constants are more | ||
| 34 | descriptive. | ||
| 35 | |||
| 36 | It is virtually the same as: | ||
| 37 | |||
| 38 | # double "not" operator is used for converting scalar to boolean value | ||
| 39 | use constant TRUE => !! 1; | ||
| 40 | use constant FALSE => !! ''; | ||
| 41 | |||
| 42 | The constants exported by C<constant::boolean> are not reported by | ||
| 43 | L<Test::Pod::Coverage>, so it is more convenient to use this module than to | ||
| 44 | define C<TRUE> and C<FALSE> constants by yourself. | ||
| 45 | |||
| 46 | The constants can be removed from class API with C<no constant::boolean> | ||
| 47 | pragma or some universal tool like L<namespace::clean>. | ||
| 48 | |||
| 49 | =for readme stop | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | 2 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 54 | |||
| 55 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 56 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 57 | |||
| 58 | 2 | our $VERSION = '0.02'; | |
| 59 | |||
| 60 | |||
| 61 | 2 | ▶ | sub import { |
|
Subroutine
import: called CC=1 SCAR=0.0
| |||
| 62 | 2 | my $caller = caller; | |
| 63 | |||
| 64 | 2 | ▶ | no strict 'refs'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 65 | # double "not" operator is used for converting scalar to boolean value | ||
| 66 | 2 | *{"${caller}::TRUE"} = sub () { !! 1 }; | |
| 67 | 2 | *{"${caller}::FALSE"} = sub () { !! '' }; | |
| 68 | |||
| 69 | 2 | return 1; | |
| 70 | }; | ||
| 71 | |||
| 72 | |||
| 73 | 1 | ▶ | sub unimport { |
|
Subroutine
unimport: called CC=2 SCAR=6.9
| |||
| 74 | 1 | require Symbol::Util; | |
| 75 | |||
| 76 | 1 | my $caller = caller; | |
| 77 | 1 | Symbol::Util::delete_sub("${caller}::$_") foreach qw( TRUE FALSE ); | |
| 78 | |||
| 79 | 1 | return 1; | |
| 80 | }; | ||
| 81 | |||
| 82 | |||
| 83 | 2 | 1; | |
| 84 | |||
| 85 | |||
| 86 | =head1 BUGS | ||
| 87 | |||
| 88 | If you find the bug or want to implement new features, please report it at | ||
| 89 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=constant-boolean> | ||
| 90 | |||
| 91 | =for readme continue | ||
| 92 | |||
| 93 | =head1 AUTHOR | ||
| 94 | |||
| 95 | Piotr Roszatycki <dexter@cpan.org> | ||
| 96 | |||
| 97 | =head1 LICENSE | ||
| 98 | |||
| 99 | Copyright 2008, 2009 by Piotr Roszatycki <dexter@cpan.org>. | ||
| 100 | |||
| 101 | This program is free software; you can redistribute it and/or modify it | ||
| 102 | under the same terms as Perl itself. | ||
| 103 | |||
| 104 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||