1 package assertions::compat;
4 our @ISA = qw(assertions);
11 my $name = @_ ? shift : 'asserting';
13 $name =~ /::/ or $name = "${pkg}::${name}";
15 $class->SUPER::import(@_);
16 my $enabled = assertions::enabled();
21 *{$name} = $enabled ? \&_on : \&_off;
25 sub _compat_assertion_handler {
27 grep $_ ne 'assertion', @_
30 sub _do_nothing_handler {}
32 # test if 'assertion' attribute is natively supported
33 my $assertion_ok=eval q{
34 sub _my_asserting_test : assertion { 1 }
38 *MODIFY_CODE_ATTRIBUTES =
39 defined($assertion_ok)
40 ? \&_do_nothing_handler
41 : \&_compat_assertion_handler;
49 assertions::compat - assertions for pre-5.9 versions of perl
53 # add support for 'assertion' attribute:
54 use base 'assertions::compat';
55 sub assert_foo : assertion { ... };
57 # then, maybe in another module:
60 # define sub 'asserting' with the assertion status:
61 use assertions::compat;
62 asserting and assert_foo(1,2,3,4);
65 use assertions::compat ASST => 'Foo::Bar::doz';
66 ASST and assert_foo('dozpera');
70 C<assertions::compat> allows to use assertions on perl versions prior
71 to 5.9.0 (that is the first one to natively support them). Though,
72 it's not magic, do not expect it to allow for conditional executed
75 This module provides support for two different functionalities:
77 =head2 The C<assertion> attribute handler
79 The subroutine attribute C<assertion> is not recognised on perls
80 without assertion support. This module provides a
81 C<MODIFY_CODE_ATTRIBUTES> handler for this attribute. It must be used
84 use base 'assertions::compat';
86 sub assert_foo : assertion { ... }
88 Be aware that the handler just discards the attribute, so subroutines
89 declared as assertions will be B<unconditionally> called on perl without
90 native support for them.
92 =head2 Assertion execution status as a constant
94 C<assertions::compat> also allows to create constant subs which value
95 is the assertion execution status. That allows checking explicitly and
96 efficiently if assertions have to be executed on perls without native
101 use assertions::compat ASST => 'Foo::Bar';
103 exports constant subroutine C<ASST>. Its value is true when assertions
104 tagged as C<Foo::Bar> has been activated via L<assertions::activate>;
105 usually done with the -A switch from the command line on perls
108 perl -A=Foo::Bar my_script.pl
110 or alternatively with...
112 perl -Massertions::activate=Foo::Bar my_script.pl
114 on pre-5.9.0 versions of perl.
116 The constant sub defined can be used following this idiom:
118 use assertions::compat ASST => 'Foo::Bar';
120 ASST and assert_foo();
122 When ASST is false, the perl interpreter optimizes away the rest of
123 the C<and> statement at compile time.
126 When no assertion selection tags are passed to C<use
127 assertions::compat>, the current module name is used as the selection
130 use assertions::compat 'ASST';
134 use assertions::compat ASST => __PACKAGE__;
136 If the name of the constant subroutine is also omitted, C<asserting>
139 This module will not emit a warning when the constant is redefined.
140 this is done on purpose to allow for code like that:
142 use assertions::compat ASST => 'Foo';
143 ASST and assert_foo();
145 use assertions::compat ASST => 'Bar';
146 ASST and assert_bar();
148 Finally, be aware that while assertion execution status is lexical
149 scoped, defined constants are not. You should be careful on that to
150 not write inconsistent code. For instance...
154 use MyAssertions qw(assert_foo);
156 use assertions::compat ASST => 'Foo::Out'
158 use assertions::compat ASST => 'Foo::In';
159 ASST and assert_foo(); # ok!
162 ASST and assert_foo() # bad usage!
163 # ASST refers to tag Foo::In while assert_foo() is
164 # called only when Foo::Out has been activated.
165 # This is not what you want!!!
170 L<perlrun>, L<assertions>, L<assertions::activate>, L<attributes>.
174 Salvador FandiE<ntilde>o, E<lt>sfandino@yahoo.comE<gt>
176 =head1 COPYRIGHT AND LICENSE
178 Copyright 2005 by Salvador FandiE<ntilde>o
180 This library is free software; you can redistribute it and/or modify
181 it under the same terms as Perl itself.