1 package assertions::compat;
6 our @ISA = qw(assertions);
13 my $name = @_ ? shift : 'asserting';
15 $name =~ /::/ or $name = "${pkg}::${name}";
17 $class->SUPER::import(@_);
18 my $enabled = assertions::enabled();
23 *{$name} = $enabled ? \&_on : \&_off;
27 sub _compat_assertion_handler {
29 grep $_ ne 'assertion', @_
32 sub _do_nothing_handler {}
34 # test if 'assertion' attribute is natively supported
35 my $assertion_ok=eval q{
36 sub _my_asserting_test : assertion { 1 }
40 *MODIFY_CODE_ATTRIBUTES =
41 defined($assertion_ok)
42 ? \&_do_nothing_handler
43 : \&_compat_assertion_handler;
46 defined($assertion_ok)
50 unless (defined $assertion_ok) {
52 require warnings::register;
53 warnings::register->import;
63 assertions::compat - assertions for pre-5.9 versions of perl
67 # add support for 'assertion' attribute:
68 use base 'assertions::compat';
69 sub assert_foo : assertion { ... };
71 # then, maybe in another module:
74 # define sub 'asserting' with the assertion status:
75 use assertions::compat;
76 asserting and assert_foo(1,2,3,4);
79 use assertions::compat ASST => 'Foo::Bar::doz';
80 ASST and assert_foo('dozpera');
84 C<assertions::compat> allows to use assertions on perl versions prior
85 to 5.9.0 (that is the first one to natively support them). Though,
86 it's not magic, do not expect it to allow for conditionally executed
89 This module provides support for two different functionalities:
91 =head2 The C<assertion> attribute handler
93 The subroutine attribute C<assertion> is not recognised on perls
94 without assertion support. This module provides a
95 C<MODIFY_CODE_ATTRIBUTES> handler for this attribute. It must be used
98 use base 'assertions::compat';
100 sub assert_foo : assertion { ... }
102 Be aware that the handler just discards the attribute, so subroutines
103 declared as assertions will be B<unconditionally> called on perl without
104 native support for them.
106 This module also provides the C<supported> function to check if
107 assertions are supported or not:
109 my $supported = assertions::compat::supported();
112 =head2 Assertion execution status as a constant
114 C<assertions::compat> also allows to create constant subs whose value
115 is the assertion execution status. That allows checking explicitly and
116 efficiently when assertions have to be executed on perls without native
121 use assertions::compat ASST => 'Foo::Bar';
123 exports constant subroutine C<ASST>. Its value is true when assertions
124 tagged as C<Foo::Bar> has been activated via L<assertions::activate>;
125 usually done with the -A switch from the command line on perls
128 perl -A=Foo::Bar my_script.pl
130 or alternatively with...
132 perl -Massertions::activate=Foo::Bar my_script.pl
134 on pre-5.9.0 versions of perl.
136 The constant sub defined can be used following this idiom:
138 use assertions::compat ASST => 'Foo::Bar';
140 ASST and assert_foo();
142 When ASST is false, the perl interpreter optimizes away the rest of
143 the C<and> statement at compile time.
146 If no assertion selection tags are passed to C<use
147 assertions::compat>, the current module name is used as the selection
150 use assertions::compat 'ASST';
154 use assertions::compat ASST => __PACKAGE__;
156 If the name of the constant subroutine is also omitted, C<asserting>
159 This module will not emit a warning when the constant is redefined.
160 this is done on purpose to allow for code like that:
162 use assertions::compat ASST => 'Foo';
163 ASST and assert_foo();
165 use assertions::compat ASST => 'Bar';
166 ASST and assert_bar();
168 Finally, be aware that while assertion execution status is lexical
169 scoped, the defined constants are not. You should be careful on that
170 to not write inconsistent code. For instance...
174 use MyAssertions qw(assert_foo);
176 use assertions::compat ASST => 'Foo::Out'
178 use assertions::compat ASST => 'Foo::In';
179 ASST and assert_foo(); # ok!
182 ASST and assert_foo() # bad usage!
183 # ASST refers to tag Foo::In while assert_foo() is
184 # called only when Foo::Out has been activated.
185 # This is not what you want!!!
190 L<perlrun>, L<assertions>, L<assertions::activate>, L<attributes>.
194 Salvador FandiE<ntilde>o, E<lt>sfandino@yahoo.comE<gt>
196 =head1 COPYRIGHT AND LICENSE
198 Copyright 2005 by Salvador FandiE<ntilde>o
200 This library is free software; you can redistribute it and/or modify
201 it under the same terms as Perl itself.