File: reftypes.pl

package info (click to toggle)
libtest-weaken-perl 3.022000-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: perl: 3,828; makefile: 24; sh: 6
file content (186 lines) | stat: -rw-r--r-- 5,549 bytes parent folder | download | duplicates (4)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!perl

# This is a sandbox for experiments with referencing and dereferencing.
# It is not part of a test suite, not even an "author" test suite.

use strict;
use warnings;

use Scalar::Util qw(reftype weaken);
use Data::Dumper;
use Carp;
use English qw( -no_match_vars );
use Fatal qw(open);

sub try_dumper {
    my $probe_ref = shift;

    my @warnings = ();
    local $SIG{__WARN__} = sub { push @warnings, $_[0]; };
    printf {*STDERR} 'Dumper: %s', Data::Dumper::Dumper( ${$probe_ref} )
        or Carp::croak("Cannot print to STDERR: $ERRNO");
    for my $warning (@warnings) {
        print {*STDERR} "Dumper warning: $warning"
            or Carp::croak("Cannot print to STDERR: $ERRNO");
    }
    return scalar @warnings;
}

my $array_ref = \@{ [qw(42)] };
my $hash_ref   = { a => 1, b => 2 };
my $scalar_ref = \42;
my $ref_ref    = \$scalar_ref;
my $regexp_ref = qr/./xms;

## no critic (Subroutines::ProhibitCallsToUndeclaredSubs)
my $vstring_ref = \(v1.2.3.4);
## use critic

my $code_ref = \&try_dumper;

## no critic (Miscellanea::ProhibitFormats,References::ProhibitDoubleSigils,Subroutines::ProhibitCallsToUndeclaredSubs)
format fmt =
@<<<<<<<<<<<<<<<
$_
.
## use critic

## no critic (Subroutines::ProhibitCallsToUndeclaredSubs)
my $format_ref = *fmt{FORMAT};
my $glob_ref   = *STDOUT{GLOB};
my $io_ref     = *STDOUT{IO};
my $fh_ref     = do {
    no warnings qw(deprecated);
    *STDOUT{FILEHANDLE};
};
## use critic

## no critic (InputOutput::RequireBriefOpen)
open my $autoviv_ref, q{>&STDERR};
## use critic

my $string     = 'abc' x 40;
my $lvalue_ref = \( pos $string );
${$lvalue_ref} = 7;

my %data = (
    'scalar'  => $scalar_ref,
    'array'   => $array_ref,
    'hash'    => $hash_ref,
    'ref'     => $ref_ref,
    'code'    => $code_ref,
    'regexp'  => $regexp_ref,
    'vstring' => $vstring_ref,
    'format'  => $format_ref,
    'glob'    => $glob_ref,
    'io'      => $io_ref,
    'fh'      => $fh_ref,
    'autoviv' => $autoviv_ref,
    'lvalue'  => $lvalue_ref,
);

REF:
while ( my ( $name, $ref ) = each %data ) {
    printf {*STDERR} "==== $name, %s, %s ====\n", ( ref $ref ),
        ( reftype $ref)
        or Carp::croak("Cannot print to STDERR: $ERRNO");
    try_dumper( \$ref );
}

REF:
for my $data_name (qw(scalar vstring regexp ref )) {
    my $ref = $data{$data_name};
    printf {*STDERR} "=== Deref test $data_name, %s, %s ===\n", ( ref $ref ),
        ( reftype $ref )
        or Carp::croak("Cannot print to STDERR: $ERRNO");
    my $old_probe = \$ref;
    try_dumper($old_probe);
    my $new_probe = \${ ${$old_probe} };
    try_dumper($new_probe);
}

REF: for my $ref ($format_ref) {
    my $probe = \$ref;
    print {*STDERR} 'Trying to deref ', ( ref $probe ), q{ }, ( ref $ref ),
        "\n"
        or Carp::croak("Cannot print to STDERR: $ERRNO");
    try_dumper($probe);

    # How to dereference ?
}

REF: for my $ref ($lvalue_ref) {
    my $probe = \$ref;
    print {*STDERR} 'Trying to deref ', ( ref $probe ), q{ }, ( ref $ref ),
        "\n"
        or Carp::croak("Cannot print to STDERR: $ERRNO");
    try_dumper($probe);
    my $new_probe = \${ ${$probe} };
    printf {*STDERR} "pos is %d\n", ${$lvalue_ref};
    ${$lvalue_ref} = 11;
    printf {*STDERR} "pos is %d\n", ${$lvalue_ref};
}

REF: for my $ref ($io_ref) {
    my $probe = \$ref;
    print {*STDERR} 'Trying to deref ', ( ref $probe ), q{ }, ( ref $ref ),
        "\n"
        or Carp::croak("Cannot print to STDERR: $ERRNO");
    try_dumper($probe);
    my $new_probe = \*{ ${$probe} };
    print { ${$new_probe} } "Printing via IO ref\n"
        or Carp::croak("Cannot print via IO ref: $ERRNO");
}

REF: for my $ref ($fh_ref) {
    my $probe = \$ref;
    print {*STDERR} 'Trying to deref ', ( ref $probe ), q{ }, ( ref $ref ),
        "\n"
        or Carp::croak("Cannot print to STDERR: $ERRNO");
    try_dumper($probe);
    my $new_probe = \*{ ${$probe} };
    print { ${$new_probe} } "Printing via FH ref\n"
        or Carp::croak("Cannot print via FH ref: $ERRNO");
}

REF: for my $ref ($glob_ref) {
    my $probe = \$ref;
    print 'Trying to deref ', ( ref $probe ), q{ }, ( ref $ref ), "\n"
        or Carp::croak("Cannot print to STDERR: $ERRNO");
    try_dumper($probe);
    my $new_probe = \*{ ${$probe} };
    print { ${$new_probe} } "Printing via GLOB ref\n"
        or Carp::croak("Cannot print via GLOB ref: $ERRNO");
}

REF:
for my $data_name (qw( glob autoviv )) {
    my $ref = $data{$data_name};
    printf {*STDERR} "=== Deref test $data_name, %s, %s ===\n", ( ref $ref ),
        ( reftype $ref )
        or Carp::croak("Cannot print to STDERR: $ERRNO");
    my $old_probe = \$ref;
    try_dumper($old_probe);
    my $new_probe = \*{ ${$old_probe} };
    print { ${$new_probe} } "Printing via $data_name ref\n"
        or Carp::croak("Cannot print via $data_name ref: $ERRNO");
    try_dumper($new_probe);
}

REF:
while ( my ( $name, $ref ) = each %data ) {
    my $ref_value     = ref $ref;
    my $reftype_value = reftype $ref;
    printf
        "==== scalar ref test of $name, ref=$ref_value, reftype=$reftype_value\n"
        or Carp::croak("Cannot print to STDERR: $ERRNO");
    my $eval_result = eval { my $deref = ${$ref}; 1 };
    if ( defined $eval_result ) {
        print "scalar deref of $reftype_value ok\n"
            or Carp::croak("Cannot print to STDOUT: $ERRNO");
    }
    else {
        print "scalar deref of $reftype_value failed: $EVAL_ERROR"
            or Carp::croak("Cannot print to STDOUT: $ERRNO");
    }
} ## end while ( my ( $name, $ref ) = each %data )