| 1 |
#!./perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More tests => 32; |
| 7 |
|
| 8 |
use Scalar::Util qw(reftype); |
| 9 |
use vars qw(*F); |
| 10 |
use Symbol qw(gensym); |
| 11 |
|
| 12 |
# Ensure we do not trigger and tied methods |
| 13 |
tie *F, 'MyTie'; |
| 14 |
my $RE = $] < 5.011 ? 'SCALAR' : 'REGEXP'; |
| 15 |
|
| 16 |
my $s = []; # SvTYPE($s) is SVt_RV, and SvROK($s) is true |
| 17 |
$s = undef; # SvTYPE($s) is SVt_RV, but SvROK($s) is false |
| 18 |
|
| 19 |
my $t; |
| 20 |
my @test = ( |
| 21 |
[ undef, 1, 'number' ], |
| 22 |
[ undef, 'A', 'string' ], |
| 23 |
[ HASH => {}, 'HASH ref' ], |
| 24 |
[ ARRAY => [], 'ARRAY ref' ], |
| 25 |
[ SCALAR => \$t, 'SCALAR ref' ], |
| 26 |
[ SCALAR => \$s, 'SCALAR ref (but SVt_RV)' ], |
| 27 |
[ REF => \(\$t), 'REF ref' ], |
| 28 |
[ GLOB => \*F, 'tied GLOB ref' ], |
| 29 |
[ GLOB => gensym, 'GLOB ref' ], |
| 30 |
[ CODE => sub {}, 'CODE ref' ], |
| 31 |
[ IO => *STDIN{IO}, 'IO ref' ], |
| 32 |
[ $RE => qr/x/, 'REGEEXP' ], |
| 33 |
); |
| 34 |
|
| 35 |
foreach my $test (@test) { |
| 36 |
my($type,$what, $n) = @$test; |
| 37 |
|
| 38 |
is( reftype($what), $type, $n); |
| 39 |
next unless ref($what); |
| 40 |
|
| 41 |
bless $what, "ABC"; |
| 42 |
is( reftype($what), $type, $n); |
| 43 |
|
| 44 |
bless $what, "0"; |
| 45 |
is( reftype($what), $type, $n); |
| 46 |
} |
| 47 |
|
| 48 |
package MyTie; |
| 49 |
|
| 50 |
sub TIEHANDLE { bless {} } |
| 51 |
sub DESTROY {} |
| 52 |
|
| 53 |
sub AUTOLOAD { |
| 54 |
our $AUTOLOAD; |
| 55 |
warn "$AUTOLOAD called"; |
| 56 |
exit 1; # May be in an eval |
| 57 |
} |