File: reftype.t

package info (click to toggle)
libscalar-list-utils-perl 1%3A1.70-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 948 kB
  • sloc: perl: 2,047; ansic: 130; makefile: 3
file content (57 lines) | stat: -rw-r--r-- 1,264 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!./perl

use strict;
use warnings;

use Test::More tests => 32;

use Scalar::Util qw(reftype);
use vars qw(*F);
use Symbol qw(gensym);

# Ensure we do not trigger and tied methods
tie *F, 'MyTie';
my $RE = $] < 5.011 ? 'SCALAR' : 'REGEXP';

my $s = []; # SvTYPE($s) is SVt_RV, and SvROK($s) is true
$s = undef; # SvTYPE($s) is SVt_RV, but SvROK($s) is false

my $t;
my @test = (
  [ undef, 1,             'number' ],
  [ undef, 'A',           'string' ],
  [ HASH   => {},         'HASH ref' ],
  [ ARRAY  => [],         'ARRAY ref' ],
  [ SCALAR => \$t,        'SCALAR ref' ],
  [ SCALAR => \$s,        'SCALAR ref (but SVt_RV)' ],
  [ REF    => \(\$t),     'REF ref' ],
  [ GLOB   => \*F,        'tied GLOB ref' ],
  [ GLOB   => gensym,     'GLOB ref' ],
  [ CODE   => sub {},     'CODE ref' ],
  [ IO     => *STDIN{IO}, 'IO ref' ],
  [ $RE    => qr/x/,      'REGEEXP' ],
);

foreach my $test (@test) {
  my($type,$what, $n) = @$test;

  is( reftype($what), $type, $n);
  next unless ref($what);

  bless $what, "ABC";
  is( reftype($what), $type, $n);

  bless $what, "0";
  is( reftype($what), $type, $n);
}

package MyTie;

sub TIEHANDLE { bless {} }
sub DESTROY {}

sub AUTOLOAD {
  our $AUTOLOAD;
  warn "$AUTOLOAD called";
  exit 1; # May be in an eval
}