10 no warnings 'experimental::builtin';
13 package FetchStoreCounter {
14 sub TIESCALAR($class, @args) { bless \@args, $class }
16 sub FETCH($self) { $self->[0]->$*++ }
17 sub STORE($self, $) { $self->[1]->$*++ }
22 use builtin qw( true false is_bool );
24 ok(true, 'true is true');
25 ok(!false, 'false is false');
27 ok(is_bool(true), 'true is bool');
28 ok(is_bool(false), 'false is bool');
29 ok(!is_bool(undef), 'undef is not bool');
30 ok(!is_bool(1), '1 is not bool');
31 ok(!is_bool(""), 'empty is not bool');
33 my $truevar = (5 == 5);
34 my $falsevar = (5 == 6);
36 ok(is_bool($truevar), '$truevar is bool');
37 ok(is_bool($falsevar), '$falsevar is bool');
39 ok(is_bool(is_bool(true)), 'is_bool true is bool');
40 ok(is_bool(is_bool(123)), 'is_bool false is bool');
44 tie my $tied, FetchStoreCounter => (\my $fetchcount, \my $storecount);
46 my $_dummy = is_bool($tied);
47 is($fetchcount, 1, 'is_bool() invokes FETCH magic');
49 $tied = is_bool(false);
50 is($storecount, 1, 'is_bool() invokes STORE magic');
52 is(prototype(\&builtin::is_bool), '$', 'is_bool prototype');
57 use builtin qw( inf nan );
59 if ($Config{d_double_has_inf}) {
60 ok(inf, 'inf is true');
61 ok(inf > 1E10, 'inf is bigger than 1E10');
62 ok(inf == inf, 'inf is equal to inf');
63 ok(inf == inf + 1, 'inf is equal to inf + 1');
65 # Invoke the real XSUB
66 my $inf = ( \&builtin::inf )->();
67 ok($inf == $inf + 1, 'inf returned by real xsub');
69 is(eval { inf }, undef, 'inf throws');
71 like($e, qr/^builtin::inf not implemented at/, 'inf fails with correct error');
74 if ($Config{d_double_has_nan}) {
75 ok(nan != nan, 'NaN is not equal to NaN');
77 my $nan = ( \&builtin::nan )->();
78 ok($nan != $nan, 'NaN returned by real xsub');
80 is(eval { nan }, undef, 'nan throws');
82 like($e, qr/^builtin::nan not implemented at/, 'nan fails with correct error');
88 use builtin qw( is_weak weaken unweaken );
93 ok(!is_weak($ref), 'ref is not weak initially');
96 ok(is_weak($ref), 'ref is weak after weaken()');
99 ok(!is_weak($ref), 'ref is not weak after unweaken()');
103 is($ref, undef, 'ref is now undef after arr is cleared');
105 is(prototype(\&builtin::weaken), '$', 'weaken prototype');
106 is(prototype(\&builtin::unweaken), '$', 'unweaken prototype');
107 is(prototype(\&builtin::is_weak), '$', 'is_weak prototype');
112 use builtin qw( refaddr reftype blessed );
115 my $obj = bless [], "Object";
117 is(refaddr($arr), $arr+0, 'refaddr yields same as ref in numeric context');
118 is(refaddr("not a ref"), undef, 'refaddr yields undef for non-reference');
120 is(reftype($arr), "ARRAY", 'reftype yields type string');
121 is(reftype($obj), "ARRAY", 'reftype yields basic container type for blessed object');
122 is(reftype("not a ref"), undef, 'reftype yields undef for non-reference');
124 is(blessed($arr), undef, 'blessed yields undef for non-object');
125 is(blessed($obj), "Object", 'blessed yields package name for object');
127 # blessed() as a boolean
128 is(blessed($obj) ? "YES" : "NO", "YES", 'blessed in boolean context still works');
130 # blessed() appears false as a boolean on package "0"
131 is(blessed(bless [], "0") ? "YES" : "NO", "NO", 'blessed in boolean context handles "0" cornercase');
133 is(prototype(\&builtin::blessed), '$', 'blessed prototype');
134 is(prototype(\&builtin::refaddr), '$', 'refaddr prototype');
135 is(prototype(\&builtin::reftype), '$', 'reftype prototype');
140 use builtin qw( created_as_string created_as_number );
142 # some literal constants
143 ok(!created_as_string(undef), 'undef created as !string');
144 ok(!created_as_number(undef), 'undef created as !number');
146 ok( created_as_string("abc"), 'abc created as string');
147 ok(!created_as_number("abc"), 'abc created as number');
149 ok(!created_as_string(123), '123 created as !string');
150 ok( created_as_number(123), '123 created as !number');
152 ok(!created_as_string(1.23), '1.23 created as !string');
153 ok( created_as_number(1.23), '1.23 created as !number');
155 ok(!created_as_string([]), '[] created as !string');
156 ok(!created_as_number([]), '[] created as !number');
158 ok(!created_as_string(builtin::true), 'true created as !string');
159 ok(!created_as_number(builtin::true), 'true created as !number');
161 ok(builtin::is_bool(created_as_string(0)), 'created_as_string returns bool');
162 ok(builtin::is_bool(created_as_number(0)), 'created_as_number returns bool');
166 ok( created_as_string($just_pv), 'def created as string');
167 ok(!created_as_number($just_pv), 'def created as number');
170 ok(!created_as_string($just_iv), '456 created as string');
171 ok( created_as_number($just_iv), '456 created as number');
174 ok(!created_as_string($just_nv), '456 created as string');
175 ok( created_as_number($just_nv), '456 created as number');
178 my $originally_pv = "1";
179 my $pv_as_iv = $originally_pv + 0;
180 ok( created_as_string($originally_pv), 'PV reused as IV created as string');
181 ok(!created_as_number($originally_pv), 'PV reused as IV created as !number');
182 ok(!created_as_string($pv_as_iv), 'New number from PV created as !string');
183 ok( created_as_number($pv_as_iv), 'New number from PV created as number');
185 my $originally_iv = 1;
186 my $iv_as_pv = "$originally_iv";
187 ok(!created_as_string($originally_iv), 'IV reused as PV created as !string');
188 ok( created_as_number($originally_iv), 'IV reused as PV created as number');
189 ok( created_as_string($iv_as_pv), 'New string from IV created as string');
190 ok(!created_as_number($iv_as_pv), 'New string from IV created as !number');
192 my $originally_nv = 1.1;
193 my $nv_as_pv = "$originally_nv";
194 ok(!created_as_string($originally_nv), 'NV reused as PV created as !string');
195 ok( created_as_number($originally_nv), 'NV reused as PV created as number');
196 ok( created_as_string($nv_as_pv), 'New string from NV created as string');
197 ok(!created_as_number($nv_as_pv), 'New string from NV created as !number');
202 ok(created_as_string($1), 'magic string');
204 is(prototype(\&builtin::created_as_string), '$', 'created_as_string prototype');
205 is(prototype(\&builtin::created_as_number), '$', 'created_as_number prototype');
210 use builtin qw( stringify );
212 is(stringify("abc"), "abc", 'stringify a plain string');
213 is(stringify(123), "123", 'stringify a number');
216 is(stringify($aref), "$aref", 'stringify an array ref');
218 use builtin qw( created_as_string );
219 ok(!ref stringify($aref), 'stringified arrayref is not a ref');
220 ok(created_as_string(stringify($aref)), 'stringified arrayref is created as string');
222 package WithOverloadedStringify {
223 use overload '""' => sub { return "STRING" };
226 is(stringify(bless [], "WithOverloadedStringify"), "STRING", 'stringify invokes "" overload');
231 use builtin qw( ceil floor );
233 cmp_ok(ceil(1.5), '==', 2, 'ceil(1.5) == 2');
234 cmp_ok(floor(1.5), '==', 1, 'floor(1.5) == 1');
238 tie my $tied, FetchStoreCounter => (\my $fetchcount, \my $storecount);
240 my $_dummy = ceil($tied);
241 is($fetchcount, 1, 'ceil() invokes FETCH magic');
244 is($storecount, 1, 'ceil() TARG invokes STORE magic');
246 $fetchcount = $storecount = 0;
247 tie $tied, FetchStoreCounter => (\$fetchcount, \$storecount);
249 $_dummy = floor($tied);
250 is($fetchcount, 1, 'floor() invokes FETCH magic');
253 is($storecount, 1, 'floor() TARG invokes STORE magic');
255 is(prototype(\&builtin::ceil), '$', 'ceil prototype');
256 is(prototype(\&builtin::floor), '$', 'floor prototype');
259 # imports are lexical; should not be visible here
261 my $ok = eval 'true()'; my $e = $@;
262 ok(!$ok, 'true() not visible outside of lexical scope');
263 like($e, qr/^Undefined subroutine &main::true called at /, 'failure from true() not visible');
266 # lexical imports work fine in a variety of situations
272 ok(regularfunc(), 'true in regular sub');
278 ok(lexicalfunc(), 'true in lexical sub');
284 ok($coderef->(), 'true in anon sub');
288 return recursefunc() if @_;
291 ok(recursefunc("rec"), 'true in self-recursive sub');
293 my sub recurselexicalfunc {
295 return __SUB__->() if @_;
298 ok(recurselexicalfunc("rec"), 'true in self-recursive lexical sub');
300 my $recursecoderef = sub {
302 return __SUB__->() if @_;
305 ok($recursecoderef->("rec"), 'true in self-recursive anon sub');
309 use builtin qw( true false );
312 cmp_ok($val, $_, !!1, "true is equivalent to !!1 by $_") for qw( eq == );
313 cmp_ok($val, $_, !0, "true is equivalent to !0 by $_") for qw( eq == );
316 cmp_ok($val, $_, !!0, "false is equivalent to !!0 by $_") for qw( eq == );
317 cmp_ok($val, $_, !1, "false is equivalent to !1 by $_") for qw( eq == );
322 use builtin qw( indexed );
324 # We don't have Test::More's is_deeply here
326 ok(eq_array([indexed], [] ),
327 'indexed on empty list');
329 ok(eq_array([indexed "A"], [0, "A"] ),
330 'indexed on singleton list');
332 ok(eq_array([indexed "X" .. "Z"], [0, "X", 1, "Y", 2, "Z"] ),
333 'indexed on 3-item list');
336 $_++ for indexed @orig;
337 ok(eq_array(\@orig, [1 .. 3]), 'indexed copies values, does not alias');
341 foreach my ($len, $s) (indexed "", "x", "xx") {
342 length($s) == $len or undef $ok;
344 ok($ok, 'indexed operates nicely with multivar foreach');
348 my %hash = indexed "a" .. "e";
349 ok(eq_hash(\%hash, { 0 => "a", 1 => "b", 2 => "c", 3 => "d", 4 => "e" }),
350 'indexed can be used to create hashes');
354 no warnings 'scalar';
356 my $count = indexed 'i', 'ii', 'iii', 'iv';
357 is($count, 8, 'indexed in scalar context yields size of list it would return');
360 is(prototype(\&builtin::indexed), '@', 'indexed prototype');
363 # indexed + foreach loop optimisation appears transparent
366 my @input = qw( zero one two three four five );
368 foreach my ( $idx, $val ) ( builtin::indexed @input ) {
369 push @output, "[$idx]=$val";
372 ok(eq_array(\@output, [qw( [0]=zero [1]=one [2]=two [3]=three [4]=four [5]=five )] ),
373 'foreach + builtin::indexed ARRAY' );
377 use builtin qw( indexed );
379 foreach my ( $idx, $val ) ( indexed @input ) {
380 push @output, "[$idx]=$val";
383 ok(eq_array(\@output, [qw( [0]=zero [1]=one [2]=two [3]=three [4]=four [5]=five )] ),
384 'foreach + imported indexed ARRAY' );
388 foreach my ( $idx, $val ) ( builtin::indexed qw( six seven eight nine ) ) {
389 push @output, "[$idx]=$val";
392 ok(eq_array(\@output, [qw( [0]=six [1]=seven [2]=eight [3]=nine )] ),
393 'foreach + builtin::indexed LIST' );
398 use builtin qw( trim );
400 is(trim(" Hello world! ") , "Hello world!" , 'trim spaces');
401 is(trim("\tHello world!\t") , "Hello world!" , 'trim tabs');
402 is(trim("\n\n\nHello\nworld!\n") , "Hello\nworld!" , 'trim \n');
403 is(trim("\t\n\n\nHello world!\n \t"), "Hello world!" , 'trim all three');
404 is(trim("Perl") , "Perl" , 'trim nothing');
405 is(trim('') , "" , 'trim empty string');
407 is(prototype(\&builtin::trim), '$', 'trim prototype');
412 local $SIG{__WARN__} = sub { $warn .= join "", @_; };
414 is(builtin::trim(undef), "", 'trim undef');
415 like($warn , qr/^Use of uninitialized value in subroutine entry at/,
416 'trim undef triggers warning');
417 local $main::TODO = "Currently uses generic value for the name of non-opcode builtins";
418 like($warn , qr/^Use of uninitialized value in trim at/,
419 'trim undef triggers warning using actual name of builtin');
422 # Fancier trim tests against a regexp and unicode
424 use builtin qw( trim );
425 my $nbsp = chr utf8::unicode_to_native(0xA0);
427 is(trim(" \N{U+2603} "), "\N{U+2603}", 'trim with unicode content');
428 is(trim("\N{U+2029}foobar\x{2028} "), "foobar",
429 'trim with unicode whitespace');
430 is(trim("$nbsp foobar$nbsp "), "foobar", 'trim with latin1 whitespace');
433 # Test on a magical fetching variable
435 use builtin qw( trim );
437 my $str3 = " Hello world!\t";
438 $str3 =~ m/(.+Hello)/;
439 is(trim($1), "Hello", "trim on a magical variable");
442 # Inplace edit, my, our variables
444 use builtin qw( trim );
446 my $str4 = "\t\tHello world!\n\n";
448 is($str4, "Hello world!", "trim on an inplace variable");
450 our $str2 = "\t\nHello world!\t ";
451 is(trim($str2), "Hello world!", "trim on an our \$var");
458 use builtin qw( export_lexically );
461 export_lexically $name => sub { "Hello, world" };
464 is(message(), "Hello, world", 'Lexically exported sub is callable');
465 ok(!__PACKAGE__->can("message"), 'Exported sub is not visible via ->can');
467 is($name, "message", '$name argument was not modified by export_lexically');
469 our ( $scalar, @array, %hash );
471 use builtin qw( export_lexically );
474 '$SCALAR' => \$scalar,
480 is($SCALAR, "value", 'Lexically exported scalar is accessible');
482 @::array = ('a' .. 'e');
483 is(scalar @ARRAY, 5, 'Lexically exported array is accessible');
485 %::hash = (key => "val");
486 is($HASH{key}, "val", 'Lexically exported hash is accessible');
491 use builtin qw( load_module );
492 use feature qw( try );
495 # Can't really test this sans string eval, as it's a compilation error:
496 eval 'load_module();';
498 ok($e, 'load_module(); fails');
499 like($e, qr/^Not enough arguments for builtin::load_module at/, 'load_module(); fails with correct error');
502 ok($e, 'load_module; fails');
503 like($e, qr/^Not enough arguments for builtin::load_module at/, 'load_module; fails with correct error');
505 # Failure to load module croaks
509 ok($e, 'load_module(undef) fails');
510 like($e, qr/^Usage: builtin::load_module\(defined string\)/, 'load_module(undef) fails with correct error');
515 ok($e, 'load_module(\"Foo") fails');
516 like($e, qr/^Usage: builtin::load_module\(defined string\)/, 'load_module(\"Foo") fails with correct error');
519 load_module(["Foo"]);
521 ok($e, 'load_module(["Foo"]) fails');
522 like($e, qr/^Usage: builtin::load_module\(defined string\)/, 'load_module(["Foo"]) fails with correct error');
528 ok($e, 'load_module("5.36") fails');
529 like($e, qr/^Can't locate 5[.]36[.]pm in \@INC/, 'load_module("5.36") fails with correct error');
532 load_module('v5.36');
535 ok($e, 'load_module("v5.36") fails');
536 like($e, qr/^Can't locate v5[.]36[.]pm in \@INC/, 'load_module("v5.36") fails with correct error');
540 fail('load_module("Dies") succeeded!');
543 ok($e, 'load_module("Dies") fails');
544 like($e, qr/^Can't locate Dies[.]pm in \@INC/, 'load_module("Dies") fails with correct error');
546 my $module_name = 'Dies';
548 load_module($module_name);
549 fail('load_module($module_name) $module_name=Dies succeeded!');
552 ok($e, 'load_module($module_name) $module_name=Dies fails');
553 like($e, qr/^Can't locate Dies[.]pm in \@INC/, 'load_module($module_name) $module_name=Dies fails with correct error');
555 $module_name =~ m!(\w+)!;
558 fail('load_module($1) from $module_name=Dies succeeded!');
561 ok($e, 'load_module($1) from $module_name=Dies fails');
562 like($e, qr/^Can't locate Dies[.]pm in \@INC/, 'load_module($1) from $module_name=Dies fails with correct error');
567 fail('load_module($1) from "Dies" succeeded!');
570 ok($e, 'load_module($1) from "Dies" fails');
571 like($e, qr/^Can't locate Dies[.]pm in \@INC/, 'load_module($1) from "Dies" fails with correct error');
574 # Loading module goes well
577 $ret = load_module("strict");
578 pass('load_module("strict") worked');
579 is($ret, "strict", 'load_module("strict") returned "strict"');
582 fail('load_module("strict") errored: ' . $e);
584 $module_name = 'strict';
586 $ret = load_module($module_name);
587 pass('load_module($module_name) $module_name=strict worked');
588 is($ret, "strict", 'load_module($module_name) returned "strict"');
591 fail('load_module($module_name) $module_name=strict errored: ' . $e);
593 $module_name =~ m!(\w+)!;
595 $ret = load_module($1);
596 pass('load_module($1) from $module_name=strict worked');
597 is($ret, "strict", 'load_module($1) from $module_name=strict returned "strict"');
600 fail('load_module($1) from $module_name=strict errored: ' . $e);
602 "strict" =~ m!(\w+)!;
604 $ret = load_module($1);
605 pass('load_module($1) from "strict" worked');
606 is($ret, "strict", 'load_module($1) from "strict" returned "strict"');
609 fail('load_module($1) from "strict" errored: ' . $e);
612 # Slightly more complex, based on tie
614 package BuiltinTestTie {
623 tie my $y, BuiltinTestTie => \$x;
626 $ret = load_module($y);
627 pass('load_module($y) from $y tied to $x=strict worked');
628 is($ret, "strict", 'load_module($y) from $y tied to $x=strict worked and returned "strict"');
631 fail('load_module($y) from $y tied to $x=strict failed: ' . $e);
635 # Can be used to import a symbol to the current namespace, too:
638 my $aref_stringified = "$aref";
641 load_module("builtin")->import("stringify");
645 if (my $error = $@) {
646 fail('load_module("builtin")->import("stringify") failed: ' . $error);
648 is($got, $aref_stringified, 'load_module("builtin")->import("stringify") works, stringifying $aref');
655 ok(true, 'true() is available from :5.39 bundle');
658 foreach my $bundle (qw( :x :5.x :5.36x :5.36.1000 :5.1000 :5.36.1.2 ),
659 ": +5.+39", ": +5.+40. -10", ": 5.40", ":5 .40", ":5.+40",
660 ":5.40 .0", ":5.40.-10", ":5.40\0") {
661 (my $pretty_bundle = $bundle) =~ s/([^[:print:]])/ sprintf("\\%o", ord $1) /ge;
662 ok(!defined eval "use builtin '$bundle';", $pretty_bundle.' is invalid bundle');
663 like($@, qr/^Invalid version bundle "\Q$pretty_bundle\E" at /);
669 fresh_perl_is(<<'EOS', "", {}, "github 21981: panic in intro_my");
671 BEGIN { B::save_BEGINs; }
678 # some of these functions don't error at this point, but they might be updated
679 # and see the same problem we fix here
680 for my $func (qw(is_bool is_weak blessed refaddr reftype ceil floor is_tainted
681 trim stringify created_as_string created_as_number)) {
683 $func =~ /ceil|floor|created_as/ ? "1.1" :
684 $func =~ /(^ref|blessed|is_weak)/ ? "\\1" : '"abc"';
685 fresh_perl_is(<<"EOS", "ok", {}, "goto $func");
686 no warnings "experimental";
687 sub f { goto &builtin::$func }
696 use builtin qw( trim );
697 sub f { 0+trim($_[0]) }
698 is(f(4), 4, "populate TARG.iv");
699 is(f(123), 123, "check TARG.IOK is reset properly");
702 # vim: tabstop=4 shiftwidth=4 expandtab autoindent softtabstop=4