1b39c5158Smillert#!./perl -T 2b39c5158Smillert 3b39c5158Smillertuse warnings; 4*9f11ffb7Safresh1our ( @warnings, $fagwoosh, $putt, $kloong ); 5b39c5158SmillertBEGIN { # ...and save 'em for later 6b39c5158Smillert $SIG{'__WARN__'} = sub { push @warnings, @_ } 7b39c5158Smillert} 8b39c5158SmillertEND { @warnings && print STDERR join "\n- ", "accumulated warnings:", @warnings } 9b39c5158Smillert 10b39c5158Smillert 11b39c5158Smillertuse strict; 12b8851fccSafresh1use Test::More tests => 109; 13b39c5158Smillertmy $TB = Test::More->builder; 14b39c5158Smillert 15b39c5158SmillertBEGIN { use_ok('constant'); } 16b39c5158Smillert 17b39c5158Smillertuse constant PI => 4 * atan2 1, 1; 18b39c5158Smillert 19b39c5158Smillertok defined PI, 'basic scalar constant'; 20b39c5158Smillertis substr(PI, 0, 7), '3.14159', ' in substr()'; 21b39c5158Smillert 22b39c5158Smillertsub deg2rad { PI * $_[0] / 180 } 23b39c5158Smillert 24b39c5158Smillertmy $ninety = deg2rad 90; 25b39c5158Smillert 26b39c5158Smillertcmp_ok abs($ninety - 1.5707), '<', 0.0001, ' in math expression'; 27b39c5158Smillert 28b39c5158Smillertuse constant UNDEF1 => undef; # the right way 29b39c5158Smillertuse constant UNDEF2 => ; # the weird way 30b39c5158Smillertuse constant 'UNDEF3' ; # the 'short' way 31b39c5158Smillertuse constant EMPTY => ( ) ; # the right way for lists 32b39c5158Smillert 33b39c5158Smillertis UNDEF1, undef, 'right way to declare an undef'; 34b39c5158Smillertis UNDEF2, undef, ' weird way'; 35b39c5158Smillertis UNDEF3, undef, ' short way'; 36b39c5158Smillert 37b39c5158Smillert# XXX Why is this way different than the other ones? 38b39c5158Smillertmy @undef = UNDEF1; 39b39c5158Smillertis @undef, 1; 40b39c5158Smillertis $undef[0], undef; 41b39c5158Smillert 42b39c5158Smillert@undef = UNDEF2; 43b39c5158Smillertis @undef, 0; 44b39c5158Smillert@undef = UNDEF3; 45b39c5158Smillertis @undef, 0; 46b39c5158Smillert@undef = EMPTY; 47b39c5158Smillertis @undef, 0; 48b39c5158Smillert 49b39c5158Smillertuse constant COUNTDOWN => scalar reverse 1, 2, 3, 4, 5; 50b39c5158Smillertuse constant COUNTLIST => reverse 1, 2, 3, 4, 5; 51b39c5158Smillertuse constant COUNTLAST => (COUNTLIST)[-1]; 52b39c5158Smillert 53b39c5158Smillertis COUNTDOWN, '54321'; 54b39c5158Smillertmy @cl = COUNTLIST; 55b39c5158Smillertis @cl, 5; 56b39c5158Smillertis COUNTDOWN, join '', @cl; 57b39c5158Smillertis COUNTLAST, 1; 58b39c5158Smillertis((COUNTLIST)[1], 4); 59b39c5158Smillert 60b39c5158Smillertuse constant ABC => 'ABC'; 61b39c5158Smillertis "abc${\( ABC )}abc", "abcABCabc"; 62b39c5158Smillert 63b39c5158Smillertuse constant DEF => 'D', 'E', chr ord 'F'; 64b39c5158Smillertis "d e f @{[ DEF ]} d e f", "d e f D E F d e f"; 65b39c5158Smillert 66b39c5158Smillertuse constant SINGLE => "'"; 67b39c5158Smillertuse constant DOUBLE => '"'; 68b39c5158Smillertuse constant BACK => '\\'; 69b39c5158Smillertmy $tt = BACK . SINGLE . DOUBLE ; 70b39c5158Smillertis $tt, q(\\'"); 71b39c5158Smillert 72b39c5158Smillertuse constant MESS => q('"'\\"'"\\); 73b39c5158Smillertis MESS, q('"'\\"'"\\); 74b39c5158Smillertis length(MESS), 8; 75b39c5158Smillert 76b39c5158Smillertuse constant LEADING => " \t1234"; 77b39c5158Smillertcmp_ok LEADING, '==', 1234; 78b39c5158Smillertis LEADING, " \t1234"; 79b39c5158Smillert 80b39c5158Smillertuse constant ZERO1 => 0; 81b39c5158Smillertuse constant ZERO2 => 0.0; 82b39c5158Smillertuse constant ZERO3 => '0.0'; 83b39c5158Smillertis ZERO1, '0'; 84b39c5158Smillertis ZERO2, '0'; 85b39c5158Smillertis ZERO3, '0.0'; 86b39c5158Smillert 87b39c5158Smillert{ 88b39c5158Smillert package Other; 89b39c5158Smillert use constant PI => 3.141; 90b39c5158Smillert} 91b39c5158Smillert 92b39c5158Smillertcmp_ok(abs(PI - 3.1416), '<', 0.0001); 93b39c5158Smillertis Other::PI, 3.141; 94b39c5158Smillert 95*9f11ffb7Safresh1# Test that constant.pm can create a dualvar out of $! 96*9f11ffb7Safresh1use constant A_DUALVAR_CONSTANT => $! = 7; 97*9f11ffb7Safresh1cmp_ok A_DUALVAR_CONSTANT, '==', 7; 98*9f11ffb7Safresh1# Make sure we have an error message string. It does not 99*9f11ffb7Safresh1# matter that 7 means different things on different platforms. 100*9f11ffb7Safresh1# If this test fails, then either constant.pm or $! is broken: 101*9f11ffb7Safresh1cmp_ok length(A_DUALVAR_CONSTANT), '>', 6; 102b39c5158Smillert 103b39c5158Smillertis @warnings, 0 or diag join "\n- ", "unexpected warning:", @warnings; 104b39c5158Smillert@warnings = (); # just in case 105b39c5158Smillertundef &PI; 106b39c5158Smillertok @warnings && ($warnings[0] =~ /Constant sub.* undefined/) or 107b39c5158Smillert diag join "\n", "unexpected warning", @warnings; 108b39c5158Smillertshift @warnings; 109b39c5158Smillert 110b39c5158Smillertis @warnings, 0, "unexpected warning"; 111b39c5158Smillert 112b39c5158Smillertmy $curr_test = $TB->current_test; 113b39c5158Smillertuse constant CSCALAR => \"ok 35\n"; 114b39c5158Smillertuse constant CHASH => { foo => "ok 36\n" }; 115b39c5158Smillertuse constant CARRAY => [ undef, "ok 37\n" ]; 116b39c5158Smillertuse constant CCODE => sub { "ok $_[0]\n" }; 117b39c5158Smillert 118b39c5158Smillertmy $output = $TB->output ; 119b39c5158Smillertprint $output ${+CSCALAR}; 120b39c5158Smillertprint $output CHASH->{foo}; 121b39c5158Smillertprint $output CARRAY->[1]; 122b39c5158Smillertprint $output CCODE->($curr_test+4); 123b39c5158Smillert 124b39c5158Smillert$TB->current_test($curr_test+4); 125b39c5158Smillert 126b39c5158Smillerteval q{ CCODE->{foo} }; 127b8851fccSafresh1ok scalar($@ =~ /^Constant is not a HASH|Not a HASH reference/); 128b39c5158Smillert 129b39c5158Smillert 130b39c5158Smillert# Allow leading underscore 131b39c5158Smillertuse constant _PRIVATE => 47; 132b39c5158Smillertis _PRIVATE, 47; 133b39c5158Smillert 134b39c5158Smillert# Disallow doubled leading underscore 135b39c5158Smillerteval q{ 136b39c5158Smillert use constant __DISALLOWED => "Oops"; 137b39c5158Smillert}; 138b39c5158Smillertlike $@, qr/begins with '__'/; 139b39c5158Smillert 140b39c5158Smillert# Check on declared() and %declared. This sub should be EXACTLY the 141b39c5158Smillert# same as the one quoted in the docs! 142b39c5158Smillertsub declared ($) { 143b39c5158Smillert use constant 1.01; # don't omit this! 144b39c5158Smillert my $name = shift; 145b39c5158Smillert $name =~ s/^::/main::/; 146b39c5158Smillert my $pkg = caller; 147b39c5158Smillert my $full_name = $name =~ /::/ ? $name : "${pkg}::$name"; 148b39c5158Smillert $constant::declared{$full_name}; 149b39c5158Smillert} 150b39c5158Smillert 151b39c5158Smillertok declared 'PI'; 152b39c5158Smillertok $constant::declared{'main::PI'}; 153b39c5158Smillert 154b39c5158Smillertok !declared 'PIE'; 155b39c5158Smillertok !$constant::declared{'main::PIE'}; 156b39c5158Smillert 157b39c5158Smillert{ 158b39c5158Smillert package Other; 159b39c5158Smillert use constant IN_OTHER_PACK => 42; 160b39c5158Smillert ::ok ::declared 'IN_OTHER_PACK'; 161b39c5158Smillert ::ok $constant::declared{'Other::IN_OTHER_PACK'}; 162b39c5158Smillert ::ok ::declared 'main::PI'; 163b39c5158Smillert ::ok $constant::declared{'main::PI'}; 164b39c5158Smillert} 165b39c5158Smillert 166b39c5158Smillertok declared 'Other::IN_OTHER_PACK'; 167b39c5158Smillertok $constant::declared{'Other::IN_OTHER_PACK'}; 168b39c5158Smillert 169b39c5158Smillert@warnings = (); 170b39c5158Smillerteval q{ 171b39c5158Smillert no warnings; 172b39c5158Smillert use warnings 'constant'; 173b39c5158Smillert use constant 'BEGIN' => 1 ; 174b39c5158Smillert use constant 'INIT' => 1 ; 175b39c5158Smillert use constant 'CHECK' => 1 ; 176b39c5158Smillert use constant 'END' => 1 ; 177b39c5158Smillert use constant 'DESTROY' => 1 ; 178b39c5158Smillert use constant 'AUTOLOAD' => 1 ; 179b39c5158Smillert use constant 'STDIN' => 1 ; 180b39c5158Smillert use constant 'STDOUT' => 1 ; 181b39c5158Smillert use constant 'STDERR' => 1 ; 182b39c5158Smillert use constant 'ARGV' => 1 ; 183b39c5158Smillert use constant 'ARGVOUT' => 1 ; 184b39c5158Smillert use constant 'ENV' => 1 ; 185b39c5158Smillert use constant 'INC' => 1 ; 186b39c5158Smillert use constant 'SIG' => 1 ; 187b39c5158Smillert use constant 'UNITCHECK' => 1; 188b39c5158Smillert}; 189b39c5158Smillert 190b39c5158Smillertmy @Expected_Warnings = 191b39c5158Smillert ( 192b39c5158Smillert qr/^Constant name 'BEGIN' is a Perl keyword at/, 193b39c5158Smillert qr/^Constant subroutine BEGIN redefined at/, 194b39c5158Smillert qr/^Constant name 'INIT' is a Perl keyword at/, 195b39c5158Smillert qr/^Constant name 'CHECK' is a Perl keyword at/, 196b39c5158Smillert qr/^Constant name 'END' is a Perl keyword at/, 197b39c5158Smillert qr/^Constant name 'DESTROY' is a Perl keyword at/, 198b39c5158Smillert qr/^Constant name 'AUTOLOAD' is a Perl keyword at/, 199b39c5158Smillert qr/^Constant name 'STDIN' is forced into package main:: a/, 200b39c5158Smillert qr/^Constant name 'STDOUT' is forced into package main:: at/, 201b39c5158Smillert qr/^Constant name 'STDERR' is forced into package main:: at/, 202b39c5158Smillert qr/^Constant name 'ARGV' is forced into package main:: at/, 203b39c5158Smillert qr/^Constant name 'ARGVOUT' is forced into package main:: at/, 204b39c5158Smillert qr/^Constant name 'ENV' is forced into package main:: at/, 205b39c5158Smillert qr/^Constant name 'INC' is forced into package main:: at/, 206b39c5158Smillert qr/^Constant name 'SIG' is forced into package main:: at/, 207b39c5158Smillert qr/^Constant name 'UNITCHECK' is a Perl keyword at/, 208b39c5158Smillert); 209b39c5158Smillert 210b39c5158Smillertunless ($] > 5.009) { 211b39c5158Smillert # Remove the UNITCHECK warning 212b39c5158Smillert pop @Expected_Warnings; 213b39c5158Smillert # But keep the count the same 214b39c5158Smillert push @Expected_Warnings, qr/^$/; 215b39c5158Smillert push @warnings, ""; 216b39c5158Smillert} 217b39c5158Smillert 218b39c5158Smillert# when run under "make test" 219b39c5158Smillertif (@warnings == 16) { 220b39c5158Smillert push @warnings, ""; 221b39c5158Smillert push @Expected_Warnings, qr/^$/; 222b39c5158Smillert} 223b39c5158Smillert# when run directly: perl -wT -Ilib t/constant.t 224b39c5158Smillertelsif (@warnings == 17) { 225b39c5158Smillert splice @Expected_Warnings, 1, 0, 226b39c5158Smillert qr/^Prototype mismatch: sub main::BEGIN \(\) vs none at/; 227b39c5158Smillert} 228b39c5158Smillert# when run directly under 5.6.2: perl -wT -Ilib t/constant.t 229b39c5158Smillertelsif (@warnings == 15) { 230b39c5158Smillert splice @Expected_Warnings, 1, 1; 231b39c5158Smillert push @warnings, "", ""; 232b39c5158Smillert push @Expected_Warnings, qr/^$/, qr/^$/; 233b39c5158Smillert} 234b39c5158Smillertelse { 235b39c5158Smillert my $rule = " -" x 20; 236b39c5158Smillert diag "/!\\ unexpected case: ", scalar @warnings, " warnings\n$rule\n"; 237b39c5158Smillert diag map { " $_" } @warnings; 238b39c5158Smillert diag $rule, $/; 239b39c5158Smillert} 240b39c5158Smillert 241b39c5158Smillertis @warnings, 17; 242b39c5158Smillert 243b39c5158Smillertfor my $idx (0..$#warnings) { 244b39c5158Smillert like $warnings[$idx], $Expected_Warnings[$idx]; 245b39c5158Smillert} 246b39c5158Smillert 247b39c5158Smillert@warnings = (); 248b39c5158Smillert 249b39c5158Smillert 250b39c5158Smillertuse constant { 251b39c5158Smillert THREE => 3, 252b39c5158Smillert FAMILY => [ qw( John Jane Sally ) ], 253b39c5158Smillert AGES => { John => 33, Jane => 28, Sally => 3 }, 254b39c5158Smillert RFAM => [ [ qw( John Jane Sally ) ] ], 255b39c5158Smillert SPIT => sub { shift }, 256b39c5158Smillert}; 257b39c5158Smillert 258b39c5158Smillertis @{+FAMILY}, THREE; 259b39c5158Smillertis @{+FAMILY}, @{RFAM->[0]}; 260b39c5158Smillertis FAMILY->[2], RFAM->[0]->[2]; 261b39c5158Smillertis AGES->{FAMILY->[1]}, 28; 262b39c5158Smillertis THREE**3, SPIT->(@{+FAMILY}**3); 263b39c5158Smillert 264b39c5158Smillert# Allow name of digits/underscores only if it begins with underscore 265b39c5158Smillert{ 266b39c5158Smillert use warnings FATAL => 'constant'; 267b39c5158Smillert eval q{ 268b39c5158Smillert use constant _1_2_3 => 'allowed'; 269b39c5158Smillert }; 270b39c5158Smillert ok( $@ eq '' ); 271b39c5158Smillert} 272b39c5158Smillert 273b39c5158Smillertsub slotch (); 274b39c5158Smillert 275b39c5158Smillert{ 276b39c5158Smillert my @warnings; 277b39c5158Smillert local $SIG{'__WARN__'} = sub { push @warnings, @_ }; 278b39c5158Smillert eval 'use constant slotch => 3; 1' or die $@; 279b39c5158Smillert 280b39c5158Smillert is ("@warnings", "", "No warnings if a prototype exists"); 281b39c5158Smillert 282b39c5158Smillert my $value = eval 'slotch'; 283b39c5158Smillert is ($@, ''); 284b39c5158Smillert is ($value, 3); 285b39c5158Smillert} 286b39c5158Smillert 287b39c5158Smillertsub zit; 288b39c5158Smillert 289b39c5158Smillert{ 290b39c5158Smillert my @warnings; 291b39c5158Smillert local $SIG{'__WARN__'} = sub { push @warnings, @_ }; 292b39c5158Smillert eval 'use constant zit => 4; 1' or die $@; 293b39c5158Smillert 294b39c5158Smillert # empty prototypes are reported differently in different versions 295b39c5158Smillert my $no_proto = $] < 5.008004 ? "" : ": none"; 296b39c5158Smillert 297b39c5158Smillert is(scalar @warnings, 1, "1 warning"); 298b39c5158Smillert like ($warnings[0], qr/^Prototype mismatch: sub main::zit$no_proto vs \(\)/, 299b39c5158Smillert "about the prototype mismatch"); 300b39c5158Smillert 301b39c5158Smillert my $value = eval 'zit'; 302b39c5158Smillert is ($@, ''); 303b39c5158Smillert is ($value, 4); 304b39c5158Smillert} 305b39c5158Smillert 306b39c5158Smillert$fagwoosh = 'geronimo'; 307b39c5158Smillert$putt = 'leutwein'; 308b39c5158Smillert$kloong = 'schlozhauer'; 309b39c5158Smillert 310b39c5158Smillert{ 311b39c5158Smillert my @warnings; 312b39c5158Smillert local $SIG{'__WARN__'} = sub { push @warnings, @_ }; 313b39c5158Smillert eval 'use constant fagwoosh => 5; 1' or die $@; 314b39c5158Smillert 315b39c5158Smillert is ("@warnings", "", "No warnings if the typeglob exists already"); 316b39c5158Smillert 317b39c5158Smillert my $value = eval 'fagwoosh'; 318b39c5158Smillert is ($@, ''); 319b39c5158Smillert is ($value, 5); 320b39c5158Smillert 321b39c5158Smillert my @value = eval 'fagwoosh'; 322b39c5158Smillert is ($@, ''); 323b39c5158Smillert is_deeply (\@value, [5]); 324b39c5158Smillert 325b39c5158Smillert eval 'use constant putt => 6, 7; 1' or die $@; 326b39c5158Smillert 327b39c5158Smillert is ("@warnings", "", "No warnings if the typeglob exists already"); 328b39c5158Smillert 329b39c5158Smillert @value = eval 'putt'; 330b39c5158Smillert is ($@, ''); 331b39c5158Smillert is_deeply (\@value, [6, 7]); 332b39c5158Smillert 333b39c5158Smillert eval 'use constant "klong"; 1' or die $@; 334b39c5158Smillert 335b39c5158Smillert is ("@warnings", "", "No warnings if the typeglob exists already"); 336b39c5158Smillert 337b39c5158Smillert $value = eval 'klong'; 338b39c5158Smillert is ($@, ''); 339b39c5158Smillert is ($value, undef); 340b39c5158Smillert 341b39c5158Smillert @value = eval 'klong'; 342b39c5158Smillert is ($@, ''); 343b39c5158Smillert is_deeply (\@value, []); 344b39c5158Smillert} 345b39c5158Smillert 346b39c5158Smillert{ 347b39c5158Smillert local $SIG{'__WARN__'} = sub { die "WARNING: $_[0]" }; 348b39c5158Smillert eval 'use constant undef, 5; 1'; 349b39c5158Smillert like $@, qr/\ACan't use undef as constant name at /; 350b39c5158Smillert} 3516fb12b70Safresh1 3526fb12b70Safresh1# Constants created by "use constant" should be read-only 3536fb12b70Safresh1 3546fb12b70Safresh1# This test will not test what we are trying to test if this glob entry 3556fb12b70Safresh1# exists already, so test that, too. 3566fb12b70Safresh1ok !exists $::{immutable}; 3576fb12b70Safresh1eval q{ 3586fb12b70Safresh1 use constant immutable => 23987423874; 3596fb12b70Safresh1 for (immutable) { eval { $_ = 22 } } 3606fb12b70Safresh1 like $@, qr/^Modification of a read-only value attempted at /, 3616fb12b70Safresh1 'constant created in empty stash slot is immutable'; 3626fb12b70Safresh1 eval { for (immutable) { ${\$_} = 432 } }; 3636fb12b70Safresh1 SKIP: { 3646fb12b70Safresh1 require Config; 3656fb12b70Safresh1 if ($Config::Config{useithreads}) { 3666fb12b70Safresh1 skip "fails under threads", 1 if $] < 5.019003; 3676fb12b70Safresh1 } 3686fb12b70Safresh1 like $@, qr/^Modification of a read-only value attempted at /, 3696fb12b70Safresh1 '... and immutable through refgen, too'; 3706fb12b70Safresh1 } 3716fb12b70Safresh1}; 3726fb12b70Safresh1() = \&{"immutable"}; # reify 3736fb12b70Safresh1eval 'for (immutable) { $_ = 42 }'; 3746fb12b70Safresh1like $@, qr/^Modification of a read-only value attempted at /, 3756fb12b70Safresh1 '... and after reification'; 3766fb12b70Safresh1 3776fb12b70Safresh1# Use an existing stash element this time. 3786fb12b70Safresh1# This next line is sufficient to trigger a different code path in 3796fb12b70Safresh1# constant.pm. 3806fb12b70Safresh1() = \%::existing_stash_entry; 3816fb12b70Safresh1use constant existing_stash_entry => 23987423874; 3826fb12b70Safresh1for (existing_stash_entry) { eval { $_ = 22 } } 3836fb12b70Safresh1like $@, qr/^Modification of a read-only value attempted at /, 3846fb12b70Safresh1 'constant created in existing stash slot is immutable'; 3856fb12b70Safresh1eval { for (existing_stash_entry) { ${\$_} = 432 } }; 3866fb12b70Safresh1SKIP: { 3876fb12b70Safresh1 if ($Config::Config{useithreads}) { 3886fb12b70Safresh1 skip "fails under threads", 1 if $] < 5.019003; 3896fb12b70Safresh1 } 3906fb12b70Safresh1 like $@, qr/^Modification of a read-only value attempted at /, 3916fb12b70Safresh1 '... and immutable through refgen, too'; 3926fb12b70Safresh1} 3936fb12b70Safresh1 3946fb12b70Safresh1# Test that list constants are also immutable. This only works under 3956fb12b70Safresh1# 5.19.3 and later. 3966fb12b70Safresh1SKIP: { 3976fb12b70Safresh1 skip "fails under 5.19.2 and earlier", 3 if $] < 5.019003; 3986fb12b70Safresh1 local $TODO = "disabled for now; breaks CPAN; see perl #119045"; 3996fb12b70Safresh1 use constant constant_list => 1..2; 4006fb12b70Safresh1 for (constant_list) { 4016fb12b70Safresh1 my $num = $_; 4026fb12b70Safresh1 eval { $_++ }; 4036fb12b70Safresh1 like $@, qr/^Modification of a read-only value attempted at /, 4046fb12b70Safresh1 "list constant has constant elements ($num)"; 4056fb12b70Safresh1 } 4066fb12b70Safresh1 undef $TODO; 4076fb12b70Safresh1 # Whether values are modifiable or no, modifying them should not affect 4086fb12b70Safresh1 # future return values. 4096fb12b70Safresh1 my @values; 4106fb12b70Safresh1 for(1..2) { 4116fb12b70Safresh1 for ((constant_list)[0]) { 4126fb12b70Safresh1 push @values, $_; 4136fb12b70Safresh1 eval {$_++}; 4146fb12b70Safresh1 } 4156fb12b70Safresh1 } 4166fb12b70Safresh1 is $values[1], $values[0], 4176fb12b70Safresh1 'modifying list const elements does not affect future retavls'; 4186fb12b70Safresh1} 419b8851fccSafresh1 420b8851fccSafresh1use constant { "tahi" => 1, "rua::rua" => 2, "toru'toru" => 3 }; 421b8851fccSafresh1use constant "wha::wha" => 4; 422b8851fccSafresh1is tahi, 1, 'unqualified constant declared with constants in other pkgs'; 423b8851fccSafresh1is rua::rua, 2, 'constant declared with ::'; 424b8851fccSafresh1is toru::toru, 3, "constant declared with '"; 425b8851fccSafresh1is wha::wha, 4, 'constant declared by itself with ::'; 426