4 # various typeglob tests
14 use open qw( :utf8 :std );
19 # type coersion on assignment
23 is(ref(\$ᴮᛅ), 'SCALAR');
26 # type coersion (not) on misc ops
31 unlike ($ᕘ, qr/abcd/);
40 is(ref(\$ᕘ), 'GLOB', 'no type coersion when assigning to *{} retval');
44 'symbolic *{} returns symtab entry when FAKE'
48 ref(\$::{ఫケ}), 'GLOB',
49 'no type coersion when assigning to retval of symbolic *{}'
54 \$::{pɥአQuઍ}, \*pɥአQuઍ,
55 "compile-time *{} returns symtab entry when FAKE"
60 ref(\$::{pɥአQuઍ}), 'GLOB',
61 'no type coersion when assigning to retval of compile-time *{}'
65 # type coersion on substitutions that match
74 # typeglobs as lvalues
75 substr($ᕘ, 0, 1) = "XXX";
76 is(ref(\$ᕘ), 'SCALAR');
77 is($ᕘ, 'XXXmain::ᴮᛅ');
79 # returning glob values
81 local($ᴮᛅ) = *main::ᕘ;
88 is(ref(\$ፉṶ), 'GLOB');
94 # nested package globs
95 # NOTE: It's probably OK if these semantics change, because the
96 # fact that %X::Y:: is stored in %X:: isn't documented.
99 { package ฝ오::ʉ; no warnings 'once'; $test=1; }
100 ok(exists $ฝ오::{'ʉ::'});
101 is($ฝ오::{'ʉ::'}, '*ฝ오::ʉ::');
104 # test undef operator clearing out entire glob
107 %ᕘ = qw(even more random stuff);
114 # test warnings from assignment of undef to glob
116 local $SIG{__WARN__} = sub { $msg = $_[0] };
121 like($msg, qr/Undefined value assigned to typeglob/);
124 # test warnings for converting globs to other forms
126 foreach ($copy, *SKRÈÈÈ) {
128 my $victim = sprintf "%d", $_;
129 like($msg, qr/Argument "\*main::(\p{ASCII}|\Q\x{\E\p{ASCII_Hex_Digit}{2}\}){3}\Q...\E" isn't numeric in sprintf/,
130 "Warning on conversion to IV");
134 $victim = sprintf "%u", $_;
135 like($msg, qr/Argument "\*main::(\p{ASCII}|\Q\x{\E\p{ASCII_Hex_Digit}{2}\}){3}\Q...\E" isn't numeric in sprintf/,
136 "Warning on conversion to UV");
140 $victim = sprintf "%e", $_;
141 like($msg, qr/Argument "\*main::(\p{ASCII}|\Q\x{\E\p{ASCII_Hex_Digit}{2}\}){3}\Q...\E" isn't numeric in sprintf/,
142 "Warning on conversion to NV");
143 like($victim, qr/^0\.0+E\+?00/i, "Expect floating point zero");
146 $victim = sprintf "%s", $_;
147 is($msg, '', "No warning on stringification");
148 is($victim, '' . $_);
152 my $test = curr_test();
153 # test *glob{THING} syntax
158 %Ẋ = ("ok $test" => "\n");
160 sub Ẋ { "ok $test\n" }
161 print ${*Ẋ{SCALAR}}, @{*Ẋ{ARRAY}}, %{*Ẋ{HASH}}, &{*Ẋ{CODE}};
162 # This needs to go here, after the print, as sub Ẋ will return the current
166 XXX This text isn't used. Should it be?
170 is (ref *Ẋ{FORMAT}, "FORMAT");
172 is (*{*Ẋ{GLOB}}, "*main::STDOUT");
175 my $test = curr_test();
177 print {*Ẋ{IO}} "ok $test\n";
181 local $SIG{__WARN__} = sub {
184 my $val = *Ẋ{FILEHANDLE};
185 print {*Ẋ{IO}} ($warn =~ /is deprecated/
186 ? "ok $test\n" : "not ok $test\n");
192 # test if defined() doesn't create any new symbols
201 no warnings 'deprecated';
213 *{$a} = sub { $state = "ok" };
220 # [ID 20010526.001] localized glob loses value when assigned to
222 $J=1; %J=(a=>1); @J=(1); local *J=*J; *J = sub{};
229 # does pp_readline() handle glob-ness correctly?
237 local $SIG{__WARN__} = sub { $w = $_[0] };
239 local *aʙȼ1 = sub { };
247 like ($w, qr/Prototype mismatch/);
251 # [17375] rcatline to formerly-defined undef was broken. Fixed in
252 # do_readline by checking SvOK. AMS, 20020918
260 # test the assignment of a GLOB to an LVALUE
262 local $SIG{__DIE__} = sub { $e = $_[0] };
264 sub ƒ { $_[0] = 0; $_[0] = "a"; $_[0] = *DATA }
266 is ($V{V}, '*main::DATA');
267 is (ref\$V{V}, 'GLOB', 'lvalue assignment preserves globs');
268 my $x = readline $V{V};
270 is ($e, '', '__DIE__ handler never called');
276 # GLOB assignment to tied element
277 local $SIG{__DIE__} = sub { $e = $_[0] };
278 sub Ʈ::TIEARRAY { bless [] => "Ʈ" }
279 sub Ʈ::STORE { $_[0]->[ $_[1] ] = $_[2] }
280 sub Ʈ::FETCH { $_[0]->[ $_[1] ] }
281 sub Ʈ::FETCHSIZE { @{$_[0]} }
284 is ($ary[0], '*main::DATA');
286 ref\tied(@ary)->[0], 'GLOB',
287 'tied elem assignment preserves globs'
289 is ($e, '', '__DIE__ handler not called');
290 my $x = readline $ary[0];
292 is ($e, '', '__DIE__ handler never called');
297 skip_if_miniperl('no dynamic loading on miniperl, no Encode', 2);
298 # Need some sort of die or warn to get the global destruction text if the
299 # bug is still present
300 my $prog = <<'EOPROG';
302 use open qw( :utf8 :std );
305 sub DESTROY {eval {die qq{Farewell $_[0]}}; print $@}
313 my $output = runperl(prog => $prog);
316 $output = Encode::decode("UTF-8", $output);
317 like($output, qr/^Farewell ᴹ=SCALAR/, "DESTROY was called");
318 unlike($output, qr/global destruction/,
319 "unreferenced symbol tables should be cleaned up immediately");
324 # Possibly not the correct test file for these tests.
325 # There are certain space optimisations implemented via promotion rules to
328 foreach (qw (оઓnḲ ga_ㄕƚo잎)) {
329 ok(!exists $::{$_}, "no symbols of any sort to start with for $_");
332 # A string in place of the typeglob is promoted to the function prototype
334 my $proto = eval 'prototype \&оઓnḲ';
336 is ($proto, "pìè", "String is promoted to prototype");
339 # A reference to a value is used to generate a constant subroutine
340 foreach my $value (3, "Perl rules", \42, qr/whatever/, [1,2,3], {1=>2},
341 \*STDIN, \&ok, \undef, *STDOUT) {
344 $proto = eval 'prototype \&оઓnḲ';
346 is ($proto, '', "Prototype for a constant subroutine is empty");
348 my $got = eval 'оઓnḲ';
350 is (ref $got, ref $value, "Correct type of value (" . ref($value) . ")");
351 is ($got, $value, "Value is correctly set");
356 $::{оઓnḲ} = \"Value";
358 *{"ga_ㄕƚo잎"} = \&{"оઓnḲ"};
360 is (ref $::{ga_ㄕƚo잎}, 'SCALAR', "Export of proxy constant as is");
361 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
362 is (eval 'ga_ㄕƚo잎', "Value", "Constant has correct value");
363 is (ref $::{ga_ㄕƚo잎}, 'SCALAR',
364 "Inlining of constant doesn't change representation");
368 eval 'sub ga_ㄕƚo잎 (); 1' or die $@;
369 is ($::{ga_ㄕƚo잎}, '', "Prototype is stored as an empty string");
371 # Check that a prototype expands.
372 *{"ga_ㄕƚo잎"} = \&{"оઓnḲ"};
374 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
375 is (eval 'ga_ㄕƚo잎', "Value", "Constant has correct value");
376 is (ref \$::{ga_ㄕƚo잎}, 'GLOB', "Symbol table has full typeglob");
381 # Check that assignment to an existing typeglob works
384 local $SIG{__WARN__} = sub { $w = $_[0] };
385 *{"zᐓt"} = \&{"оઓnḲ"};
386 is($w, '', "Should be no warning");
389 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
390 is (eval 'zᐓt', "Value", "Constant has correct value");
391 is (ref \$::{zᐓt}, 'GLOB', "Symbol table has full typeglob");
392 is (join ('!', @::zᐓt), 'Zᐓt!', "Existing array still in typeglob");
398 # Check that assignment to an existing subroutine works
401 local $SIG{__WARN__} = sub { $w = $_[0] };
402 *{"Ṩp맅싵Ş"} = \&{"оઓnḲ"};
403 like($w, qr/^Constant subroutine main::Ṩp맅싵Ş redefined/,
404 "Redefining a constant sub should warn");
407 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
408 is (eval 'Ṩp맅싵Ş', "Value", "Constant has correct value");
409 is (ref \$::{Ṩp맅싵Ş}, 'GLOB', "Symbol table has full typeglob");
411 # Check that assignment to an existing typeglob works
414 local $SIG{__WARN__} = sub { $w = $_[0] };
416 *{"plუᒃ"} = \&{"оઓnḲ"};
417 is($w, '', "Should be no warning");
420 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
421 is (eval 'plუᒃ', "Value", "Constant has correct value");
422 is (ref \$::{plუᒃ}, 'GLOB', "Symbol table has full typeglob");
424 my $gr = eval '\*plუᒃ' or die;
428 local $SIG{__WARN__} = sub { $w = $_[0] };
430 is($w, '', "Redefining a constant sub to another constant sub with the same underlying value should not warn (It's just re-exporting, and that was always legal)");
433 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
434 is (eval 'plუᒃ', "Value", "Constant has correct value");
435 is (ref \$::{plუᒃ}, 'GLOB', "Symbol table has full typeglob");
437 # Non-void context should defeat the optimisation, and will cause the original
438 # to be promoted (what change 26482 intended)
442 local $SIG{__WARN__} = sub { $w = $_[0] };
443 $result = *{"aẈʞƙʞƙʞƙ"} = \&{"оઓnḲ"};
444 is($w, '', "Should be no warning");
447 is (ref \$result, 'GLOB',
448 "Non void assignment should still return a typeglob");
450 is (ref \$::{оઓnḲ}, 'GLOB', "This export does affect original");
451 is (eval 'plუᒃ', "Value", "Constant has correct value");
452 is (ref \$::{plუᒃ}, 'GLOB', "Symbol table has full typeglob");
455 $::{оઓnḲ} = \"Value";
459 local $SIG{__WARN__} = sub { $w = $_[0] };
460 *{"z앞"} = \&{"оઓnḲ"};
461 is($w, '', "Should be no warning");
465 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
466 is (eval 'z앞', "Value", "Constant has correct value");
467 is (ref $::{z앞}, 'SCALAR', "Exported target is also a PCS");
470 local $SIG{__WARN__} = sub { die $_[0] };
471 *{"ビfᶠ"} = \&{"оઓnḲ"};
475 is (ref \$::{оઓnḲ}, 'GLOB', "This export does affect original");
476 is (eval 'ビfᶠ', "Value", "Constant has correct value");
477 is (ref \$::{ビfᶠ}, 'GLOB', "Symbol table has full typeglob");
480 use vars qw($gᓙʞ $sምḲ $ᕘf);
481 # Check reference assignment isn't affected by the SV type (bug #38439)
484 $ᕘf = "halt and cool down";
493 is($ᕘf, "halt and cool down");
503 foreach my $value ([1,2,3], {1=>2}, *STDOUT{IO}, \&ok, *STDOUT{FORMAT}) {
504 # *STDOUT{IO} returns a reference to a PVIO. As it's blessed, ref returns
505 # IO::Handle, which isn't what we want.
511 $proto = eval 'prototype \&оઓnḲ';
512 like ($@, qr/^Cannot convert a reference to $type to typeglob/,
513 "Cannot upgrade ref-to-$type to typeglob");
518 no warnings qw(once uninitialized);
520 my $r = eval {no strict; ${*{$g}{SCALAR}}};
521 is ($@, '', "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax");
524 $r = eval {use strict; ${*{$g}{SCALAR}}};
526 "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax under strict");
530 # Bug reported by broquaint on IRC
531 *ᔅᓗsḨ::{HASH}->{ISA}=[];
533 pass("gv_fetchmeth coped with the unexpected");
535 # An audit found these:
545 like ($@, qr/^Can't locate object method "맆"/, "Even with SUPER");
547 is(ᔅᓗsḨ->isa('swoosh'), '');
551 die if exists $::{본ㄎ};
554 eval 'is(본ㄎ(), "포ヰe",
555 "Assignment works when glob created midway (bug 45607)"); 1'
560 # [perl #72740] - indirect object syntax, heuristically imputed due to
561 # the non-existence of a function, should not cause a stash entry to be
562 # created for the non-existent function.
566 my $f = bless({}, RƬ72740b);
573 sub s2 { "RƬ72740b::s2" }
574 sub s4 { "RƬ72740b::s4" }
576 ok(exists($RƬ72740a::{s1}), "RƬ72740a::s1 exists");
577 ok(!exists($RƬ72740a::{s2}), "RƬ72740a::s2 does not exist");
578 ok(exists($RƬ72740a::{s3}), "RƬ72740a::s3 exists");
579 ok(exists($RƬ72740a::{s4}), "RƬ72740a::s4 exists");
580 is(RƬ72740a::s1(), "RƬ72740b::s2", "RƬ72740::s1 parsed correctly");
581 is(RƬ72740a::s3(), "RƬ72740b::s4", "RƬ72740::s3 parsed correctly");
584 # [perl #71686] Globs that are in symbol table can be un-globbed
587 is (eval 'local *::Ḟ앜ɞ = \"chuck"; $Ḟ앜ɞ', 'chuck',
588 "Localized glob didn't coerce into a RV");
589 is ($@, '', "Can localize FAKE glob that's present in stash");
591 is (scalar $::{Ḟ앜ɞ}, "*main::ŚyṀ",
592 "Localized FAKE glob's value was correctly restored");
595 # [perl #1804] *$x assignment when $x is a copy of another glob
596 # And [perl #77508] (same thing with list assignment)
599 my $x = *_ràndom::glob_that_is_not_used_elsewhere;
602 "$x", '*_ràndom::glob_that_is_not_used_elsewhere',
603 '[perl #1804] *$x assignment when $x is FAKE',
605 $x = *_ràndom::glob_that_is_not_used_elsewhere;
606 (my $dummy, *$x) = (undef,[]);
608 "$x", '*_ràndom::glob_that_is_not_used_elsewhere',
609 '[perl #77508] *$x list assignment when $x is FAKE',
610 ) or require Devel::Peek, Devel::Peek::Dump($x);
614 # this caused panics or 'Attempt to free unreferenced scalar'
615 # (its a compile-time issue, so the die lets us skip the prints)
618 local $SIG{__WARN__} = sub { push @warnings, @_ };
621 BEGIN { $::{FÒÒ} = \'ᴮᛅ' }
627 like($@, qr/made it/, "#76540 - no panic");
628 ok(!@warnings, "#76540 - no 'Attempt to free unreferenced scalar'");
631 # [perl #77362] various bugs related to globs as PVLVs
633 no warnings qw 'once void';
634 my %h; # We pass a key of this hash to the subroutine to get a PVLV.
636 # Set up our glob-as-PVLV
640 # Bad symbol for array
641 ok eval{ @$_; 1 }, 'PVLV glob slots can be autovivified' or diag $@;
644 # This should call TIEHANDLE, not TIESCALAR
645 *thèxt::TIEHANDLE = sub{};
646 ok eval{ tie *$_, 'thèxt'; 1 }, 'PVLV globs can be tied as handles'
649 # Assigning undef to the glob should not overwrite it...
652 local $SIG{__WARN__} = sub { $w = shift };
654 is $_, "*main::hòn", 'PVLV: assigning undef to the glob does nothing';
655 like $w, qr\Undefined value assigned to typeglob\,
656 'PVLV: assigning undef to the glob warns';
659 # Neither should reference assignment.
661 is $_, "*main::hòn", "PVLV: arrayref assignment assigns to the AV slot";
663 # Concatenation should still work.
664 ok eval { $_ .= 'thlèw' }, 'PVLV concatenation does not die' or diag $@;
665 is $_, '*main::hònthlèw', 'PVLV concatenation works';
667 # And we should be able to overwrite it with a string, number, or refer-
668 # ence, too, if we omit the *.
669 $_ = *hòn; $_ = 'tzòr';
670 is $_, 'tzòr', 'PVLV: assigning a string over a glob';
672 is $_, 23, 'PVLV: assigning an integer over a glob';
673 $_ = *hòn; $_ = 23.23;
674 is $_, 23.23, 'PVLV: assigning a float over a glob';
675 $_ = *hòn; $_ = \my $sthat;
676 is $_, \$sthat, 'PVLV: assigning a reference over a glob';
678 # This bug was found by code inspection. Could this ever happen in
680 # This duplicates a file handle, accessing it through a PVLV glob, the
681 # glob having been removed from the symbol table, so a stringified form
682 # of it does not work. This checks that sv_2io does not stringify a PVLV.
684 open *quìn, "test.pl"; # test.pl is as good a file as any
686 ok eval { open my $zow, "<&", $_ }, 'PVLV: sv_2io stringifieth not'
689 # Similar tests to make sure sv_2cv etc. do not stringify.
691 ok eval { &$_ }, "PVLV glob can be called as a sub" or diag $@;
694 is eval { &$_ }, 2, 'PVLV holding a string can be called as a sub'
697 # Coderef-to-glob assignment when the glob is no longer accessible
698 # under its name: These tests are to make sure the OPpASSIGN_CV_TO_GV
699 # optimisation takes PVLVs into account, which is why the RHSs have to be
701 use constant ghèèn => 'quàrè';
705 is eval { &$_ }, 'quàrè',
706 'PVLV: constant assignment when the glob is detached from the symtab'
710 *ghèck = sub { 'lon' };
712 is eval { &$_ }, 'lon',
713 'PVLV: coderef assignment when the glob is detached from the symtab'
717 skip_if_miniperl("no dynamic loading on miniperl, so can't load PerlIO::scalar", 1);
718 # open should accept a PVLV as its first argument
720 ok eval { open $_,'<', \my $thlext }, 'PVLV can be the first arg to open'
724 # -t should not stringify
725 $_ = *thlìt; delete $::{thlìt};
727 ok defined -t $_, 'PVLV: -t does not stringify';
730 # but some systems donâ
\80\99t support this on file handles
734 open my $quìle, "<", 'test.pl';
738 } ? $pass : $@ =~ /not implemented on filehandles/,
739 "PVLV: -T does not stringify";
740 # Unopened file handle
743 local $SIG{__WARN__} = sub { $w .= shift };
746 like $w, qr\unopened filehandle vòr\,
747 'PVLV globs get their names reported in unopened error messages';
754 pass('Can assign integers to typeglobs');
756 pass('Can assign floats to typeglobs');
758 pass('Can assign strings to typeglobs');
763 sub TIESCALAR{bless[]}
764 sub STORE{ die "No!"}
765 sub FETCH{ no warnings 'once'; *thrìt }
767 () = "$a"; # do a fetch; now $a holds a glob
768 eval { *$a = sub{} };
772 "[perl #77812] Globs in tied scalars can be reified if STORE dies"
775 # These two crashed prior to 5.13.6. In 5.13.6 they were fatal errors. They
776 # were fixed in 5.13.7.
778 my $glob = \*hèèn::ISA;
779 delete $::{"hèèn::"};
781 }, "glob-to-*ISA assignment works when *ISA has lost its stash";
783 my $glob = \*slàre::ISA;
784 delete $::{"slàre::"};
786 }, "array-to-*ISA assignment works when *ISA has lost its stash";
787 # These two crashed in 5.13.6. They were likewise fixed in 5.13.7.
790 my $glob = do { no warnings "once"; \*phìng::ᕘ};
791 delete $::{"phìng::"};
793 }, "Assigning a glob-with-sub to a glob that has lost its stash warks";
799 }, "Assigning a glob to a glob-with-sub that has lost its stash warks";
803 sub TIESCALAR{ bless \\pop }
804 sub FETCH { $${$_[0]} }
805 sub STORE { $${$_[0]} = $_[1] }
807 tie my $alias, 'Tie::Alias', my $var;
813 is $alias, 3, "[perl #77926] Glob reification during localisation";
817 # This code causes gp_free to call a destructor when a glob is being
818 # restored on scope exit. The destructor used to see SVs with a refcount of
819 # zero inside the glob, which could result in crashes (though not in this
820 # test case, which just panics).
824 *Trìt::DESTROY = sub {
825 $thwèxt = 42; # panic
829 local *thwèxt = bless [],'Trìt';
833 'no error when gp_free calls a destructor that assigns to the gv';