1#!./perl 2 3use strict; 4use warnings; 5 6BEGIN { 7 chdir 't' if -d 't'; 8 @INC = '../lib'; 9} 10 11use Test::More; 12 13BEGIN { $_ = 'foo'; } # because Symbol used to clobber $_ 14 15use Symbol; 16 17ok( $_ eq 'foo', 'check $_ clobbering' ); 18 19 20# First test gensym() 21my $sym1 = gensym; 22ok( ref($sym1) eq 'GLOB', 'gensym() returns a GLOB' ); 23 24my $sym2 = gensym; 25 26ok( $sym1 ne $sym2, 'gensym() returns a different GLOB' ); 27 28ungensym $sym1; 29 30$sym1 = $sym2 = undef; 31 32# Test geniosym() 33 34use Symbol qw(geniosym); 35 36$sym1 = geniosym; 37like( $sym1, qr/=IO\(/, 'got an IO ref' ); 38 39our $FOO = 'Eymascalar'; 40*FOO = $sym1; 41 42is( $sym1, *FOO{IO}, 'assigns into glob OK' ); 43 44is( $FOO, 'Eymascalar', 'leaves scalar alone' ); 45 46{ 47 local $^W=1; # 5.005 compat. 48 my $warn; 49 local $SIG{__WARN__} = sub { $warn .= "@_" }; 50 readline FOO; 51 like( $warn, qr/unopened filehandle/, 'warns like an unopened filehandle' ); 52} 53 54# Test qualify() 55package foo; 56 57use Symbol qw(qualify qualify_to_ref); # must import into this package too 58 59::ok( qualify("x") eq "foo::x", 'qualify() with a simple identifier' ); 60::ok( qualify("x", "FOO") eq "FOO::x", 'qualify() with a package' ); 61::ok( qualify("BAR::x") eq "BAR::x", 62 'qualify() with a qualified identifier' ); 63::ok( qualify("STDOUT") eq "main::STDOUT", 64 'qualify() with a reserved identifier' ); 65::ok( qualify("ARGV", "FOO") eq "main::ARGV", 66 'qualify() with a reserved identifier and a package' ); 67::ok( qualify("_foo") eq "foo::_foo", 68 'qualify() with an identifier starting with a _' ); 69::ok( qualify("^FOO") eq "main::\cFOO", 70 'qualify() with an identifier starting with a ^' ); 71::is( qualify('\*x'), 'foo::\*x', 72 'qualify() reference to a typeglob' ); 73::is( qualify('\*x', 'FOO'), 'FOO::\*x', 74 'qualify() reference to a typeglob' ); 75{ 76 use strict 'refs'; 77 my $fhref = qualify_to_ref("main::STDOUT"); 78 ::is( ref($fhref), 'GLOB', 79 'qualify_to_ref() returned ref to typeglob (1 argument)'); 80 81 my $key = 'baz'; 82 my $ref_to_key = qualify_to_ref( $key, __PACKAGE__ ); 83 ::is( ref($ref_to_key), 'GLOB', 84 'qualify_to_ref() returned ref to typeglob (2 arguments)'); 85 86 my $ref_to_key_scalar = *{$ref_to_key}{SCALAR}; 87 ::is( ref($ref_to_key_scalar), 'SCALAR', 88 'able to get SCALAR entry in typeglob'); 89} 90 91# tests for delete_package 92package main; 93no warnings 'once'; 94$Transient::variable = 42; 95ok( exists $::{'Transient::'}, 'transient stash exists' ); 96ok( defined $Transient::{variable}, 'transient variable in stash' ); 97Symbol::delete_package('Transient'); 98ok( !exists $Transient::{variable}, 'transient variable no longer in stash' ); 99is( scalar(keys %Transient::), 0, 'transient stash is empty' ); 100ok( !exists $::{'Transient::'}, 'no transient stash' ); 101 102$Foo::variable = 43; 103ok( exists $::{'Foo::'}, 'second transient stash exists' ); 104ok( defined $Foo::{variable}, 'second transient variable in stash' ); 105Symbol::delete_package('::Foo'); 106is( scalar(keys %Foo::), 0, 'second transient stash is empty' ); 107ok( !exists $::{'Foo::'}, 'no second transient stash' ); 108 109$Bar::variable = 44; 110ok( exists $::{'Bar::'}, 'third transient stash exists' ); 111ok( defined $Bar::{variable}, 'third transient variable in stash' ); 112ok( ! defined(Symbol::delete_package('Bar::Bar::')), 113 'delete_package() returns undef due to undefined leaf'); 114 115done_testing(); 116