4 # various typeglob tests
17 # type coersion on assignment
21 is(ref(\$bar), 'SCALAR');
24 # type coersion (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');
35 # type coersion on substitutions that match
44 # typeglobs as lvalues
45 substr($foo, 0, 1) = "XXX";
46 is(ref(\$foo), 'SCALAR');
47 is($foo, 'XXXmain::bar');
49 # returning glob values
51 local($bar) = *main::foo;
58 is(ref(\$fuu), 'GLOB');
62 is(ref(\$baa), 'GLOB');
64 # nested package globs
65 # NOTE: It's probably OK if these semantics change, because the
66 # fact that %X::Y:: is stored in %X:: isn't documented.
69 { package Foo::Bar; no warnings 'once'; $test=1; }
70 ok(exists $Foo::{'Bar::'});
71 is($Foo::{'Bar::'}, '*Foo::Bar::');
74 # test undef operator clearing out entire glob
76 @foo = qw(more stuff);
77 %foo = qw(even more random stuff);
84 # test warnings from assignment of undef to glob
86 local $SIG{__WARN__} = sub { $msg = $_[0] };
91 like($msg, qr/Undefined value assigned to typeglob/);
94 # test warnings for converting globs to other forms
96 foreach ($copy, *SKREEE) {
98 my $victim = sprintf "%d", $_;
99 like($msg, qr/Argument "\*main::[A-Z]{6}" isn't numeric in sprintf/,
100 "Warning on conversion to IV");
104 $victim = sprintf "%u", $_;
105 like($msg, qr/Argument "\*main::[A-Z]{6}" isn't numeric in sprintf/,
106 "Warning on conversion to UV");
110 $victim = sprintf "%e", $_;
111 like($msg, qr/Argument "\*main::[A-Z]{6}" isn't numeric in sprintf/,
112 "Warning on conversion to NV");
113 like($victim, qr/^0\.0+E\+?00/i, "Expect floating point zero");
116 $victim = sprintf "%s", $_;
117 is($msg, '', "No warning on stringification");
118 is($victim, '' . $_);
122 my $test = curr_test();
123 # test *glob{THING} syntax
128 %x = ("ok $test" => "\n");
130 sub x { "ok $test\n" }
131 print ${*x{SCALAR}}, @{*x{ARRAY}}, %{*x{HASH}}, &{*x{CODE}};
132 # This needs to go here, after the print, as sub x will return the current
136 XXX This text isn't used. Should it be?
140 is (ref *x{FORMAT}, "FORMAT");
142 is (*{*x{GLOB}}, "*main::STDOUT");
145 my $test = curr_test();
147 print {*x{IO}} "ok $test\n";
151 local $SIG{__WARN__} = sub {
154 my $val = *x{FILEHANDLE};
155 print {*x{IO}} ($warn =~ /is deprecated/
156 ? "ok $test\n" : "not ok $test\n");
162 # test if defined() doesn't create any new symbols
171 no warnings 'deprecated';
183 *{$a} = sub { $state = "ok" };
191 # although it *should* if you're talking about magicals
208 "o" =~ /(((((((((((o)))))))))))/;
213 # [ID 20010526.001] localized glob loses value when assigned to
215 $j=1; %j=(a=>1); @j=(1); local *j=*j; *j = sub{};
222 # does pp_readline() handle glob-ness correctly?
230 local $SIG{__WARN__} = sub { $w = $_[0] };
232 local *abc1 = sub { };
240 like ($w, qr/Prototype mismatch/);
244 # [17375] rcatline to formerly-defined undef was broken. Fixed in
245 # do_readline by checking SvOK. AMS, 20020918
253 # test the assignment of a GLOB to an LVALUE
255 local $SIG{__DIE__} = sub { $e = $_[0] };
257 sub f { $_[0] = 0; $_[0] = "a"; $_[0] = *DATA }
259 is ($v{v}, '*main::DATA');
260 is (ref\$v{v}, 'GLOB', 'lvalue assignment preserves globs');
261 my $x = readline $v{v};
267 # GLOB assignment to tied element
268 local $SIG{__DIE__} = sub { $e = $_[0] };
269 sub T::TIEARRAY { bless [] => "T" }
270 sub T::STORE { $_[0]->[ $_[1] ] = $_[2] }
271 sub T::FETCH { $_[0]->[ $_[1] ] }
272 sub T::FETCHSIZE { @{$_[0]} }
275 is ($ary[0], '*main::DATA');
277 ref\tied(@ary)->[0], 'GLOB',
278 'tied elem assignment preserves globs'
281 my $x = readline $ary[0];
286 # Need some sort of die or warn to get the global destruction text if the
287 # bug is still present
288 my $output = runperl(prog => <<'EOPROG');
291 sub DESTROY {eval {die qq{Farewell $_[0]}}; print $@}
297 like($output, qr/^Farewell M=SCALAR/, "DESTROY was called");
298 unlike($output, qr/global destruction/,
299 "unreferenced symbol tables should be cleaned up immediately");
302 # Possibly not the correct test file for these tests.
303 # There are certain space optimisations implemented via promotion rules to
306 foreach (qw (oonk ga_shloip)) {
307 ok(!exists $::{$_}, "no symbols of any sort to start with for $_");
310 # A string in place of the typeglob is promoted to the function prototype
312 my $proto = eval 'prototype \&oonk';
314 is ($proto, "pie", "String is promoted to prototype");
317 # A reference to a value is used to generate a constant subroutine
318 foreach my $value (3, "Perl rules", \42, qr/whatever/, [1,2,3], {1=>2},
319 \*STDIN, \&ok, \undef, *STDOUT) {
322 $proto = eval 'prototype \&oonk';
324 is ($proto, '', "Prototype for a constant subroutine is empty");
326 my $got = eval 'oonk';
328 is (ref $got, ref $value, "Correct type of value (" . ref($value) . ")");
329 is ($got, $value, "Value is correctly set");
333 $::{oonk} = \"Value";
335 *{"ga_shloip"} = \&{"oonk"};
337 is (ref $::{ga_shloip}, 'SCALAR', "Export of proxy constant as is");
338 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
339 is (eval 'ga_shloip', "Value", "Constant has correct value");
340 is (ref $::{ga_shloip}, 'SCALAR',
341 "Inlining of constant doesn't change represenatation");
343 delete $::{ga_shloip};
345 eval 'sub ga_shloip (); 1' or die $@;
346 is ($::{ga_shloip}, '', "Prototype is stored as an empty string");
348 # Check that a prototype expands.
349 *{"ga_shloip"} = \&{"oonk"};
351 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
352 is (eval 'ga_shloip', "Value", "Constant has correct value");
353 is (ref \$::{ga_shloip}, 'GLOB', "Symbol table has full typeglob");
358 # Check that assignment to an existing typeglob works
361 local $SIG{__WARN__} = sub { $w = $_[0] };
362 *{"zwot"} = \&{"oonk"};
363 is($w, '', "Should be no warning");
366 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
367 is (eval 'zwot', "Value", "Constant has correct value");
368 is (ref \$::{zwot}, 'GLOB', "Symbol table has full typeglob");
369 is (join ('!', @::zwot), 'Zwot!', "Existing array still in typeglob");
375 # Check that assignment to an existing subroutine works
378 local $SIG{__WARN__} = sub { $w = $_[0] };
379 *{"spritsits"} = \&{"oonk"};
380 like($w, qr/^Constant subroutine main::spritsits redefined/,
381 "Redefining a constant sub should warn");
384 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
385 is (eval 'spritsits', "Value", "Constant has correct value");
386 is (ref \$::{spritsits}, 'GLOB', "Symbol table has full typeglob");
388 # Check that assignment to an existing typeglob works
391 local $SIG{__WARN__} = sub { $w = $_[0] };
393 *{"plunk"} = \&{"oonk"};
394 is($w, '', "Should be no warning");
397 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
398 is (eval 'plunk', "Value", "Constant has correct value");
399 is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
401 my $gr = eval '\*plunk' or die;
405 local $SIG{__WARN__} = sub { $w = $_[0] };
407 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)");
410 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
411 is (eval 'plunk', "Value", "Constant has correct value");
412 is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
414 # Non-void context should defeat the optimisation, and will cause the original
415 # to be promoted (what change 26482 intended)
419 local $SIG{__WARN__} = sub { $w = $_[0] };
420 $result = *{"awkkkkkk"} = \&{"oonk"};
421 is($w, '', "Should be no warning");
424 is (ref \$result, 'GLOB',
425 "Non void assignment should still return a typeglob");
427 is (ref \$::{oonk}, 'GLOB', "This export does affect original");
428 is (eval 'plunk', "Value", "Constant has correct value");
429 is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
432 $::{oonk} = \"Value";
436 local $SIG{__WARN__} = sub { $w = $_[0] };
437 *{"zap"} = \&{"oonk"};
438 is($w, '', "Should be no warning");
442 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
443 is (eval 'zap', "Value", "Constant has correct value");
444 is (ref $::{zap}, 'SCALAR', "Exported target is also a PCS");
447 local $SIG{__WARN__} = sub { die $_[0] };
448 *{"biff"} = \&{"oonk"};
452 is (ref \$::{oonk}, 'GLOB', "This export does affect original");
453 is (eval 'biff', "Value", "Constant has correct value");
454 is (ref \$::{biff}, 'GLOB', "Symbol table has full typeglob");
457 use vars qw($glook $smek $foof);
458 # Check reference assignment isn't affected by the SV type (bug #38439)
461 $foof = "halt and cool down";
470 is($foof, "halt and cool down");
478 foreach my $value ([1,2,3], {1=>2}, *STDOUT{IO}, \&ok, *STDOUT{FORMAT}) {
479 # *STDOUT{IO} returns a reference to a PVIO. As it's blessed, ref returns
480 # IO::Handle, which isn't what we want.
486 $proto = eval 'prototype \&oonk';
487 like ($@, qr/^Cannot convert a reference to $type to typeglob/,
488 "Cannot upgrade ref-to-$type to typeglob");
492 no warnings qw(once uninitialized);
494 my $r = eval {no strict; ${*{$g}{SCALAR}}};
495 is ($@, '', "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax");
498 $r = eval {use strict; ${*{$g}{SCALAR}}};
500 "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax under strict");
504 # Bug reported by broquaint on IRC
505 *slosh::{HASH}->{ISA}=[];
507 pass("gv_fetchmeth coped with the unexpected");
509 # An audit found these:
518 like ($@, qr/^Can't locate object method "rip"/, "Even with SUPER");
520 is(slosh->isa('swoosh'), '');
522 $CORE::GLOBAL::{"lock"}=[];
523 eval "no warnings; lock";
524 like($@, qr/^Not enough arguments for lock/,
525 "Can't trip up general keyword overloading");
527 $CORE::GLOBAL::{"readline"}=[];
528 eval "<STDOUT> if 0";
529 is($@, '', "Can't trip up readline overloading");
531 $CORE::GLOBAL::{"readpipe"}=[];
533 is($@, '', "Can't trip up readpipe overloading");
537 die if exists $::{BONK};
538 $::{BONK} = \"powie";
539 *{"BONK"} = \&{"BONK"};
540 eval 'is(BONK(), "powie",
541 "Assigment works when glob created midway (bug 45607)"); 1'
545 # For now these tests are here, but they would probably be better in a file for
546 # tests for croaks. (And in turn, that probably deserves to be in a different
547 # directory. Gerard Goossen has a point about the layout being unclear
550 no warnings 'numeric';
554 no warnings 'numeric';
561 foreach my $type (qw(integer number string)) {
562 my $prog = "coerce_$type(*STDERR)";
563 is (scalar eval "$prog; 1", undef, "$prog failed...");
564 like ($@, qr/Can't coerce GLOB to $type in/,
565 "with the correct error message");
568 # RT #60954 anonymous glob should be defined, and not coredump when
569 # stringified. The behaviours are:
571 # defined($glob) "$glob"
572 # 5.8.8 false "" with uninit warning
573 # 5.10.0 true (coredump)
577 my $io_ref = *STDOUT{IO};
579 ok(defined $glob, "RT #60954 anon glob should be defined");
582 local $SIG{__WARN__} = sub { $warn = $_[0] };
585 is($warn, '', "RT #60954 anon glob stringification shouln't warn");
586 is($str, '', "RT #60954 anon glob stringification should be empty");
589 # [perl #71254] - Assigning a glob to a variable that has a current
590 # match position. (We are testing that Perl_magic_setmglob respects globs'
591 # special used of SvSCREAM.)
593 $m = 2; $m=~s/./0/gems; $m= *STDERR;
595 "$m", "*main::STDERR",
596 '[perl #71254] assignment of globs to vars with pos'
600 # [perl #72740] - indirect object syntax, heuristically imputed due to
601 # the non-existence of a function, should not cause a stash entry to be
602 # created for the non-existent function.
605 my $f = bless({}, RT72740b);
612 sub s2 { "RT72740b::s2" }
613 sub s4 { "RT72740b::s4" }
615 ok(exists($RT72740a::{s1}), "RT72740a::s1 exists");
616 ok(!exists($RT72740a::{s2}), "RT72740a::s2 does not exist");
617 ok(exists($RT72740a::{s3}), "RT72740a::s3 exists");
618 ok(exists($RT72740a::{s4}), "RT72740a::s4 exists");
619 is(RT72740a::s1(), "RT72740b::s2", "RT72740::s1 parsed correctly");
620 is(RT72740a::s3(), "RT72740b::s4", "RT72740::s3 parsed correctly");
622 # [perl #71686] Globs that are in symbol table can be un-globbed
625 is (eval 'local *::fake = \"chuck"; $fake', 'chuck',
626 "Localized glob didn't coerce into a RV");
627 is ($@, '', "Can localize FAKE glob that's present in stash");
628 is (scalar $::{fake}, "*main::sym",
629 "Localized FAKE glob's value was correctly restored");
631 # [perl #1804] *$x assignment when $x is a copy of another glob
634 my $x = *_random::glob_that_is_not_used_elsewhere;
637 "$x", '*_random::glob_that_is_not_used_elsewhere',
638 '[perl #1804] *$x assignment when $x is FAKE',
643 # this caused panics or 'Attempt to free unreferenced scalar'
644 # (its a compile-time issue, so the die lets us skip the prints)
647 local $SIG{__WARN__} = sub { push @warnings, @_ };
650 BEGIN { $::{FOO} = \'bar' }
656 like($@, qr/made it/, "#76540 - no panic");
657 ok(!@warnings, "#76540 - no 'Attempt to free unreferenced scalar'");
660 # [perl #77362] various bugs related to globs as PVLVs
662 no warnings qw 'once void';
663 my %h; # We pass a key of this hash to the subroutine to get a PVLV.
665 # Set up our glob-as-PVLV
668 # Bad symbol for array
669 ok eval{ @$_; 1 }, 'PVLV glob slots can be autovivified' or diag $@;
671 # This should call TIEHANDLE, not TIESCALAR
672 *thext::TIEHANDLE = sub{};
673 ok eval{ tie *$_, 'thext'; 1 }, 'PVLV globs can be tied as handles'
676 # Assigning undef to the glob should not overwrite it...
679 local $SIG{__WARN__} = sub { $w = shift };
681 is $_, "*main::hon", 'PVLV: assigning undef to the glob does nothing';
682 like $w, qr\Undefined value assigned to typeglob\,
683 'PVLV: assigning undef to the glob warns';
686 # Neither should number assignment...
688 is $_, "*main::1", "PVLV: integer-to-glob assignment assigns a glob";
690 is $_, "*main::2", "PVLV: float-to-glob assignment assigns a glob";
692 # Nor reference assignment.
694 is $_, "*main::thit", "PVLV: globref-to-glob assignment assigns a glob";
696 is $_, "*main::thit", "PVLV: arrayref assignment assigns to the AV slot";
698 # Concatenation should still work.
699 ok eval { $_ .= 'thlew' }, 'PVLV concatenation does not die' or diag $@;
700 is $_, '*main::thitthlew', 'PVLV concatenation works';
702 # And we should be able to overwrite it with a string, number, or refer-
703 # ence, too, if we omit the *.
704 $_ = *hon; $_ = 'tzor';
705 is $_, 'tzor', 'PVLV: assigning a string over a glob';
707 is $_, 23, 'PVLV: assigning an integer over a glob';
708 $_ = *hon; $_ = 23.23;
709 is $_, 23.23, 'PVLV: assigning a float over a glob';
710 $_ = *hon; $_ = \my $sthat;
711 is $_, \$sthat, 'PVLV: assigning a reference over a glob';
713 # This bug was found by code inspection. Could this ever happen in
715 # This duplicates a file handle, accessing it through a PVLV glob, the
716 # glob having been removed from the symbol table, so a stringified form
717 # of it does not work. This checks that sv_2io does not stringify a PVLV.
719 open *quin, "test.pl"; # test.pl is as good a file as any
721 ok eval { open my $zow, "<&", $_ }, 'PVLV: sv_2io stringifieth not'
724 # Similar tests to make sure sv_2cv etc. do not stringify.
726 ok eval { &$_ }, "PVLV glob can be called as a sub" or diag $@;
729 is eval { &$_ }, 2, 'PVLV holding a string can be called as a sub'
732 # Coderef-to-glob assignment when the glob is no longer accessible
733 # under its name: These tests are to make sure the OPpASSIGN_CV_TO_GV
734 # optimisation takes PVLVs into account, which is why the RHSs have to be
736 use constant gheen => 'quare';
740 is eval { &$_ }, 'quare',
741 'PVLV: constant assignment when the glob is detached from the symtab'
745 *gheck = sub { 'lon' };
747 is eval { &$_ }, 'lon',
748 'PVLV: coderef assignment when the glob is detached from the symtab'
751 # open should accept a PVLV as its first argument
753 ok eval { open $_,'<', \my $thlext }, 'PVLV can be the first arg to open'
756 # -t should not stringify
757 $_ = *thlit; delete $::{thlit};
759 ok defined -t $_, 'PVLV: -t does not stringify';
762 # but some systems don’t support this on file handles
766 open my $quile, "<", 'test.pl';
770 } ? $pass : $@ =~ /not implemented on filehandles/,
771 "PVLV: -T does not stringify";
773 # Unopened file handle
776 local $SIG{__WARN__} = sub { $w .= shift };
779 like $w, qr\unopened filehandle vor\,
780 'PVLV globs get their names reported in unopened error messages';