10 no warnings 'experimental::builtin';
12 package FetchStoreCounter {
13 sub TIESCALAR($class, @args) { bless \@args, $class }
15 sub FETCH($self) { $self->[0]->$*++ }
16 sub STORE($self, $) { $self->[1]->$*++ }
21 use builtin qw( true false is_bool );
23 ok(true, 'true is true');
24 ok(!false, 'false is false');
26 ok(is_bool(true), 'true is bool');
27 ok(is_bool(false), 'false is bool');
28 ok(!is_bool(undef), 'undef is not bool');
29 ok(!is_bool(1), '1 is not bool');
30 ok(!is_bool(""), 'empty is not bool');
32 my $truevar = (5 == 5);
33 my $falsevar = (5 == 6);
35 ok(is_bool($truevar), '$truevar is bool');
36 ok(is_bool($falsevar), '$falsevar is bool');
38 ok(is_bool(is_bool(true)), 'is_bool true is bool');
39 ok(is_bool(is_bool(123)), 'is_bool false is bool');
43 tie my $tied, FetchStoreCounter => (\my $fetchcount, \my $storecount);
45 my $_dummy = is_bool($tied);
46 is($fetchcount, 1, 'is_bool() invokes FETCH magic');
48 $tied = is_bool(false);
49 is($storecount, 1, 'is_bool() invokes STORE magic');
51 is(prototype(\&builtin::is_bool), '$', 'is_bool prototype');
56 use builtin qw( is_weak weaken unweaken );
61 ok(!is_weak($ref), 'ref is not weak initially');
64 ok(is_weak($ref), 'ref is weak after weaken()');
67 ok(!is_weak($ref), 'ref is not weak after unweaken()');
71 is($ref, undef, 'ref is now undef after arr is cleared');
73 is(prototype(\&builtin::weaken), '$', 'weaken prototype');
74 is(prototype(\&builtin::unweaken), '$', 'unweaken prototype');
75 is(prototype(\&builtin::is_weak), '$', 'is_weak prototype');
80 use builtin qw( refaddr reftype blessed );
83 my $obj = bless [], "Object";
85 is(refaddr($arr), $arr+0, 'refaddr yields same as ref in numeric context');
86 is(refaddr("not a ref"), undef, 'refaddr yields undef for non-reference');
88 is(reftype($arr), "ARRAY", 'reftype yields type string');
89 is(reftype($obj), "ARRAY", 'reftype yields basic container type for blessed object');
90 is(reftype("not a ref"), undef, 'reftype yields undef for non-reference');
92 is(blessed($arr), undef, 'blessed yields undef for non-object');
93 is(blessed($obj), "Object", 'blessed yields package name for object');
95 # blessed() as a boolean
96 is(blessed($obj) ? "YES" : "NO", "YES", 'blessed in boolean context still works');
98 # blessed() appears false as a boolean on package "0"
99 is(blessed(bless [], "0") ? "YES" : "NO", "NO", 'blessed in boolean context handles "0" cornercase');
101 is(prototype(\&builtin::blessed), '$', 'blessed prototype');
102 is(prototype(\&builtin::refaddr), '$', 'refaddr prototype');
103 is(prototype(\&builtin::reftype), '$', 'reftype prototype');
108 use builtin qw( created_as_string created_as_number );
110 # some literal constants
111 ok(!created_as_string(undef), 'undef created as !string');
112 ok(!created_as_number(undef), 'undef created as !number');
114 ok( created_as_string("abc"), 'abc created as string');
115 ok(!created_as_number("abc"), 'abc created as number');
117 ok(!created_as_string(123), '123 created as !string');
118 ok( created_as_number(123), '123 created as !number');
120 ok(!created_as_string(1.23), '1.23 created as !string');
121 ok( created_as_number(1.23), '1.23 created as !number');
123 ok(!created_as_string([]), '[] created as !string');
124 ok(!created_as_number([]), '[] created as !number');
126 ok(!created_as_string(builtin::true), 'true created as !string');
127 ok(!created_as_number(builtin::true), 'true created as !number');
129 ok(builtin::is_bool(created_as_string(0)), 'created_as_string returns bool');
130 ok(builtin::is_bool(created_as_number(0)), 'created_as_number returns bool');
134 ok( created_as_string($just_pv), 'def created as string');
135 ok(!created_as_number($just_pv), 'def created as number');
138 ok(!created_as_string($just_iv), '456 created as string');
139 ok( created_as_number($just_iv), '456 created as number');
142 ok(!created_as_string($just_nv), '456 created as string');
143 ok( created_as_number($just_nv), '456 created as number');
146 my $originally_pv = "1";
147 my $pv_as_iv = $originally_pv + 0;
148 ok( created_as_string($originally_pv), 'PV reused as IV created as string');
149 ok(!created_as_number($originally_pv), 'PV reused as IV created as !number');
150 ok(!created_as_string($pv_as_iv), 'New number from PV created as !string');
151 ok( created_as_number($pv_as_iv), 'New number from PV created as number');
153 my $originally_iv = 1;
154 my $iv_as_pv = "$originally_iv";
155 ok(!created_as_string($originally_iv), 'IV reused as PV created as !string');
156 ok( created_as_number($originally_iv), 'IV reused as PV created as number');
157 ok( created_as_string($iv_as_pv), 'New string from IV created as string');
158 ok(!created_as_number($iv_as_pv), 'New string from IV created as !number');
160 my $originally_nv = 1.1;
161 my $nv_as_pv = "$originally_nv";
162 ok(!created_as_string($originally_nv), 'NV reused as PV created as !string');
163 ok( created_as_number($originally_nv), 'NV reused as PV created as number');
164 ok( created_as_string($nv_as_pv), 'New string from NV created as string');
165 ok(!created_as_number($nv_as_pv), 'New string from NV created as !number');
170 ok(created_as_string($1), 'magic string');
172 is(prototype(\&builtin::created_as_string), '$', 'created_as_string prototype');
173 is(prototype(\&builtin::created_as_number), '$', 'created_as_number prototype');
178 use builtin qw( ceil floor );
180 cmp_ok(ceil(1.5), '==', 2, 'ceil(1.5) == 2');
181 cmp_ok(floor(1.5), '==', 1, 'floor(1.5) == 1');
185 tie my $tied, FetchStoreCounter => (\my $fetchcount, \my $storecount);
187 my $_dummy = ceil($tied);
188 is($fetchcount, 1, 'ceil() invokes FETCH magic');
191 is($storecount, 1, 'ceil() TARG invokes STORE magic');
193 $fetchcount = $storecount = 0;
194 tie $tied, FetchStoreCounter => (\$fetchcount, \$storecount);
196 $_dummy = floor($tied);
197 is($fetchcount, 1, 'floor() invokes FETCH magic');
200 is($storecount, 1, 'floor() TARG invokes STORE magic');
202 is(prototype(\&builtin::ceil), '$', 'ceil prototype');
203 is(prototype(\&builtin::floor), '$', 'floor prototype');
206 # imports are lexical; should not be visible here
208 my $ok = eval 'true()'; my $e = $@;
209 ok(!$ok, 'true() not visible outside of lexical scope');
210 like($e, qr/^Undefined subroutine &main::true called at /, 'failure from true() not visible');
213 # lexical imports work fine in a variety of situations
219 ok(regularfunc(), 'true in regular sub');
225 ok(lexicalfunc(), 'true in lexical sub');
231 ok($coderef->(), 'true in anon sub');
235 return recursefunc() if @_;
238 ok(recursefunc("rec"), 'true in self-recursive sub');
240 my $recursecoderef = sub {
241 use feature 'current_sub';
243 return __SUB__->() if @_;
246 ok($recursecoderef->("rec"), 'true in self-recursive anon sub');
250 use builtin qw( true false );
253 cmp_ok($val, $_, !!1, "true is equivalent to !!1 by $_") for qw( eq == );
254 cmp_ok($val, $_, !0, "true is equivalent to !0 by $_") for qw( eq == );
257 cmp_ok($val, $_, !!0, "false is equivalent to !!0 by $_") for qw( eq == );
258 cmp_ok($val, $_, !1, "false is equivalent to !1 by $_") for qw( eq == );
263 use builtin qw( indexed );
265 # We don't have Test::More's is_deeply here
267 ok(eq_array([indexed], [] ),
268 'indexed on empty list');
270 ok(eq_array([indexed "A"], [0, "A"] ),
271 'indexed on singleton list');
273 ok(eq_array([indexed "X" .. "Z"], [0, "X", 1, "Y", 2, "Z"] ),
274 'indexed on 3-item list');
277 $_++ for indexed @orig;
278 ok(eq_array(\@orig, [1 .. 3]), 'indexed copies values, does not alias');
281 no warnings 'experimental::for_list';
284 foreach my ($len, $s) (indexed "", "x", "xx") {
285 length($s) == $len or undef $ok;
287 ok($ok, 'indexed operates nicely with multivar foreach');
291 my %hash = indexed "a" .. "e";
292 ok(eq_hash(\%hash, { 0 => "a", 1 => "b", 2 => "c", 3 => "d", 4 => "e" }),
293 'indexed can be used to create hashes');
297 no warnings 'scalar';
299 my $count = indexed 'i', 'ii', 'iii', 'iv';
300 is($count, 8, 'indexed in scalar context yields size of list it would return');
306 use builtin qw( trim );
308 is(trim(" Hello world! ") , "Hello world!" , 'trim spaces');
309 is(trim("\tHello world!\t") , "Hello world!" , 'trim tabs');
310 is(trim("\n\n\nHello\nworld!\n") , "Hello\nworld!" , 'trim \n');
311 is(trim("\t\n\n\nHello world!\n \t"), "Hello world!" , 'trim all three');
312 is(trim("Perl") , "Perl" , 'trim nothing');
313 is(trim('') , "" , 'trim empty string');
315 is(prototype(\&builtin::trim), '$', 'trim prototype');
320 local $SIG{__WARN__} = sub { $warn .= join "", @_; };
322 is(builtin::trim(undef), "", 'trim undef');
323 like($warn , qr/^Use of uninitialized value in subroutine entry at/,
324 'trim undef triggers warning');
325 local $main::TODO = "Currently uses generic value for the name of non-opcode builtins";
326 like($warn , qr/^Use of uninitialized value in trim at/,
327 'trim undef triggers warning using actual name of builtin');
330 # Fancier trim tests against a regexp and unicode
332 use builtin qw( trim );
333 my $nbsp = chr utf8::unicode_to_native(0xA0);
335 is(trim(" \N{U+2603} "), "\N{U+2603}", 'trim with unicode content');
336 is(trim("\N{U+2029}foobar\x{2028} "), "foobar",
337 'trim with unicode whitespace');
338 is(trim("$nbsp foobar$nbsp "), "foobar", 'trim with latin1 whitespace');
341 # Test on a magical fetching variable
343 use builtin qw( trim );
345 my $str3 = " Hello world!\t";
346 $str3 =~ m/(.+Hello)/;
347 is(trim($1), "Hello", "trim on a magical variable");
350 # Inplace edit, my, our variables
352 use builtin qw( trim );
354 my $str4 = "\t\tHello world!\n\n";
356 is($str4, "Hello world!", "trim on an inplace variable");
358 our $str2 = "\t\nHello world!\t ";
359 is(trim($str2), "Hello world!", "trim on an our \$var");
364 use builtin qw( is_tainted );
366 is(is_tainted($0), !!${^TAINT}, "\$0 is tainted (if tainting is supported)");
367 ok(!is_tainted($1), "\$1 isn't tainted");
370 tie my $tied, FetchStoreCounter => (\my $fetchcount, \my $storecount);
372 my $_dummy = is_tainted($tied);
373 is($fetchcount, 1, 'is_tainted() invokes FETCH magic');
375 $tied = is_tainted($0);
376 is($storecount, 1, 'is_tainted() invokes STORE magic');
378 is(prototype(\&builtin::is_tainted), '$', 'is_tainted prototype');
385 use builtin qw( export_lexically );
388 export_lexically $name => sub { "Hello, world" };
391 is(message(), "Hello, world", 'Lexically exported sub is callable');
392 ok(!__PACKAGE__->can("message"), 'Exported sub is not visible via ->can');
394 is($name, "message", '$name argument was not modified by export_lexically');
396 our ( $scalar, @array, %hash );
398 use builtin qw( export_lexically );
401 '$SCALAR' => \$scalar,
407 is($SCALAR, "value", 'Lexically exported scalar is accessible');
409 @::array = ('a' .. 'e');
410 is(scalar @ARRAY, 5, 'Lexically exported array is accessible');
412 %::hash = (key => "val");
413 is($HASH{key}, "val", 'Lexically exported hash is accessible');
416 # vim: tabstop=4 shiftwidth=4 expandtab autoindent softtabstop=4