4 # various typeglob tests
11 skip_all_without_unicode_tables();
15 use open qw( :utf8 :std );
20 # type coersion on assignment
24 is(ref(\$ᴮᛅ), 'SCALAR');
27 # type coersion (not) on misc ops
32 unlike ($ᕘ, qr/abcd/);
41 is(ref(\$ᕘ), 'GLOB', 'no type coersion when assigning to *{} retval');
45 'symbolic *{} returns symtab entry when FAKE'
49 ref(\$::{ఫケ}), 'GLOB',
50 'no type coersion when assigning to retval of symbolic *{}'
55 \$::{pɥአQuઍ}, \*pɥአQuઍ,
56 "compile-time *{} returns symtab entry when FAKE"
61 ref(\$::{pɥአQuઍ}), 'GLOB',
62 'no type coersion when assigning to retval of compile-time *{}'
66 # type coersion on substitutions that match
75 # typeglobs as lvalues
76 substr($ᕘ, 0, 1) = "XXX";
77 is(ref(\$ᕘ), 'SCALAR');
78 is($ᕘ, 'XXXmain::ᴮᛅ');
80 # returning glob values
82 local($ᴮᛅ) = *main::ᕘ;
89 is(ref(\$ፉṶ), 'GLOB');
95 # nested package globs
96 # NOTE: It's probably OK if these semantics change, because the
97 # fact that %X::Y:: is stored in %X:: isn't documented.
100 { package ฝ오::ʉ; no warnings 'once'; $test=1; }
101 ok(exists $ฝ오::{'ʉ::'});
102 is($ฝ오::{'ʉ::'}, '*ฝ오::ʉ::');
105 # test undef operator clearing out entire glob
108 %ᕘ = qw(even more random stuff);
115 # test warnings from assignment of undef to glob
117 local $SIG{__WARN__} = sub { $msg = $_[0] };
122 like($msg, qr/Undefined value assigned to typeglob/);
124 my $O_grave = utf8::unicode_to_native(0xd2);
125 my $E_grave = utf8::unicode_to_native(0xc8);
127 # It took a lot of experimentation to get the backslashes right (khw)
128 "Argument \"\\*main::(?:PW\\\\x\\{%x\\}MPF"
129 . "|SKR\\\\x\\{%x\\}\\\\x\\{%x\\}\\\\x\\{%x\\})\" "
130 . "isn't numeric in sprintf",
131 $O_grave, $E_grave, $E_grave, $E_grave);
135 # test warnings for converting globs to other forms
137 foreach ($copy, *SKRÈÈÈ) {
139 my $victim = sprintf "%d", $_;
140 like($msg, $pat, "Warning on conversion to IV");
144 $victim = sprintf "%u", $_;
145 like($msg, $pat, "Warning on conversion to UV");
149 $victim = sprintf "%e", $_;
150 like($msg, $pat, "Warning on conversion to NV");
151 like($victim, qr/^0\.0+E\+?00/i, "Expect floating point zero");
154 $victim = sprintf "%s", $_;
155 is($msg, '', "No warning on stringification");
156 is($victim, '' . $_);
160 my $test = curr_test();
161 # test *glob{THING} syntax
166 %Ẋ = ("ok $test" => "\n");
168 sub Ẋ { "ok $test\n" }
169 print ${*Ẋ{SCALAR}}, @{*Ẋ{ARRAY}}, %{*Ẋ{HASH}}, &{*Ẋ{CODE}};
170 # This needs to go here, after the print, as sub Ẋ will return the current
174 XXX This text isn't used. Should it be?
178 is (ref *Ẋ{FORMAT}, "FORMAT");
180 is (*{*Ẋ{GLOB}}, "*main::STDOUT");
183 my $test = curr_test();
185 print {*Ẋ{IO}} "ok $test\n";
189 local $SIG{__WARN__} = sub {
192 my $val = *Ẋ{FILEHANDLE};
194 # deprecation warning removed in v5.23 -- rjbs, 2015-12-31
195 # https://github.com/Perl/perl5/issues/15105
196 print {*Ẋ{IO}} (! defined $warn
197 ? "ok $test\n" : "not ok $test\n");
203 # test if defined() doesn't create any new symbols
215 *{$a} = sub { $state = "ok" };
222 # [ID 20010526.001 (#7038)] localized glob loses value when assigned to
224 $J=1; %J=(a=>1); @J=(1); local *J=*J; *J = sub{};
231 # does pp_readline() handle glob-ness correctly?
239 local $SIG{__WARN__} = sub { $w = $_[0] };
241 local *aʙȼ1 = sub { };
249 like ($w, qr/Prototype mismatch/);
253 # [17375] rcatline to formerly-defined undef was broken. Fixed in
254 # do_readline by checking SvOK. AMS, 20020918
262 # test the assignment of a GLOB to an LVALUE
264 local $SIG{__DIE__} = sub { $e = $_[0] };
266 sub ƒ { $_[0] = 0; $_[0] = "a"; $_[0] = *DATA }
268 is ($V{V}, '*main::DATA');
269 is (ref\$V{V}, 'GLOB', 'lvalue assignment preserves globs');
270 my $x = readline $V{V};
272 is ($e, '', '__DIE__ handler never called');
278 # GLOB assignment to tied element
279 local $SIG{__DIE__} = sub { $e = $_[0] };
280 sub Ʈ::TIEARRAY { bless [] => "Ʈ" }
281 sub Ʈ::STORE { $_[0]->[ $_[1] ] = $_[2] }
282 sub Ʈ::FETCH { $_[0]->[ $_[1] ] }
283 sub Ʈ::FETCHSIZE { @{$_[0]} }
286 is ($ary[0], '*main::DATA');
288 ref\tied(@ary)->[0], 'GLOB',
289 'tied elem assignment preserves globs'
291 is ($e, '', '__DIE__ handler not called');
292 my $x = readline $ary[0];
294 is ($e, '', '__DIE__ handler never called');
299 skip_if_miniperl('no dynamic loading on miniperl, no Encode', 2);
300 # Need some sort of die or warn to get the global destruction text if the
301 # bug is still present
302 my $prog = <<'EOPROG';
304 use open qw( :utf8 :std );
307 sub DESTROY {eval {die qq{Farewell $_[0]}}; print $@}
315 my $output = runperl(prog => $prog);
318 $output = Encode::decode("UTF-8", $output);
319 like($output, qr/^Farewell ᴹ=SCALAR/, "DESTROY was called");
320 unlike($output, qr/global destruction/,
321 "unreferenced symbol tables should be cleaned up immediately");
326 # Possibly not the correct test file for these tests.
327 # There are certain space optimisations implemented via promotion rules to
330 foreach (qw (оઓnḲ ga_ㄕƚo잎)) {
331 ok(!exists $::{$_}, "no symbols of any sort to start with for $_");
334 # A string in place of the typeglob is promoted to the function prototype
336 my $proto = eval 'prototype \&оઓnḲ';
338 is ($proto, "pìè", "String is promoted to prototype");
341 # A reference to a value is used to generate a constant subroutine
342 foreach my $value (3, "Perl rules", \42, qr/whatever/, [1,2,3], {1=>2},
343 \*STDIN, \&ok, \undef, *STDOUT) {
346 $proto = eval 'prototype \&оઓnḲ';
348 is ($proto, '', "Prototype for a constant subroutine is empty");
350 my $got = eval 'оઓnḲ';
352 is (ref $got, ref $value, "Correct type of value (" . ref($value) . ")");
353 is ($got, $value, "Value is correctly set");
358 $::{оઓnḲ} = \"Value";
360 *{"ga_ㄕƚo잎"} = \&{"оઓnḲ"};
362 is (ref $::{ga_ㄕƚo잎}, 'SCALAR', "Export of proxy constant as is");
363 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
364 is (eval 'ga_ㄕƚo잎', "Value", "Constant has correct value");
365 is (ref $::{ga_ㄕƚo잎}, 'SCALAR',
366 "Inlining of constant doesn't change representation");
370 eval 'sub ga_ㄕƚo잎 (); 1' or die $@;
371 is ($::{ga_ㄕƚo잎}, '', "Prototype is stored as an empty string");
373 # Check that a prototype expands.
374 *{"ga_ㄕƚo잎"} = \&{"оઓnḲ"};
376 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
377 is (eval 'ga_ㄕƚo잎', "Value", "Constant has correct value");
378 is (ref \$::{ga_ㄕƚo잎}, 'GLOB', "Symbol table has full typeglob");
383 # Check that assignment to an existing typeglob works
386 local $SIG{__WARN__} = sub { $w = $_[0] };
387 *{"zᐓt"} = \&{"оઓnḲ"};
388 is($w, '', "Should be no warning");
391 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
392 is (eval 'zᐓt', "Value", "Constant has correct value");
393 is (ref \$::{zᐓt}, 'GLOB', "Symbol table has full typeglob");
394 is (join ('!', @::zᐓt), 'Zᐓt!', "Existing array still in typeglob");
400 # Check that assignment to an existing subroutine works
403 local $SIG{__WARN__} = sub { $w = $_[0] };
404 *{"Ṩp맅싵Ş"} = \&{"оઓnḲ"};
405 like($w, qr/^Constant subroutine main::Ṩp맅싵Ş redefined/,
406 "Redefining a constant sub should warn");
409 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
410 is (eval 'Ṩp맅싵Ş', "Value", "Constant has correct value");
411 is (ref \$::{Ṩp맅싵Ş}, 'GLOB', "Symbol table has full typeglob");
413 # Check that assignment to an existing typeglob works
416 local $SIG{__WARN__} = sub { $w = $_[0] };
418 *{"plუᒃ"} = \&{"оઓnḲ"};
419 is($w, '', "Should be no warning");
422 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
423 is (eval 'plუᒃ', "Value", "Constant has correct value");
424 is (ref \$::{plუᒃ}, 'GLOB', "Symbol table has full typeglob");
426 my $gr = eval '\*plუᒃ' or die;
430 local $SIG{__WARN__} = sub { $w = $_[0] };
432 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)");
435 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
436 is (eval 'plუᒃ', "Value", "Constant has correct value");
437 is (ref \$::{plუᒃ}, 'GLOB', "Symbol table has full typeglob");
439 # Non-void context should defeat the optimisation, and will cause the original
440 # to be promoted (what change 26482 intended)
444 local $SIG{__WARN__} = sub { $w = $_[0] };
445 $result = *{"aẈʞƙʞƙʞƙ"} = \&{"оઓnḲ"};
446 is($w, '', "Should be no warning");
449 is (ref \$result, 'GLOB',
450 "Non void assignment should still return a typeglob");
452 is (ref \$::{оઓnḲ}, 'GLOB', "This export does affect original");
453 is (eval 'plუᒃ', "Value", "Constant has correct value");
454 is (ref \$::{plუᒃ}, 'GLOB', "Symbol table has full typeglob");
457 $::{оઓnḲ} = \"Value";
461 local $SIG{__WARN__} = sub { $w = $_[0] };
462 *{"z앞"} = \&{"оઓnḲ"};
463 is($w, '', "Should be no warning");
467 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
468 is (eval 'z앞', "Value", "Constant has correct value");
469 is (ref $::{z앞}, 'SCALAR', "Exported target is also a PCS");
472 local $SIG{__WARN__} = sub { die $_[0] };
473 *{"ビfᶠ"} = \&{"оઓnḲ"};
477 is (ref \$::{оઓnḲ}, 'GLOB', "This export does affect original");
478 is (eval 'ビfᶠ', "Value", "Constant has correct value");
479 is (ref \$::{ビfᶠ}, 'GLOB', "Symbol table has full typeglob");
482 use vars qw($gᓙʞ $sምḲ $ᕘf);
483 # Check reference assignment isn't affected by the SV type (bug #38439)
486 $ᕘf = "halt and cool down";
495 is($ᕘf, "halt and cool down");
505 foreach my $value ({1=>2}, *STDOUT{IO}, *STDOUT{FORMAT}) {
506 # *STDOUT{IO} returns a reference to a PVIO. As it's blessed, ref returns
507 # IO::Handle, which isn't what we want.
513 $proto = eval 'prototype \&оઓnḲ';
514 like ($@, qr/^Cannot convert a reference to $type to typeglob/,
515 "Cannot upgrade ref-to-$type to typeglob");
520 no warnings qw(once uninitialized);
522 my $r = eval {no strict; ${*{$g}{SCALAR}}};
523 is ($@, '', "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax");
526 $r = eval {use strict; ${*{$g}{SCALAR}}};
528 "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax under strict");
532 # Bug reported by broquaint on IRC
533 *ᔅᓗsḨ::{HASH}->{ISA}=[];
535 pass("gv_fetchmeth coped with the unexpected");
537 # An audit found these:
547 like ($@, qr/^Can't locate object method "맆"/, "Even with SUPER");
549 is(ᔅᓗsḨ->isa('swoosh'), '');
553 die if exists $::{본ㄎ};
556 eval 'is(본ㄎ(), "포ヰe",
557 "Assignment works when glob created midway (bug 45607)"); 1'
562 # [perl #72740] - indirect object syntax, heuristically imputed due to
563 # the non-existence of a function, should not cause a stash entry to be
564 # created for the non-existent function.
568 my $f = bless({}, RƬ72740b);
575 sub s2 { "RƬ72740b::s2" }
576 sub s4 { "RƬ72740b::s4" }
578 ok(exists($RƬ72740a::{s1}), "RƬ72740a::s1 exists");
579 ok(!exists($RƬ72740a::{s2}), "RƬ72740a::s2 does not exist");
580 ok(exists($RƬ72740a::{s3}), "RƬ72740a::s3 exists");
581 ok(exists($RƬ72740a::{s4}), "RƬ72740a::s4 exists");
582 is(RƬ72740a::s1(), "RƬ72740b::s2", "RƬ72740::s1 parsed correctly");
583 is(RƬ72740a::s3(), "RƬ72740b::s4", "RƬ72740::s3 parsed correctly");
586 # [perl #71686] Globs that are in symbol table can be un-globbed
589 is (eval 'local *::Ḟ앜ɞ = \"chuck"; $Ḟ앜ɞ', 'chuck',
590 "Localized glob didn't coerce into a RV");
591 is ($@, '', "Can localize FAKE glob that's present in stash");
593 is (scalar $::{Ḟ앜ɞ}, "*main::ŚyṀ",
594 "Localized FAKE glob's value was correctly restored");
597 # [perl #1804] *$x assignment when $x is a copy of another glob
598 # And [perl #77508] (same thing with list assignment)
601 my $x = *_ràndom::glob_that_is_not_used_elsewhere;
604 "$x", '*_ràndom::glob_that_is_not_used_elsewhere',
605 '[perl #1804] *$x assignment when $x is FAKE',
607 $x = *_ràndom::glob_that_is_not_used_elsewhere;
608 (my $dummy, *$x) = (undef,[]);
610 "$x", '*_ràndom::glob_that_is_not_used_elsewhere',
611 '[perl #77508] *$x list assignment when $x is FAKE',
612 ) or require Devel::Peek, Devel::Peek::Dump($x);
616 # this caused panics or 'Attempt to free unreferenced scalar'
617 # (its a compile-time issue, so the die lets us skip the prints)
620 local $SIG{__WARN__} = sub { push @warnings, @_ };
623 BEGIN { $::{FÒÒ} = \'ᴮᛅ' }
629 like($@, qr/made it/, "#76540 - no panic");
630 ok(!@warnings, "#76540 - no 'Attempt to free unreferenced scalar'");
633 # [perl #77362] various bugs related to globs as PVLVs
635 no warnings qw 'once void';
636 my %h; # We pass a key of this hash to the subroutine to get a PVLV.
638 # Set up our glob-as-PVLV
642 # Bad symbol for array
643 ok eval{ @$_; 1 }, 'PVLV glob slots can be autovivified' or diag $@;
646 # This should call TIEHANDLE, not TIESCALAR
647 *thèxt::TIEHANDLE = sub{};
648 ok eval{ tie *$_, 'thèxt'; 1 }, 'PVLV globs can be tied as handles'
651 # Assigning undef to the glob should not overwrite it...
654 local $SIG{__WARN__} = sub { $w = shift };
656 is $_, "*main::hòn", 'PVLV: assigning undef to the glob does nothing';
657 like $w, qr\Undefined value assigned to typeglob\,
658 'PVLV: assigning undef to the glob warns';
661 # Neither should reference assignment.
663 is $_, "*main::hòn", "PVLV: arrayref assignment assigns to the AV slot";
665 # Concatenation should still work.
666 ok eval { $_ .= 'thlèw' }, 'PVLV concatenation does not die' or diag $@;
667 is $_, '*main::hònthlèw', 'PVLV concatenation works';
669 # And we should be able to overwrite it with a string, number, or refer-
670 # ence, too, if we omit the *.
671 $_ = *hòn; $_ = 'tzòr';
672 is $_, 'tzòr', 'PVLV: assigning a string over a glob';
674 is $_, 23, 'PVLV: assigning an integer over a glob';
675 $_ = *hòn; $_ = 23.23;
676 is $_, 23.23, 'PVLV: assigning a float over a glob';
677 $_ = *hòn; $_ = \my $sthat;
678 is $_, \$sthat, 'PVLV: assigning a reference over a glob';
680 # This bug was found by code inspection. Could this ever happen in
682 # This duplicates a file handle, accessing it through a PVLV glob, the
683 # glob having been removed from the symbol table, so a stringified form
684 # of it does not work. This checks that sv_2io does not stringify a PVLV.
686 open *quìn, "test.pl"; # test.pl is as good a file as any
688 ok eval { open my $zow, "<&", $_ }, 'PVLV: sv_2io stringifieth not'
691 # Similar tests to make sure sv_2cv etc. do not stringify.
693 ok eval { &$_ }, "PVLV glob can be called as a sub" or diag $@;
696 is eval { &$_ }, 2, 'PVLV holding a string can be called as a sub'
699 # Coderef-to-glob assignment when the glob is no longer accessible
700 # under its name: These tests are to make sure the OPpASSIGN_CV_TO_GV
701 # optimisation takes PVLVs into account, which is why the RHSs have to be
703 use constant ghèèn => 'quàrè';
707 is eval { &$_ }, 'quàrè',
708 'PVLV: constant assignment when the glob is detached from the symtab'
712 *ghèck = sub { 'lon' };
714 is eval { &$_ }, 'lon',
715 'PVLV: coderef assignment when the glob is detached from the symtab'
719 skip_if_miniperl("no dynamic loading on miniperl, so can't load PerlIO::scalar", 1);
720 # open should accept a PVLV as its first argument
722 ok eval { open $_,'<', \my $thlext }, 'PVLV can be the first arg to open'
726 # -t should not stringify
727 $_ = *thlìt; delete $::{thlìt};
729 ok defined -t $_, 'PVLV: -t does not stringify';
732 # but some systems donâ
\80\99t support this on file handles
736 open my $quìle, "<", 'test.pl';
740 } ? $pass : $@ =~ /not implemented on filehandles/,
741 "PVLV: -T does not stringify";
742 # Unopened file handle
745 local $SIG{__WARN__} = sub { $w .= shift };
748 like $w, qr\unopened filehandle vòr\,
749 'PVLV globs get their names reported in unopened error messages';
756 pass('Can assign integers to typeglobs');
758 pass('Can assign floats to typeglobs');
760 pass('Can assign strings to typeglobs');
765 sub TIESCALAR{bless[]}
766 sub STORE{ die "No!"}
767 sub FETCH{ no warnings 'once'; *thrìt }
769 () = "$a"; # do a fetch; now $a holds a glob
770 eval { *$a = sub{} };
774 "[perl #77812] Globs in tied scalars can be reified if STORE dies"
777 # These two crashed prior to 5.13.6. In 5.13.6 they were fatal errors. They
778 # were fixed in 5.13.7.
780 my $glob = \*hèèn::ISA;
781 delete $::{"hèèn::"};
783 }, "glob-to-*ISA assignment works when *ISA has lost its stash";
785 my $glob = \*slàre::ISA;
786 delete $::{"slàre::"};
788 }, "array-to-*ISA assignment works when *ISA has lost its stash";
789 # These two crashed in 5.13.6. They were likewise fixed in 5.13.7.
792 my $glob = do { no warnings "once"; \*phìng::ᕘ};
793 delete $::{"phìng::"};
795 }, "Assigning a glob-with-sub to a glob that has lost its stash warks";
801 }, "Assigning a glob to a glob-with-sub that has lost its stash warks";
805 sub TIESCALAR{ bless \\pop }
806 sub FETCH { $${$_[0]} }
807 sub STORE { $${$_[0]} = $_[1] }
809 tie my $alias, 'Tie::Alias', my $var;
815 is $alias, 3, "[perl #77926] Glob reification during localisation";
819 # This code causes gp_free to call a destructor when a glob is being
820 # restored on scope exit. The destructor used to see SVs with a refcount of
821 # zero inside the glob, which could result in crashes (though not in this
822 # test case, which just panics).
826 *Trìt::DESTROY = sub {
827 $thwèxt = 42; # panic
831 local *thwèxt = bless [],'Trìt';
835 'no error when gp_free calls a destructor that assigns to the gv';