4 # various typeglob tests
17 # type coercion on assignment
21 is(ref(\$bar), 'SCALAR');
24 # type coercion (not) on misc ops
27 is(ref(\$foo), 'GLOB');
29 unlike ($foo, qr/abcd/);
30 is(ref(\$foo), 'GLOB');
32 is($foo, '*main::bar');
33 is(ref(\$foo), 'GLOB');
38 is(ref(\$foo), 'GLOB', 'no type coercion when assigning to *{} retval');
41 \$::{phake}, \*{"phake"},
42 'symbolic *{} returns symtab entry when FAKE'
44 ${\*{"phake"}} = undef;
46 ref(\$::{phake}), 'GLOB',
47 'no type coercion when assigning to retval of symbolic *{}'
52 \$::{phaque}, \*phaque,
53 "compile-time *{} returns symtab entry when FAKE"
58 ref(\$::{phaque}), 'GLOB',
59 'no type coercion when assigning to retval of compile-time *{}'
63 # type coercion on substitutions that match
72 # typeglobs as lvalues
73 substr($foo, 0, 1) = "XXX";
74 is(ref(\$foo), 'SCALAR');
75 is($foo, 'XXXmain::bar');
77 # returning glob values
79 local($bar) = *main::foo;
86 is(ref(\$fuu), 'GLOB');
90 is(ref(\$baa), 'GLOB');
92 # nested package globs
93 # NOTE: It's probably OK if these semantics change, because the
94 # fact that %X::Y:: is stored in %X:: isn't documented.
97 { package Foo::Bar; no warnings 'once'; $test=1; }
98 ok(exists $Foo::{'Bar::'});
99 is($Foo::{'Bar::'}, '*Foo::Bar::');
102 # test undef operator clearing out entire glob
104 @foo = qw(more stuff);
105 %foo = qw(even more random stuff);
112 # test warnings from assignment of undef to glob
114 local $SIG{__WARN__} = sub { $msg = $_[0] };
119 like($msg, qr/Undefined value assigned to typeglob/);
122 # test warnings for converting globs to other forms
124 foreach ($copy, *SKREEE) {
126 my $victim = sprintf "%d", $_;
127 like($msg, qr/Argument "\*main::[A-Z]{6}" isn't numeric in sprintf/,
128 "Warning on conversion to IV");
132 $victim = sprintf "%u", $_;
133 like($msg, qr/Argument "\*main::[A-Z]{6}" isn't numeric in sprintf/,
134 "Warning on conversion to UV");
138 $victim = sprintf "%e", $_;
139 like($msg, qr/Argument "\*main::[A-Z]{6}" isn't numeric in sprintf/,
140 "Warning on conversion to NV");
141 like($victim, qr/^0\.0+E\+?00/i, "Expect floating point zero");
144 $victim = sprintf "%s", $_;
145 is($msg, '', "No warning on stringification");
146 is($victim, '' . $_);
150 my $test = curr_test();
151 # test *glob{THING} syntax
156 %x = ("ok $test" => "\n");
158 sub x { "ok $test\n" }
159 print ${*x{SCALAR}}, @{*x{ARRAY}}, %{*x{HASH}}, &{*x{CODE}};
160 # This needs to go here, after the print, as sub x will return the current
164 XXX This text isn't used. Should it be?
168 is (ref *x{FORMAT}, "FORMAT");
170 is (*{*x{GLOB}}, "*main::STDOUT");
173 my $test = curr_test();
175 print {*x{IO}} "ok $test\n";
179 local $SIG{__WARN__} = sub {
182 my $val = *x{FILEHANDLE};
183 print {*x{IO}} ($warn =~ /is deprecated/
184 ? "ok $test\n" : "not ok $test\n");
190 # test if defined() doesn't create any new symbols
199 no warnings 'deprecated';
211 *{$a} = sub { $state = "ok" };
219 # although it *should* if you're talking about magicals
236 "o" =~ /(((((((((((o)))))))))))/;
241 # [ID 20010526.001] localized glob loses value when assigned to
243 $j=1; %j=(a=>1); @j=(1); local *j=*j; *j = sub{};
250 # does pp_readline() handle glob-ness correctly?
258 local $SIG{__WARN__} = sub { $w = $_[0] };
260 local *abc1 = sub { };
268 like ($w, qr/Prototype mismatch/);
272 # [17375] rcatline to formerly-defined undef was broken. Fixed in
273 # do_readline by checking SvOK. AMS, 20020918
281 # test the assignment of a GLOB to an LVALUE
283 local $SIG{__DIE__} = sub { $e = $_[0] };
285 sub f { $_[0] = 0; $_[0] = "a"; $_[0] = *DATA }
287 is ($v{v}, '*main::DATA');
288 is (ref\$v{v}, 'GLOB', 'lvalue assignment preserves globs');
289 my $x = readline $v{v};
291 is ($e, '', '__DIE__ handler never called');
296 # GLOB assignment to tied element
297 local $SIG{__DIE__} = sub { $e = $_[0] };
298 sub T::TIEARRAY { bless [] => "T" }
299 sub T::STORE { $_[0]->[ $_[1] ] = $_[2] }
300 sub T::FETCH { $_[0]->[ $_[1] ] }
301 sub T::FETCHSIZE { @{$_[0]} }
304 is ($ary[0], '*main::DATA');
306 ref\tied(@ary)->[0], 'GLOB',
307 'tied elem assignment preserves globs'
309 is ($e, '', '__DIE__ handler not called');
310 my $x = readline $ary[0];
312 is ($e, '', '__DIE__ handler never called');
316 # Need some sort of die or warn to get the global destruction text if the
317 # bug is still present
318 my $output = runperl(prog => <<'EOPROG');
321 sub DESTROY {eval {die qq{Farewell $_[0]}}; print $@}
327 like($output, qr/^Farewell M=SCALAR/, "DESTROY was called");
328 unlike($output, qr/global destruction/,
329 "unreferenced symbol tables should be cleaned up immediately");
332 # Possibly not the correct test file for these tests.
333 # There are certain space optimisations implemented via promotion rules to
336 foreach (qw (oonk ga_shloip)) {
337 ok(!exists $::{$_}, "no symbols of any sort to start with for $_");
340 # A string in place of the typeglob is promoted to the function prototype
342 my $proto = eval 'prototype \&oonk';
344 is ($proto, "pie", "String is promoted to prototype");
347 # A reference to a value is used to generate a constant subroutine
348 foreach my $value (3, "Perl rules", \42, qr/whatever/, [1,2,3], {1=>2},
349 \*STDIN, \&ok, \undef, *STDOUT) {
352 $proto = eval 'prototype \&oonk';
354 is ($proto, '', "Prototype for a constant subroutine is empty");
356 my $got = eval 'oonk';
358 is (ref $got, ref $value, "Correct type of value (" . ref($value) . ")");
359 is ($got, $value, "Value is correctly set");
363 $::{oonk} = \"Value";
365 *{"ga_shloip"} = \&{"oonk"};
367 is (ref $::{ga_shloip}, 'SCALAR', "Export of proxy constant as is");
368 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
369 is (eval 'ga_shloip', "Value", "Constant has correct value");
370 is (ref $::{ga_shloip}, 'SCALAR',
371 "Inlining of constant doesn't change representation");
373 delete $::{ga_shloip};
375 eval 'sub ga_shloip (); 1' or die $@;
376 is ($::{ga_shloip}, '', "Prototype is stored as an empty string");
378 # Check that a prototype expands.
379 *{"ga_shloip"} = \&{"oonk"};
381 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
382 is (eval 'ga_shloip', "Value", "Constant has correct value");
383 is (ref \$::{ga_shloip}, 'GLOB', "Symbol table has full typeglob");
388 # Check that assignment to an existing typeglob works
391 local $SIG{__WARN__} = sub { $w = $_[0] };
392 *{"zwot"} = \&{"oonk"};
393 is($w, '', "Should be no warning");
396 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
397 is (eval 'zwot', "Value", "Constant has correct value");
398 is (ref \$::{zwot}, 'GLOB', "Symbol table has full typeglob");
399 is (join ('!', @::zwot), 'Zwot!', "Existing array still in typeglob");
405 # Check that assignment to an existing subroutine works
408 local $SIG{__WARN__} = sub { $w = $_[0] };
409 *{"spritsits"} = \&{"oonk"};
410 like($w, qr/^Constant subroutine main::spritsits redefined/,
411 "Redefining a constant sub should warn");
414 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
415 is (eval 'spritsits', "Value", "Constant has correct value");
416 is (ref \$::{spritsits}, 'GLOB', "Symbol table has full typeglob");
418 # Check that assignment to an existing typeglob works
421 local $SIG{__WARN__} = sub { $w = $_[0] };
423 *{"plunk"} = \&{"oonk"};
424 is($w, '', "Should be no warning");
427 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
428 is (eval 'plunk', "Value", "Constant has correct value");
429 is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
431 my $gr = eval '\*plunk' or die;
435 local $SIG{__WARN__} = sub { $w = $_[0] };
437 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)");
440 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
441 is (eval 'plunk', "Value", "Constant has correct value");
442 is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
444 # Non-void context should defeat the optimisation, and will cause the original
445 # to be promoted (what change 26482 intended)
449 local $SIG{__WARN__} = sub { $w = $_[0] };
450 $result = *{"awkkkkkk"} = \&{"oonk"};
451 is($w, '', "Should be no warning");
454 is (ref \$result, 'GLOB',
455 "Non void assignment should still return a typeglob");
457 is (ref \$::{oonk}, 'GLOB', "This export does affect original");
458 is (eval 'plunk', "Value", "Constant has correct value");
459 is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
462 $::{oonk} = \"Value";
466 local $SIG{__WARN__} = sub { $w = $_[0] };
467 *{"zap"} = \&{"oonk"};
468 is($w, '', "Should be no warning");
472 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
473 is (eval 'zap', "Value", "Constant has correct value");
474 is (ref $::{zap}, 'SCALAR', "Exported target is also a PCS");
477 local $SIG{__WARN__} = sub { die $_[0] };
478 *{"biff"} = \&{"oonk"};
482 is (ref \$::{oonk}, 'GLOB', "This export does affect original");
483 is (eval 'biff', "Value", "Constant has correct value");
484 is (ref \$::{biff}, 'GLOB', "Symbol table has full typeglob");
487 use vars qw($glook $smek $foof);
488 # Check reference assignment isn't affected by the SV type (bug #38439)
491 $foof = "halt and cool down";
500 is($foof, "halt and cool down");
508 foreach my $value ([1,2,3], {1=>2}, *STDOUT{IO}, \&ok, *STDOUT{FORMAT}) {
509 # *STDOUT{IO} returns a reference to a PVIO. As it's blessed, ref returns
510 # IO::Handle, which isn't what we want.
516 $proto = eval 'prototype \&oonk';
517 like ($@, qr/^Cannot convert a reference to $type to typeglob/,
518 "Cannot upgrade ref-to-$type to typeglob");
522 no warnings qw(once uninitialized);
524 my $r = eval {no strict; ${*{$g}{SCALAR}}};
525 is ($@, '', "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax");
528 $r = eval {use strict; ${*{$g}{SCALAR}}};
530 "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax under strict");
534 # Bug reported by broquaint on IRC
535 *slosh::{HASH}->{ISA}=[];
537 pass("gv_fetchmeth coped with the unexpected");
539 # An audit found these:
548 like ($@, qr/^Can't locate object method "rip"/, "Even with SUPER");
550 is(slosh->isa('swoosh'), '');
552 $CORE::GLOBAL::{"lock"}=[];
553 eval "no warnings; lock";
554 like($@, qr/^Not enough arguments for lock/,
555 "Can't trip up general keyword overloading");
557 $CORE::GLOBAL::{"readline"}=[];
558 eval "<STDOUT> if 0";
559 is($@, '', "Can't trip up readline overloading");
561 $CORE::GLOBAL::{"readpipe"}=[];
563 is($@, '', "Can't trip up readpipe overloading");
567 die if exists $::{BONK};
568 $::{BONK} = \"powie";
569 *{"BONK"} = \&{"BONK"};
570 eval 'is(BONK(), "powie",
571 "Assignment works when glob created midway (bug 45607)"); 1'
575 # For now these tests are here, but they would probably be better in a file for
576 # tests for croaks. (And in turn, that probably deserves to be in a different
577 # directory. Gerard Goossen has a point about the layout being unclear
580 no warnings 'numeric';
584 no warnings 'numeric';
591 foreach my $type (qw(integer number string)) {
592 my $prog = "coerce_$type(*STDERR)";
593 is (scalar eval "$prog; 1", undef, "$prog failed...");
594 like ($@, qr/Can't coerce GLOB to $type in/,
595 "with the correct error message");
598 # RT #60954 anonymous glob should be defined, and not coredump when
599 # stringified. The behaviours are:
601 # defined($glob) "$glob" $glob .= ...
602 # 5.8.8 false "" with uninit warning "" with uninit warning
603 # 5.10.0 true (coredump) (coredump)
604 # 5.1[24] true "" "" with uninit warning
605 # 5.16 true "*__ANON__::..." "*__ANON__::..."
608 my $io_ref = *STDOUT{IO};
610 ok(defined $glob, "RT #60954 anon glob should be defined");
613 local $SIG{__WARN__} = sub { $warn = $_[0] };
616 is($warn, '', "RT #60954 anon glob stringification shouldn't warn");
617 is($str, '*__ANON__::$__ANONIO__',
618 "RT #60954/#96326 anon glob stringification");
621 # [perl #71254] - Assigning a glob to a variable that has a current
622 # match position. (We are testing that Perl_magic_setmglob respects globs'
623 # special used of SvSCREAM.)
625 $m = 2; $m=~s/./0/gems; $m= *STDERR;
627 "$m", "*main::STDERR",
628 '[perl #71254] assignment of globs to vars with pos'
632 # [perl #72740] - indirect object syntax, heuristically imputed due to
633 # the non-existence of a function, should not cause a stash entry to be
634 # created for the non-existent function.
637 my $f = bless({}, RT72740b);
644 sub s2 { "RT72740b::s2" }
645 sub s4 { "RT72740b::s4" }
647 ok(exists($RT72740a::{s1}), "RT72740a::s1 exists");
648 ok(!exists($RT72740a::{s2}), "RT72740a::s2 does not exist");
649 ok(exists($RT72740a::{s3}), "RT72740a::s3 exists");
650 ok(exists($RT72740a::{s4}), "RT72740a::s4 exists");
651 is(RT72740a::s1(), "RT72740b::s2", "RT72740::s1 parsed correctly");
652 is(RT72740a::s3(), "RT72740b::s4", "RT72740::s3 parsed correctly");
654 # [perl #71686] Globs that are in symbol table can be un-globbed
657 is (eval 'local *::fake = \"chuck"; $fake', 'chuck',
658 "Localized glob didn't coerce into a RV");
659 is ($@, '', "Can localize FAKE glob that's present in stash");
660 is (scalar $::{fake}, "*main::sym",
661 "Localized FAKE glob's value was correctly restored");
663 # [perl #1804] *$x assignment when $x is a copy of another glob
664 # And [perl #77508] (same thing with list assignment)
667 my $x = *_random::glob_that_is_not_used_elsewhere;
670 "$x", '*_random::glob_that_is_not_used_elsewhere',
671 '[perl #1804] *$x assignment when $x is FAKE',
673 $x = *_random::glob_that_is_not_used_elsewhere;
674 (my $dummy, *$x) = (undef,[]);
676 "$x", '*_random::glob_that_is_not_used_elsewhere',
677 '[perl #77508] *$x list assignment when $x is FAKE',
678 ) or require Devel::Peek, Devel::Peek::Dump($x);
682 # this caused panics or 'Attempt to free unreferenced scalar'
683 # (its a compile-time issue, so the die lets us skip the prints)
686 local $SIG{__WARN__} = sub { push @warnings, @_ };
689 BEGIN { $::{FOO} = \'bar' }
695 like($@, qr/made it/, "#76540 - no panic");
696 ok(!@warnings, "#76540 - no 'Attempt to free unreferenced scalar'");
699 # [perl #77362] various bugs related to globs as PVLVs
701 no warnings qw 'once void';
702 my %h; # We pass a key of this hash to the subroutine to get a PVLV.
704 # Set up our glob-as-PVLV
707 # Bad symbol for array
708 ok eval{ @$_; 1 }, 'PVLV glob slots can be autovivified' or diag $@;
710 # This should call TIEHANDLE, not TIESCALAR
711 *thext::TIEHANDLE = sub{};
712 ok eval{ tie *$_, 'thext'; 1 }, 'PVLV globs can be tied as handles'
715 # Assigning undef to the glob should not overwrite it...
718 local $SIG{__WARN__} = sub { $w = shift };
720 is $_, "*main::hon", 'PVLV: assigning undef to the glob does nothing';
721 like $w, qr\Undefined value assigned to typeglob\,
722 'PVLV: assigning undef to the glob warns';
725 # Neither should reference assignment.
727 is $_, "*main::hon", "PVLV: arrayref assignment assigns to the AV slot";
729 # Concatenation should still work.
730 ok eval { $_ .= 'thlew' }, 'PVLV concatenation does not die' or diag $@;
731 is $_, '*main::honthlew', 'PVLV concatenation works';
733 # And we should be able to overwrite it with a string, number, or refer-
734 # ence, too, if we omit the *.
735 $_ = *hon; $_ = 'tzor';
736 is $_, 'tzor', 'PVLV: assigning a string over a glob';
738 is $_, 23, 'PVLV: assigning an integer over a glob';
739 $_ = *hon; $_ = 23.23;
740 is $_, 23.23, 'PVLV: assigning a float over a glob';
741 $_ = *hon; $_ = \my $sthat;
742 is $_, \$sthat, 'PVLV: assigning a reference over a glob';
744 # This bug was found by code inspection. Could this ever happen in
746 # This duplicates a file handle, accessing it through a PVLV glob, the
747 # glob having been removed from the symbol table, so a stringified form
748 # of it does not work. This checks that sv_2io does not stringify a PVLV.
750 open *quin, "test.pl"; # test.pl is as good a file as any
752 ok eval { open my $zow, "<&", $_ }, 'PVLV: sv_2io stringifieth not'
755 # Similar tests to make sure sv_2cv etc. do not stringify.
757 ok eval { &$_ }, "PVLV glob can be called as a sub" or diag $@;
760 is eval { &$_ }, 2, 'PVLV holding a string can be called as a sub'
763 # Coderef-to-glob assignment when the glob is no longer accessible
764 # under its name: These tests are to make sure the OPpASSIGN_CV_TO_GV
765 # optimisation takes PVLVs into account, which is why the RHSs have to be
767 use constant gheen => 'quare';
771 is eval { &$_ }, 'quare',
772 'PVLV: constant assignment when the glob is detached from the symtab'
776 *gheck = sub { 'lon' };
778 is eval { &$_ }, 'lon',
779 'PVLV: coderef assignment when the glob is detached from the symtab'
783 skip_if_miniperl("no dynamic loading on miniperl, so can't load PerlIO::scalar", 1);
784 # open should accept a PVLV as its first argument
786 ok eval { open $_,'<', \my $thlext }, 'PVLV can be the first arg to open'
790 # -t should not stringify
791 $_ = *thlit; delete $::{thlit};
793 ok defined -t $_, 'PVLV: -t does not stringify';
796 # but some systems don’t support this on file handles
800 open my $quile, "<", 'test.pl';
804 } ? $pass : $@ =~ /not implemented on filehandles/,
805 "PVLV: -T does not stringify";
807 # Unopened file handle
810 local $SIG{__WARN__} = sub { $w .= shift };
813 like $w, qr\unopened filehandle vor\,
814 'PVLV globs get their names reported in unopened error messages';
821 pass('Can assign integers to typeglobs');
823 pass('Can assign floats to typeglobs');
825 pass('Can assign strings to typeglobs');
829 sub TIESCALAR{bless[]}
830 sub STORE{ die "No!"}
831 sub FETCH{ no warnings 'once'; *thrit }
833 () = "$a"; # do a fetch; now $a holds a glob
834 eval { *$a = sub{} };
838 "[perl #77812] Globs in tied scalars can be reified if STORE dies"
841 # These two crashed prior to 5.13.6. In 5.13.6 they were fatal errors. They
842 # were fixed in 5.13.7.
844 my $glob = \*heen::ISA;
845 delete $::{"heen::"};
847 }, "glob-to-*ISA assignment works when *ISA has lost its stash";
849 my $glob = \*slare::ISA;
850 delete $::{"slare::"};
852 }, "array-to-*ISA assignment works when *ISA has lost its stash";
853 # These two crashed in 5.13.6. They were likewise fixed in 5.13.7.
856 my $glob = do { no warnings "once"; \*phing::foo};
857 delete $::{"phing::"};
859 }, "Assigning a glob-with-sub to a glob that has lost its stash warks";
862 my $glob = \*pon::foo;
865 }, "Assigning a glob to a glob-with-sub that has lost its stash warks";
869 sub TIESCALAR{ bless \\pop }
870 sub FETCH { $${$_[0]} }
871 sub STORE { $${$_[0]} = $_[1] }
873 tie my $alias, 'Tie::Alias', my $var;
879 is $alias, 3, "[perl #77926] Glob reification during localisation";
883 # This code causes gp_free to call a destructor when a glob is being
884 # restored on scope exit. The destructor used to see SVs with a refcount of
885 # zero inside the glob, which could result in crashes (though not in this
886 # test case, which just panics).
890 *Trit::DESTROY = sub {
891 $thwext = 42; # panic
896 $thwext = bless[],'Trit';
900 'no error when gp_free calls a destructor that assigns to the gv';
904 eval { *{my $undef} = 3 };
905 like $@, qr/^Can't use an undefined value as a symbol reference at /,
906 '*{ $undef } assignment';
907 eval { *{;undef} = 3 };
908 like $@, qr/^Can't use an undefined value as a symbol reference at /,
909 '*{ ;undef } assignment';
911 # [perl #99142] defined &{"foo"} when there is a constant stub
912 # If I break your module, you get to have it mentioned in Perl's tests. :-)
913 package HTTP::MobileAttribute::Plugin::Locator {
914 use constant LOCATOR_GPS => 1;
915 ::ok defined &{__PACKAGE__."::LOCATOR_GPS"},
916 'defined &{"name of constant"}';
917 ::ok Internals::SvREFCNT(${__PACKAGE__."::"}{LOCATOR_GPS}),
918 "stash elem for slot is not freed prematurely";