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');
38 is(ref(\$foo), 'GLOB', 'no type coersion when assigning to *{} retval');
41 \$::{phake}, \*{"phake"},
42 'symbolic *{} returns symtab entry when FAKE'
44 ${\*{"phake"}} = undef;
46 ref(\$::{phake}), 'GLOB',
47 'no type coersion when assigning to retval of symbolic *{}'
52 \$::{phaque}, \*phaque,
53 "compile-time *{} returns symtab entry when FAKE"
58 ref(\$::{phaque}), 'GLOB',
59 'no type coersion when assigning to retval of compile-time *{}'
63 # type coersion 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};
295 # GLOB assignment to tied element
296 local $SIG{__DIE__} = sub { $e = $_[0] };
297 sub T::TIEARRAY { bless [] => "T" }
298 sub T::STORE { $_[0]->[ $_[1] ] = $_[2] }
299 sub T::FETCH { $_[0]->[ $_[1] ] }
300 sub T::FETCHSIZE { @{$_[0]} }
303 is ($ary[0], '*main::DATA');
305 ref\tied(@ary)->[0], 'GLOB',
306 'tied elem assignment preserves globs'
309 my $x = readline $ary[0];
314 # Need some sort of die or warn to get the global destruction text if the
315 # bug is still present
316 my $output = runperl(prog => <<'EOPROG');
319 sub DESTROY {eval {die qq{Farewell $_[0]}}; print $@}
325 like($output, qr/^Farewell M=SCALAR/, "DESTROY was called");
326 unlike($output, qr/global destruction/,
327 "unreferenced symbol tables should be cleaned up immediately");
330 # Possibly not the correct test file for these tests.
331 # There are certain space optimisations implemented via promotion rules to
334 foreach (qw (oonk ga_shloip)) {
335 ok(!exists $::{$_}, "no symbols of any sort to start with for $_");
338 # A string in place of the typeglob is promoted to the function prototype
340 my $proto = eval 'prototype \&oonk';
342 is ($proto, "pie", "String is promoted to prototype");
345 # A reference to a value is used to generate a constant subroutine
346 foreach my $value (3, "Perl rules", \42, qr/whatever/, [1,2,3], {1=>2},
347 \*STDIN, \&ok, \undef, *STDOUT) {
350 $proto = eval 'prototype \&oonk';
352 is ($proto, '', "Prototype for a constant subroutine is empty");
354 my $got = eval 'oonk';
356 is (ref $got, ref $value, "Correct type of value (" . ref($value) . ")");
357 is ($got, $value, "Value is correctly set");
361 $::{oonk} = \"Value";
363 *{"ga_shloip"} = \&{"oonk"};
365 is (ref $::{ga_shloip}, 'SCALAR', "Export of proxy constant as is");
366 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
367 is (eval 'ga_shloip', "Value", "Constant has correct value");
368 is (ref $::{ga_shloip}, 'SCALAR',
369 "Inlining of constant doesn't change represenatation");
371 delete $::{ga_shloip};
373 eval 'sub ga_shloip (); 1' or die $@;
374 is ($::{ga_shloip}, '', "Prototype is stored as an empty string");
376 # Check that a prototype expands.
377 *{"ga_shloip"} = \&{"oonk"};
379 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
380 is (eval 'ga_shloip', "Value", "Constant has correct value");
381 is (ref \$::{ga_shloip}, 'GLOB', "Symbol table has full typeglob");
386 # Check that assignment to an existing typeglob works
389 local $SIG{__WARN__} = sub { $w = $_[0] };
390 *{"zwot"} = \&{"oonk"};
391 is($w, '', "Should be no warning");
394 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
395 is (eval 'zwot', "Value", "Constant has correct value");
396 is (ref \$::{zwot}, 'GLOB', "Symbol table has full typeglob");
397 is (join ('!', @::zwot), 'Zwot!', "Existing array still in typeglob");
403 # Check that assignment to an existing subroutine works
406 local $SIG{__WARN__} = sub { $w = $_[0] };
407 *{"spritsits"} = \&{"oonk"};
408 like($w, qr/^Constant subroutine main::spritsits redefined/,
409 "Redefining a constant sub should warn");
412 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
413 is (eval 'spritsits', "Value", "Constant has correct value");
414 is (ref \$::{spritsits}, 'GLOB', "Symbol table has full typeglob");
416 # Check that assignment to an existing typeglob works
419 local $SIG{__WARN__} = sub { $w = $_[0] };
421 *{"plunk"} = \&{"oonk"};
422 is($w, '', "Should be no warning");
425 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
426 is (eval 'plunk', "Value", "Constant has correct value");
427 is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
429 my $gr = eval '\*plunk' or die;
433 local $SIG{__WARN__} = sub { $w = $_[0] };
435 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)");
438 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
439 is (eval 'plunk', "Value", "Constant has correct value");
440 is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
442 # Non-void context should defeat the optimisation, and will cause the original
443 # to be promoted (what change 26482 intended)
447 local $SIG{__WARN__} = sub { $w = $_[0] };
448 $result = *{"awkkkkkk"} = \&{"oonk"};
449 is($w, '', "Should be no warning");
452 is (ref \$result, 'GLOB',
453 "Non void assignment should still return a typeglob");
455 is (ref \$::{oonk}, 'GLOB', "This export does affect original");
456 is (eval 'plunk', "Value", "Constant has correct value");
457 is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
460 $::{oonk} = \"Value";
464 local $SIG{__WARN__} = sub { $w = $_[0] };
465 *{"zap"} = \&{"oonk"};
466 is($w, '', "Should be no warning");
470 is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
471 is (eval 'zap', "Value", "Constant has correct value");
472 is (ref $::{zap}, 'SCALAR', "Exported target is also a PCS");
475 local $SIG{__WARN__} = sub { die $_[0] };
476 *{"biff"} = \&{"oonk"};
480 is (ref \$::{oonk}, 'GLOB', "This export does affect original");
481 is (eval 'biff', "Value", "Constant has correct value");
482 is (ref \$::{biff}, 'GLOB', "Symbol table has full typeglob");
485 use vars qw($glook $smek $foof);
486 # Check reference assignment isn't affected by the SV type (bug #38439)
489 $foof = "halt and cool down";
498 is($foof, "halt and cool down");
506 foreach my $value ([1,2,3], {1=>2}, *STDOUT{IO}, \&ok, *STDOUT{FORMAT}) {
507 # *STDOUT{IO} returns a reference to a PVIO. As it's blessed, ref returns
508 # IO::Handle, which isn't what we want.
514 $proto = eval 'prototype \&oonk';
515 like ($@, qr/^Cannot convert a reference to $type to typeglob/,
516 "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 *slosh::{HASH}->{ISA}=[];
535 pass("gv_fetchmeth coped with the unexpected");
537 # An audit found these:
546 like ($@, qr/^Can't locate object method "rip"/, "Even with SUPER");
548 is(slosh->isa('swoosh'), '');
550 $CORE::GLOBAL::{"lock"}=[];
551 eval "no warnings; lock";
552 like($@, qr/^Not enough arguments for lock/,
553 "Can't trip up general keyword overloading");
555 $CORE::GLOBAL::{"readline"}=[];
556 eval "<STDOUT> if 0";
557 is($@, '', "Can't trip up readline overloading");
559 $CORE::GLOBAL::{"readpipe"}=[];
561 is($@, '', "Can't trip up readpipe overloading");
565 die if exists $::{BONK};
566 $::{BONK} = \"powie";
567 *{"BONK"} = \&{"BONK"};
568 eval 'is(BONK(), "powie",
569 "Assigment works when glob created midway (bug 45607)"); 1'
573 # For now these tests are here, but they would probably be better in a file for
574 # tests for croaks. (And in turn, that probably deserves to be in a different
575 # directory. Gerard Goossen has a point about the layout being unclear
578 no warnings 'numeric';
582 no warnings 'numeric';
589 foreach my $type (qw(integer number string)) {
590 my $prog = "coerce_$type(*STDERR)";
591 is (scalar eval "$prog; 1", undef, "$prog failed...");
592 like ($@, qr/Can't coerce GLOB to $type in/,
593 "with the correct error message");
596 # RT #60954 anonymous glob should be defined, and not coredump when
597 # stringified. The behaviours are:
599 # defined($glob) "$glob"
600 # 5.8.8 false "" with uninit warning
601 # 5.10.0 true (coredump)
605 my $io_ref = *STDOUT{IO};
607 ok(defined $glob, "RT #60954 anon glob should be defined");
610 local $SIG{__WARN__} = sub { $warn = $_[0] };
613 is($warn, '', "RT #60954 anon glob stringification shouln't warn");
614 is($str, '', "RT #60954 anon glob stringification should be empty");
617 # [perl #71254] - Assigning a glob to a variable that has a current
618 # match position. (We are testing that Perl_magic_setmglob respects globs'
619 # special used of SvSCREAM.)
621 $m = 2; $m=~s/./0/gems; $m= *STDERR;
623 "$m", "*main::STDERR",
624 '[perl #71254] assignment of globs to vars with pos'
628 # [perl #72740] - indirect object syntax, heuristically imputed due to
629 # the non-existence of a function, should not cause a stash entry to be
630 # created for the non-existent function.
633 my $f = bless({}, RT72740b);
640 sub s2 { "RT72740b::s2" }
641 sub s4 { "RT72740b::s4" }
643 ok(exists($RT72740a::{s1}), "RT72740a::s1 exists");
644 ok(!exists($RT72740a::{s2}), "RT72740a::s2 does not exist");
645 ok(exists($RT72740a::{s3}), "RT72740a::s3 exists");
646 ok(exists($RT72740a::{s4}), "RT72740a::s4 exists");
647 is(RT72740a::s1(), "RT72740b::s2", "RT72740::s1 parsed correctly");
648 is(RT72740a::s3(), "RT72740b::s4", "RT72740::s3 parsed correctly");
650 # [perl #71686] Globs that are in symbol table can be un-globbed
653 is (eval 'local *::fake = \"chuck"; $fake', 'chuck',
654 "Localized glob didn't coerce into a RV");
655 is ($@, '', "Can localize FAKE glob that's present in stash");
656 is (scalar $::{fake}, "*main::sym",
657 "Localized FAKE glob's value was correctly restored");
659 # [perl #1804] *$x assignment when $x is a copy of another glob
660 # And [perl #77508] (same thing with list assignment)
663 my $x = *_random::glob_that_is_not_used_elsewhere;
666 "$x", '*_random::glob_that_is_not_used_elsewhere',
667 '[perl #1804] *$x assignment when $x is FAKE',
669 $x = *_random::glob_that_is_not_used_elsewhere;
670 (my $dummy, *$x) = (undef,[]);
672 "$x", '*_random::glob_that_is_not_used_elsewhere',
673 '[perl #77508] *$x list assignment when $x is FAKE',
674 ) or require Devel::Peek, Devel::Peek::Dump($x);
678 # this caused panics or 'Attempt to free unreferenced scalar'
679 # (its a compile-time issue, so the die lets us skip the prints)
682 local $SIG{__WARN__} = sub { push @warnings, @_ };
685 BEGIN { $::{FOO} = \'bar' }
691 like($@, qr/made it/, "#76540 - no panic");
692 ok(!@warnings, "#76540 - no 'Attempt to free unreferenced scalar'");
695 # [perl #77362] various bugs related to globs as PVLVs
697 no warnings qw 'once void';
698 my %h; # We pass a key of this hash to the subroutine to get a PVLV.
700 # Set up our glob-as-PVLV
703 # Bad symbol for array
704 ok eval{ @$_; 1 }, 'PVLV glob slots can be autovivified' or diag $@;
706 # This should call TIEHANDLE, not TIESCALAR
707 *thext::TIEHANDLE = sub{};
708 ok eval{ tie *$_, 'thext'; 1 }, 'PVLV globs can be tied as handles'
711 # Assigning undef to the glob should not overwrite it...
714 local $SIG{__WARN__} = sub { $w = shift };
716 is $_, "*main::hon", 'PVLV: assigning undef to the glob does nothing';
717 like $w, qr\Undefined value assigned to typeglob\,
718 'PVLV: assigning undef to the glob warns';
721 # Neither should reference assignment.
723 is $_, "*main::hon", "PVLV: arrayref assignment assigns to the AV slot";
725 # Concatenation should still work.
726 ok eval { $_ .= 'thlew' }, 'PVLV concatenation does not die' or diag $@;
727 is $_, '*main::honthlew', 'PVLV concatenation works';
729 # And we should be able to overwrite it with a string, number, or refer-
730 # ence, too, if we omit the *.
731 $_ = *hon; $_ = 'tzor';
732 is $_, 'tzor', 'PVLV: assigning a string over a glob';
734 is $_, 23, 'PVLV: assigning an integer over a glob';
735 $_ = *hon; $_ = 23.23;
736 is $_, 23.23, 'PVLV: assigning a float over a glob';
737 $_ = *hon; $_ = \my $sthat;
738 is $_, \$sthat, 'PVLV: assigning a reference over a glob';
740 # This bug was found by code inspection. Could this ever happen in
742 # This duplicates a file handle, accessing it through a PVLV glob, the
743 # glob having been removed from the symbol table, so a stringified form
744 # of it does not work. This checks that sv_2io does not stringify a PVLV.
746 open *quin, "test.pl"; # test.pl is as good a file as any
748 ok eval { open my $zow, "<&", $_ }, 'PVLV: sv_2io stringifieth not'
751 # Similar tests to make sure sv_2cv etc. do not stringify.
753 ok eval { &$_ }, "PVLV glob can be called as a sub" or diag $@;
756 is eval { &$_ }, 2, 'PVLV holding a string can be called as a sub'
759 # Coderef-to-glob assignment when the glob is no longer accessible
760 # under its name: These tests are to make sure the OPpASSIGN_CV_TO_GV
761 # optimisation takes PVLVs into account, which is why the RHSs have to be
763 use constant gheen => 'quare';
767 is eval { &$_ }, 'quare',
768 'PVLV: constant assignment when the glob is detached from the symtab'
772 *gheck = sub { 'lon' };
774 is eval { &$_ }, 'lon',
775 'PVLV: coderef assignment when the glob is detached from the symtab'
778 # open should accept a PVLV as its first argument
780 ok eval { open $_,'<', \my $thlext }, 'PVLV can be the first arg to open'
783 # -t should not stringify
784 $_ = *thlit; delete $::{thlit};
786 ok defined -t $_, 'PVLV: -t does not stringify';
789 # but some systems don’t support this on file handles
793 open my $quile, "<", 'test.pl';
797 } ? $pass : $@ =~ /not implemented on filehandles/,
798 "PVLV: -T does not stringify";
800 # Unopened file handle
803 local $SIG{__WARN__} = sub { $w .= shift };
806 like $w, qr\unopened filehandle vor\,
807 'PVLV globs get their names reported in unopened error messages';
814 pass('Can assign integers to typeglobs');
816 pass('Can assign floats to typeglobs');
818 pass('Can assign strings to typeglobs');