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/);
125 # test warnings for converting globs to other forms
127 foreach ($copy, *SKRÈÈÈ) {
129 my $victim = sprintf "%d", $_;
130 like($msg, qr/^Argument "\*main::(?:PW\\x\{d2\}MPF|SKR\\x\{c8\}\\x\{c8\}\\x\{c8\})" isn't numeric in sprintf/,
131 "Warning on conversion to IV");
135 $victim = sprintf "%u", $_;
136 like($msg, qr/^Argument "\*main::(?:PW\\x\{d2\}MPF|SKR\\x\{c8\}\\x\{c8\}\\x\{c8\})" isn't numeric in sprintf/,
137 "Warning on conversion to UV");
141 $victim = sprintf "%e", $_;
142 like($msg, qr/^Argument "\*main::(?:PW\\x\{d2\}MPF|SKR\\x\{c8\}\\x\{c8\}\\x\{c8\})" isn't numeric in sprintf/,
143 "Warning on conversion to NV");
144 like($victim, qr/^0\.0+E\+?00/i, "Expect floating point zero");
147 $victim = sprintf "%s", $_;
148 is($msg, '', "No warning on stringification");
149 is($victim, '' . $_);
153 my $test = curr_test();
154 # test *glob{THING} syntax
159 %Ẋ = ("ok $test" => "\n");
161 sub Ẋ { "ok $test\n" }
162 print ${*Ẋ{SCALAR}}, @{*Ẋ{ARRAY}}, %{*Ẋ{HASH}}, &{*Ẋ{CODE}};
163 # This needs to go here, after the print, as sub Ẋ will return the current
167 XXX This text isn't used. Should it be?
171 is (ref *Ẋ{FORMAT}, "FORMAT");
173 is (*{*Ẋ{GLOB}}, "*main::STDOUT");
176 my $test = curr_test();
178 print {*Ẋ{IO}} "ok $test\n";
182 local $SIG{__WARN__} = sub {
185 my $val = *Ẋ{FILEHANDLE};
186 print {*Ẋ{IO}} ($warn =~ /is deprecated/
187 ? "ok $test\n" : "not ok $test\n");
193 # test if defined() doesn't create any new symbols
205 *{$a} = sub { $state = "ok" };
212 # [ID 20010526.001] localized glob loses value when assigned to
214 $J=1; %J=(a=>1); @J=(1); local *J=*J; *J = sub{};
221 # does pp_readline() handle glob-ness correctly?
229 local $SIG{__WARN__} = sub { $w = $_[0] };
231 local *aʙȼ1 = sub { };
239 like ($w, qr/Prototype mismatch/);
243 # [17375] rcatline to formerly-defined undef was broken. Fixed in
244 # do_readline by checking SvOK. AMS, 20020918
252 # test the assignment of a GLOB to an LVALUE
254 local $SIG{__DIE__} = sub { $e = $_[0] };
256 sub ƒ { $_[0] = 0; $_[0] = "a"; $_[0] = *DATA }
258 is ($V{V}, '*main::DATA');
259 is (ref\$V{V}, 'GLOB', 'lvalue assignment preserves globs');
260 my $x = readline $V{V};
262 is ($e, '', '__DIE__ handler never called');
268 # GLOB assignment to tied element
269 local $SIG{__DIE__} = sub { $e = $_[0] };
270 sub Ʈ::TIEARRAY { bless [] => "Ʈ" }
271 sub Ʈ::STORE { $_[0]->[ $_[1] ] = $_[2] }
272 sub Ʈ::FETCH { $_[0]->[ $_[1] ] }
273 sub Ʈ::FETCHSIZE { @{$_[0]} }
276 is ($ary[0], '*main::DATA');
278 ref\tied(@ary)->[0], 'GLOB',
279 'tied elem assignment preserves globs'
281 is ($e, '', '__DIE__ handler not called');
282 my $x = readline $ary[0];
284 is ($e, '', '__DIE__ handler never called');
289 skip_if_miniperl('no dynamic loading on miniperl, no Encode', 2);
290 # Need some sort of die or warn to get the global destruction text if the
291 # bug is still present
292 my $prog = <<'EOPROG';
294 use open qw( :utf8 :std );
297 sub DESTROY {eval {die qq{Farewell $_[0]}}; print $@}
305 my $output = runperl(prog => $prog);
308 $output = Encode::decode("UTF-8", $output);
309 like($output, qr/^Farewell ᴹ=SCALAR/, "DESTROY was called");
310 unlike($output, qr/global destruction/,
311 "unreferenced symbol tables should be cleaned up immediately");
316 # Possibly not the correct test file for these tests.
317 # There are certain space optimisations implemented via promotion rules to
320 foreach (qw (оઓnḲ ga_ㄕƚo잎)) {
321 ok(!exists $::{$_}, "no symbols of any sort to start with for $_");
324 # A string in place of the typeglob is promoted to the function prototype
326 my $proto = eval 'prototype \&оઓnḲ';
328 is ($proto, "pìè", "String is promoted to prototype");
331 # A reference to a value is used to generate a constant subroutine
332 foreach my $value (3, "Perl rules", \42, qr/whatever/, [1,2,3], {1=>2},
333 \*STDIN, \&ok, \undef, *STDOUT) {
336 $proto = eval 'prototype \&оઓnḲ';
338 is ($proto, '', "Prototype for a constant subroutine is empty");
340 my $got = eval 'оઓnḲ';
342 is (ref $got, ref $value, "Correct type of value (" . ref($value) . ")");
343 is ($got, $value, "Value is correctly set");
348 $::{оઓnḲ} = \"Value";
350 *{"ga_ㄕƚo잎"} = \&{"оઓnḲ"};
352 is (ref $::{ga_ㄕƚo잎}, 'SCALAR', "Export of proxy constant as is");
353 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
354 is (eval 'ga_ㄕƚo잎', "Value", "Constant has correct value");
355 is (ref $::{ga_ㄕƚo잎}, 'SCALAR',
356 "Inlining of constant doesn't change representation");
360 eval 'sub ga_ㄕƚo잎 (); 1' or die $@;
361 is ($::{ga_ㄕƚo잎}, '', "Prototype is stored as an empty string");
363 # Check that a prototype expands.
364 *{"ga_ㄕƚo잎"} = \&{"оઓnḲ"};
366 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
367 is (eval 'ga_ㄕƚo잎', "Value", "Constant has correct value");
368 is (ref \$::{ga_ㄕƚo잎}, 'GLOB', "Symbol table has full typeglob");
373 # Check that assignment to an existing typeglob works
376 local $SIG{__WARN__} = sub { $w = $_[0] };
377 *{"zᐓt"} = \&{"оઓnḲ"};
378 is($w, '', "Should be no warning");
381 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
382 is (eval 'zᐓt', "Value", "Constant has correct value");
383 is (ref \$::{zᐓt}, 'GLOB', "Symbol table has full typeglob");
384 is (join ('!', @::zᐓt), 'Zᐓt!', "Existing array still in typeglob");
390 # Check that assignment to an existing subroutine works
393 local $SIG{__WARN__} = sub { $w = $_[0] };
394 *{"Ṩp맅싵Ş"} = \&{"оઓnḲ"};
395 like($w, qr/^Constant subroutine main::Ṩp맅싵Ş redefined/,
396 "Redefining a constant sub should warn");
399 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
400 is (eval 'Ṩp맅싵Ş', "Value", "Constant has correct value");
401 is (ref \$::{Ṩp맅싵Ş}, 'GLOB', "Symbol table has full typeglob");
403 # Check that assignment to an existing typeglob works
406 local $SIG{__WARN__} = sub { $w = $_[0] };
408 *{"plუᒃ"} = \&{"оઓnḲ"};
409 is($w, '', "Should be no warning");
412 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
413 is (eval 'plუᒃ', "Value", "Constant has correct value");
414 is (ref \$::{plუᒃ}, 'GLOB', "Symbol table has full typeglob");
416 my $gr = eval '\*plუᒃ' or die;
420 local $SIG{__WARN__} = sub { $w = $_[0] };
422 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)");
425 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
426 is (eval 'plუᒃ', "Value", "Constant has correct value");
427 is (ref \$::{plუᒃ}, 'GLOB', "Symbol table has full typeglob");
429 # Non-void context should defeat the optimisation, and will cause the original
430 # to be promoted (what change 26482 intended)
434 local $SIG{__WARN__} = sub { $w = $_[0] };
435 $result = *{"aẈʞƙʞƙʞƙ"} = \&{"оઓnḲ"};
436 is($w, '', "Should be no warning");
439 is (ref \$result, 'GLOB',
440 "Non void assignment should still return a typeglob");
442 is (ref \$::{оઓnḲ}, 'GLOB', "This export does affect original");
443 is (eval 'plუᒃ', "Value", "Constant has correct value");
444 is (ref \$::{plუᒃ}, 'GLOB', "Symbol table has full typeglob");
447 $::{оઓnḲ} = \"Value";
451 local $SIG{__WARN__} = sub { $w = $_[0] };
452 *{"z앞"} = \&{"оઓnḲ"};
453 is($w, '', "Should be no warning");
457 is (ref $::{оઓnḲ}, 'SCALAR', "Export doesn't affect original");
458 is (eval 'z앞', "Value", "Constant has correct value");
459 is (ref $::{z앞}, 'SCALAR', "Exported target is also a PCS");
462 local $SIG{__WARN__} = sub { die $_[0] };
463 *{"ビfᶠ"} = \&{"оઓnḲ"};
467 is (ref \$::{оઓnḲ}, 'GLOB', "This export does affect original");
468 is (eval 'ビfᶠ', "Value", "Constant has correct value");
469 is (ref \$::{ビfᶠ}, 'GLOB', "Symbol table has full typeglob");
472 use vars qw($gᓙʞ $sምḲ $ᕘf);
473 # Check reference assignment isn't affected by the SV type (bug #38439)
476 $ᕘf = "halt and cool down";
485 is($ᕘf, "halt and cool down");
495 foreach my $value ({1=>2}, *STDOUT{IO}, *STDOUT{FORMAT}) {
496 # *STDOUT{IO} returns a reference to a PVIO. As it's blessed, ref returns
497 # IO::Handle, which isn't what we want.
503 $proto = eval 'prototype \&оઓnḲ';
504 like ($@, qr/^Cannot convert a reference to $type to typeglob/,
505 "Cannot upgrade ref-to-$type to typeglob");
510 no warnings qw(once uninitialized);
512 my $r = eval {no strict; ${*{$g}{SCALAR}}};
513 is ($@, '', "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax");
516 $r = eval {use strict; ${*{$g}{SCALAR}}};
518 "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax under strict");
522 # Bug reported by broquaint on IRC
523 *ᔅᓗsḨ::{HASH}->{ISA}=[];
525 pass("gv_fetchmeth coped with the unexpected");
527 # An audit found these:
537 like ($@, qr/^Can't locate object method "맆"/, "Even with SUPER");
539 is(ᔅᓗsḨ->isa('swoosh'), '');
543 die if exists $::{본ㄎ};
546 eval 'is(본ㄎ(), "포ヰe",
547 "Assignment works when glob created midway (bug 45607)"); 1'
552 # [perl #72740] - indirect object syntax, heuristically imputed due to
553 # the non-existence of a function, should not cause a stash entry to be
554 # created for the non-existent function.
558 my $f = bless({}, RƬ72740b);
565 sub s2 { "RƬ72740b::s2" }
566 sub s4 { "RƬ72740b::s4" }
568 ok(exists($RƬ72740a::{s1}), "RƬ72740a::s1 exists");
569 ok(!exists($RƬ72740a::{s2}), "RƬ72740a::s2 does not exist");
570 ok(exists($RƬ72740a::{s3}), "RƬ72740a::s3 exists");
571 ok(exists($RƬ72740a::{s4}), "RƬ72740a::s4 exists");
572 is(RƬ72740a::s1(), "RƬ72740b::s2", "RƬ72740::s1 parsed correctly");
573 is(RƬ72740a::s3(), "RƬ72740b::s4", "RƬ72740::s3 parsed correctly");
576 # [perl #71686] Globs that are in symbol table can be un-globbed
579 is (eval 'local *::Ḟ앜ɞ = \"chuck"; $Ḟ앜ɞ', 'chuck',
580 "Localized glob didn't coerce into a RV");
581 is ($@, '', "Can localize FAKE glob that's present in stash");
583 is (scalar $::{Ḟ앜ɞ}, "*main::ŚyṀ",
584 "Localized FAKE glob's value was correctly restored");
587 # [perl #1804] *$x assignment when $x is a copy of another glob
588 # And [perl #77508] (same thing with list assignment)
591 my $x = *_ràndom::glob_that_is_not_used_elsewhere;
594 "$x", '*_ràndom::glob_that_is_not_used_elsewhere',
595 '[perl #1804] *$x assignment when $x is FAKE',
597 $x = *_ràndom::glob_that_is_not_used_elsewhere;
598 (my $dummy, *$x) = (undef,[]);
600 "$x", '*_ràndom::glob_that_is_not_used_elsewhere',
601 '[perl #77508] *$x list assignment when $x is FAKE',
602 ) or require Devel::Peek, Devel::Peek::Dump($x);
606 # this caused panics or 'Attempt to free unreferenced scalar'
607 # (its a compile-time issue, so the die lets us skip the prints)
610 local $SIG{__WARN__} = sub { push @warnings, @_ };
613 BEGIN { $::{FÒÒ} = \'ᴮᛅ' }
619 like($@, qr/made it/, "#76540 - no panic");
620 ok(!@warnings, "#76540 - no 'Attempt to free unreferenced scalar'");
623 # [perl #77362] various bugs related to globs as PVLVs
625 no warnings qw 'once void';
626 my %h; # We pass a key of this hash to the subroutine to get a PVLV.
628 # Set up our glob-as-PVLV
632 # Bad symbol for array
633 ok eval{ @$_; 1 }, 'PVLV glob slots can be autovivified' or diag $@;
636 # This should call TIEHANDLE, not TIESCALAR
637 *thèxt::TIEHANDLE = sub{};
638 ok eval{ tie *$_, 'thèxt'; 1 }, 'PVLV globs can be tied as handles'
641 # Assigning undef to the glob should not overwrite it...
644 local $SIG{__WARN__} = sub { $w = shift };
646 is $_, "*main::hòn", 'PVLV: assigning undef to the glob does nothing';
647 like $w, qr\Undefined value assigned to typeglob\,
648 'PVLV: assigning undef to the glob warns';
651 # Neither should reference assignment.
653 is $_, "*main::hòn", "PVLV: arrayref assignment assigns to the AV slot";
655 # Concatenation should still work.
656 ok eval { $_ .= 'thlèw' }, 'PVLV concatenation does not die' or diag $@;
657 is $_, '*main::hònthlèw', 'PVLV concatenation works';
659 # And we should be able to overwrite it with a string, number, or refer-
660 # ence, too, if we omit the *.
661 $_ = *hòn; $_ = 'tzòr';
662 is $_, 'tzòr', 'PVLV: assigning a string over a glob';
664 is $_, 23, 'PVLV: assigning an integer over a glob';
665 $_ = *hòn; $_ = 23.23;
666 is $_, 23.23, 'PVLV: assigning a float over a glob';
667 $_ = *hòn; $_ = \my $sthat;
668 is $_, \$sthat, 'PVLV: assigning a reference over a glob';
670 # This bug was found by code inspection. Could this ever happen in
672 # This duplicates a file handle, accessing it through a PVLV glob, the
673 # glob having been removed from the symbol table, so a stringified form
674 # of it does not work. This checks that sv_2io does not stringify a PVLV.
676 open *quìn, "test.pl"; # test.pl is as good a file as any
678 ok eval { open my $zow, "<&", $_ }, 'PVLV: sv_2io stringifieth not'
681 # Similar tests to make sure sv_2cv etc. do not stringify.
683 ok eval { &$_ }, "PVLV glob can be called as a sub" or diag $@;
686 is eval { &$_ }, 2, 'PVLV holding a string can be called as a sub'
689 # Coderef-to-glob assignment when the glob is no longer accessible
690 # under its name: These tests are to make sure the OPpASSIGN_CV_TO_GV
691 # optimisation takes PVLVs into account, which is why the RHSs have to be
693 use constant ghèèn => 'quàrè';
697 is eval { &$_ }, 'quàrè',
698 'PVLV: constant assignment when the glob is detached from the symtab'
702 *ghèck = sub { 'lon' };
704 is eval { &$_ }, 'lon',
705 'PVLV: coderef assignment when the glob is detached from the symtab'
709 skip_if_miniperl("no dynamic loading on miniperl, so can't load PerlIO::scalar", 1);
710 # open should accept a PVLV as its first argument
712 ok eval { open $_,'<', \my $thlext }, 'PVLV can be the first arg to open'
716 # -t should not stringify
717 $_ = *thlìt; delete $::{thlìt};
719 ok defined -t $_, 'PVLV: -t does not stringify';
722 # but some systems donâ
\80\99t support this on file handles
726 open my $quìle, "<", 'test.pl';
730 } ? $pass : $@ =~ /not implemented on filehandles/,
731 "PVLV: -T does not stringify";
732 # Unopened file handle
735 local $SIG{__WARN__} = sub { $w .= shift };
738 like $w, qr\unopened filehandle vòr\,
739 'PVLV globs get their names reported in unopened error messages';
746 pass('Can assign integers to typeglobs');
748 pass('Can assign floats to typeglobs');
750 pass('Can assign strings to typeglobs');
755 sub TIESCALAR{bless[]}
756 sub STORE{ die "No!"}
757 sub FETCH{ no warnings 'once'; *thrìt }
759 () = "$a"; # do a fetch; now $a holds a glob
760 eval { *$a = sub{} };
764 "[perl #77812] Globs in tied scalars can be reified if STORE dies"
767 # These two crashed prior to 5.13.6. In 5.13.6 they were fatal errors. They
768 # were fixed in 5.13.7.
770 my $glob = \*hèèn::ISA;
771 delete $::{"hèèn::"};
773 }, "glob-to-*ISA assignment works when *ISA has lost its stash";
775 my $glob = \*slàre::ISA;
776 delete $::{"slàre::"};
778 }, "array-to-*ISA assignment works when *ISA has lost its stash";
779 # These two crashed in 5.13.6. They were likewise fixed in 5.13.7.
782 my $glob = do { no warnings "once"; \*phìng::ᕘ};
783 delete $::{"phìng::"};
785 }, "Assigning a glob-with-sub to a glob that has lost its stash warks";
791 }, "Assigning a glob to a glob-with-sub that has lost its stash warks";
795 sub TIESCALAR{ bless \\pop }
796 sub FETCH { $${$_[0]} }
797 sub STORE { $${$_[0]} = $_[1] }
799 tie my $alias, 'Tie::Alias', my $var;
805 is $alias, 3, "[perl #77926] Glob reification during localisation";
809 # This code causes gp_free to call a destructor when a glob is being
810 # restored on scope exit. The destructor used to see SVs with a refcount of
811 # zero inside the glob, which could result in crashes (though not in this
812 # test case, which just panics).
816 *Trìt::DESTROY = sub {
817 $thwèxt = 42; # panic
821 local *thwèxt = bless [],'Trìt';
825 'no error when gp_free calls a destructor that assigns to the gv';