9 use vars qw{ @warnings };
10 BEGIN { # ...and save 'em for later
11 $SIG{'__WARN__'} = sub { push @warnings, @_ }
13 END { print STDERR @warnings }
17 use Test::More tests => 75;
18 my $TB = Test::More->builder;
20 BEGIN { use_ok('constant'); }
22 use constant PI => 4 * atan2 1, 1;
24 ok defined PI, 'basic scalar constant';
25 is substr(PI, 0, 7), '3.14159', ' in substr()';
27 sub deg2rad { PI * $_[0] / 180 }
29 my $ninety = deg2rad 90;
31 cmp_ok abs($ninety - 1.5707), '<', 0.0001, ' in math expression';
33 use constant UNDEF1 => undef; # the right way
34 use constant UNDEF2 => ; # the weird way
35 use constant 'UNDEF3' ; # the 'short' way
36 use constant EMPTY => ( ) ; # the right way for lists
38 is UNDEF1, undef, 'right way to declare an undef';
39 is UNDEF2, undef, ' weird way';
40 is UNDEF3, undef, ' short way';
42 # XXX Why is this way different than the other ones?
54 use constant COUNTDOWN => scalar reverse 1, 2, 3, 4, 5;
55 use constant COUNTLIST => reverse 1, 2, 3, 4, 5;
56 use constant COUNTLAST => (COUNTLIST)[-1];
58 is COUNTDOWN, '54321';
61 is COUNTDOWN, join '', @cl;
63 is((COUNTLIST)[1], 4);
65 use constant ABC => 'ABC';
66 is "abc${\( ABC )}abc", "abcABCabc";
68 use constant DEF => 'D', 'E', chr ord 'F';
69 is "d e f @{[ DEF ]} d e f", "d e f D E F d e f";
71 use constant SINGLE => "'";
72 use constant DOUBLE => '"';
73 use constant BACK => '\\';
74 my $tt = BACK . SINGLE . DOUBLE ;
77 use constant MESS => q('"'\\"'"\\);
78 is MESS, q('"'\\"'"\\);
81 use constant TRAILING => '12 cats';
83 no warnings 'numeric';
84 cmp_ok TRAILING, '==', 12;
86 is TRAILING, '12 cats';
88 use constant LEADING => " \t1234";
89 cmp_ok LEADING, '==', 1234;
90 is LEADING, " \t1234";
92 use constant ZERO1 => 0;
93 use constant ZERO2 => 0.0;
94 use constant ZERO3 => '0.0';
101 use constant PI => 3.141;
104 cmp_ok(abs(PI - 3.1416), '<', 0.0001);
107 use constant E2BIG => $! = 7;
108 cmp_ok E2BIG, '==', 7;
109 # This is something like "Arg list too long", but the actual message
110 # text may vary, so we can't test much better than this.
111 cmp_ok length(E2BIG), '>', 6;
113 is @warnings, 0 or diag join "\n", "unexpected warning", @warnings;
114 @warnings = (); # just in case
116 ok @warnings && ($warnings[0] =~ /Constant sub.* undefined/) or
117 diag join "\n", "unexpected warning", @warnings;
120 is @warnings, 0, "unexpected warning";
122 my $curr_test = $TB->current_test;
123 use constant CSCALAR => \"ok 37\n";
124 use constant CHASH => { foo => "ok 38\n" };
125 use constant CARRAY => [ undef, "ok 39\n" ];
126 use constant CCODE => sub { "ok $_[0]\n" };
131 print CCODE->($curr_test+4);
133 $TB->current_test($curr_test+4);
135 eval q{ CCODE->{foo} };
136 ok scalar($@ =~ /^Constant is not a HASH/);
139 # Allow leading underscore
140 use constant _PRIVATE => 47;
143 # Disallow doubled leading underscore
145 use constant __DISALLOWED => "Oops";
147 like $@, qr/begins with '__'/;
149 # Check on declared() and %declared. This sub should be EXACTLY the
150 # same as the one quoted in the docs!
152 use constant 1.01; # don't omit this!
154 $name =~ s/^::/main::/;
156 my $full_name = $name =~ /::/ ? $name : "${pkg}::$name";
157 $constant::declared{$full_name};
161 ok $constant::declared{'main::PI'};
164 ok !$constant::declared{'main::PIE'};
168 use constant IN_OTHER_PACK => 42;
169 ::ok ::declared 'IN_OTHER_PACK';
170 ::ok $constant::declared{'Other::IN_OTHER_PACK'};
171 ::ok ::declared 'main::PI';
172 ::ok $constant::declared{'main::PI'};
175 ok declared 'Other::IN_OTHER_PACK';
176 ok $constant::declared{'Other::IN_OTHER_PACK'};
181 use warnings 'constant';
182 use constant 'BEGIN' => 1 ;
183 use constant 'INIT' => 1 ;
184 use constant 'CHECK' => 1 ;
185 use constant 'END' => 1 ;
186 use constant 'DESTROY' => 1 ;
187 use constant 'AUTOLOAD' => 1 ;
188 use constant 'STDIN' => 1 ;
189 use constant 'STDOUT' => 1 ;
190 use constant 'STDERR' => 1 ;
191 use constant 'ARGV' => 1 ;
192 use constant 'ARGVOUT' => 1 ;
193 use constant 'ENV' => 1 ;
194 use constant 'INC' => 1 ;
195 use constant 'SIG' => 1 ;
199 my @Expected_Warnings =
201 qr/^Constant name 'BEGIN' is a Perl keyword at/,
202 qr/^Constant subroutine BEGIN redefined at/,
203 qr/^Constant name 'INIT' is a Perl keyword at/,
204 qr/^Constant name 'CHECK' is a Perl keyword at/,
205 qr/^Constant name 'END' is a Perl keyword at/,
206 qr/^Constant name 'DESTROY' is a Perl keyword at/,
207 qr/^Constant name 'AUTOLOAD' is a Perl keyword at/,
208 qr/^Constant name 'STDIN' is forced into package main:: a/,
209 qr/^Constant name 'STDOUT' is forced into package main:: at/,
210 qr/^Constant name 'STDERR' is forced into package main:: at/,
211 qr/^Constant name 'ARGV' is forced into package main:: at/,
212 qr/^Constant name 'ARGVOUT' is forced into package main:: at/,
213 qr/^Constant name 'ENV' is forced into package main:: at/,
214 qr/^Constant name 'INC' is forced into package main:: at/,
215 qr/^Constant name 'SIG' is forced into package main:: at/,
217 for my $idx (0..$#warnings) {
218 like $warnings[$idx], $Expected_Warnings[$idx];
225 FAMILY => [ qw( John Jane Sally ) ],
226 AGES => { John => 33, Jane => 28, Sally => 3 },
227 RFAM => [ [ qw( John Jane Sally ) ] ],
228 SPIT => sub { shift },
231 is @{+FAMILY}, THREE;
232 is @{+FAMILY}, @{RFAM->[0]};
233 is FAMILY->[2], RFAM->[0]->[2];
234 is AGES->{FAMILY->[1]}, 28;
235 is THREE**3, SPIT->(@{+FAMILY}**3);
237 # Allow name of digits/underscores only if it begins with underscore
239 use warnings FATAL => 'constant';
241 use constant _1_2_3 => 'allowed';