This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
mktables: Add comment
[perl5.git] / lib / Symbol.t
CommitLineData
66deed98 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
66deed98 6}
7
66759c69 8use Test::More tests => 26;
66deed98 9
10BEGIN { $_ = 'foo'; } # because Symbol used to clobber $_
11
12use Symbol;
13
c74f62b5 14ok( $_ eq 'foo', 'check $_ clobbering' );
66deed98 15
16
17# First test gensym()
18$sym1 = gensym;
c74f62b5 19ok( ref($sym1) eq 'GLOB', 'gensym() returns a GLOB' );
66deed98 20
21$sym2 = gensym;
22
c74f62b5 23ok( $sym1 ne $sym2, 'gensym() returns a different GLOB' );
66deed98 24
25ungensym $sym1;
26
27$sym1 = $sym2 = undef;
28
ae716a98
YST
29# Test geniosym()
30
31use Symbol qw(geniosym);
32
33$sym1 = geniosym;
34like( $sym1, qr/=IO\(/, 'got an IO ref' );
35
36$FOO = 'Eymascalar';
37*FOO = $sym1;
38
39is( $sym1, *FOO{IO}, 'assigns into glob OK' );
40
41is( $FOO, 'Eymascalar', 'leaves scalar alone' );
42
43{
44 local $^W=1; # 5.005 compat.
45 my $warn;
46 local $SIG{__WARN__} = sub { $warn .= "@_" };
47 readline FOO;
48 like( $warn, qr/unopened filehandle/, 'warns like an unopened filehandle' );
49}
66deed98 50
51# Test qualify()
52package foo;
53
54use Symbol qw(qualify); # must import into this package too
55
c74f62b5
RGS
56::ok( qualify("x") eq "foo::x", 'qualify() with a simple identifier' );
57::ok( qualify("x", "FOO") eq "FOO::x", 'qualify() with a package' );
58::ok( qualify("BAR::x") eq "BAR::x",
59 'qualify() with a qualified identifier' );
60::ok( qualify("STDOUT") eq "main::STDOUT",
61 'qualify() with a reserved identifier' );
62::ok( qualify("ARGV", "FOO") eq "main::ARGV",
63 'qualify() with a reserved identifier and a package' );
64::ok( qualify("_foo") eq "foo::_foo",
65 'qualify() with an identifier starting with a _' );
66::ok( qualify("^FOO") eq "main::\cFOO",
67 'qualify() with an identifier starting with a ^' );
83628169
RGS
68
69# tests for delete_package
70package main;
71$Transient::variable = 42;
72ok( exists $::{'Transient::'}, 'transient stash exists' );
73ok( defined $Transient::{variable}, 'transient variable in stash' );
74Symbol::delete_package('Transient');
75ok( !exists $Transient::{variable}, 'transient variable no longer in stash' );
76is( scalar(keys %Transient::), 0, 'transient stash is empty' );
77ok( !exists $::{'Transient::'}, 'no transient stash' );
66759c69
JK
78
79$Foo::variable = 43;
80ok( exists $::{'Foo::'}, 'second transient stash exists' );
81ok( defined $Foo::{variable}, 'second transient variable in stash' );
82Symbol::delete_package('::Foo');
83is( scalar(keys %Foo::), 0, 'second transient stash is empty' );
84ok( !exists $::{'Foo::'}, 'no second transient stash' );
85
86$Bar::variable = 44;
87ok( exists $::{'Bar::'}, 'third transient stash exists' );
88ok( defined $Bar::{variable}, 'third transient variable in stash' );
89ok( ! defined(Symbol::delete_package('Bar::Bar::')),
90 'delete_package() returns undef due to undefined leaf');