Skip to content

Commit

Permalink
Fatalize using hash|array as reference
Browse files Browse the repository at this point in the history
This has been deprecated, scheduled to come out in 5.22
  • Loading branch information
khwilliamson committed Jun 16, 2014
1 parent faa7d3a commit 1f1ec7b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 48 deletions.
6 changes: 2 additions & 4 deletions op.c
Expand Up @@ -8086,8 +8086,7 @@ Perl_newAVREF(pTHX_ OP *o)
return o;
}
else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)) {
Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
"Using an array as a reference is deprecated");
Perl_croak(aTHX_ "Can't use an array as a reference");
}
return newUNOP(OP_RV2AV, 0, scalar(o));
}
Expand All @@ -8113,8 +8112,7 @@ Perl_newHVREF(pTHX_ OP *o)
return o;
}
else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)) {
Perl_ck_warner_d(aTHX_ packWARN(WARN_DEPRECATED),
"Using a hash as a reference is deprecated");
Perl_croak(aTHX_ "Can't use a hash as a reference");
}
return newUNOP(OP_RV2HV, 0, scalar(o));
}
Expand Down
26 changes: 12 additions & 14 deletions pod/perldiag.pod
Expand Up @@ -1187,6 +1187,18 @@ indicates that such a conversion was attempted.
(F) You tried to call perl with the B<-m> switch, but you put something
other than "=" after the module name.

=item Can't use a hash as a reference

(F) You tried to use a hash as a reference, as in
C<< %foo->{"bar"} >> or C<< %$ref->{"hello"} >>. Versions of perl <= 5.6.1
used to allow this syntax, but shouldn't have.

=item Can't use an array as a reference

(F) You tried to use an array as a reference, as in
C<< @foo->[23] >> or C<< @$ref->[99] >>. Versions of perl <= 5.6.1 used to
allow this syntax, but shouldn't have.

=item Can't use anonymous symbol table for method lookup

(F) The internal routine that does method lookup was handed a symbol
Expand Down Expand Up @@ -6434,20 +6446,6 @@ or if you meant this

You need to add either braces or blanks to disambiguate.

=item Using a hash as a reference is deprecated

(D deprecated) You tried to use a hash as a reference, as in
C<< %foo->{"bar"} >> or C<< %$ref->{"hello"} >>. Versions of perl <= 5.6.1
used to allow this syntax, but shouldn't have. It is now
deprecated, and will be removed in a future version.

=item Using an array as a reference is deprecated

(D deprecated) You tried to use an array as a reference, as in
C<< @foo->[23] >> or C<< @$ref->[99] >>. Versions of perl <= 5.6.1 used to
allow this syntax, but shouldn't have. It is now deprecated,
and will be removed in a future version.

=item Using just the first character returned by \N{} in character class in
regex; marked by S<<-- HERE> in m/%s/

Expand Down
63 changes: 45 additions & 18 deletions t/lib/warnings/op
Expand Up @@ -300,33 +300,60 @@ syntax error at - line 4, near "[]"
Execution of - aborted due to compilation errors.
########
# op.c
my (@foo, %foo);
%main::foo->{"bar"};
%foo->{"bar"};
@main::foo->[23];
@foo->[23];
$main::foo = {}; %$main::foo->{"bar"};
$foo = {}; %$foo->{"bar"};
$main::foo = []; @$main::foo->[34];
$foo = []; @$foo->[34];
no warnings 'deprecated';
my %foo;
%main::foo->{"bar"};
EXPECT
OPTION fatal
Can't use a hash as a reference at - line 3.
########
# op.c
my %foo;
%foo->{"bar"};
EXPECT
OPTION fatal
Can't use a hash as a reference at - line 3.
########
# op.c
my @foo;
@main::foo->[23];
EXPECT
OPTION fatal
Can't use an array as a reference at - line 3.
########
# op.c
my @foo;
@foo->[23];
EXPECT
OPTION fatal
Can't use an array as a reference at - line 3.
########
# op.c
my %foo;
$main::foo = {}; %$main::foo->{"bar"};
EXPECT
OPTION fatal
Can't use a hash as a reference at - line 3.
########
# op.c
my %foo;
$foo = {}; %$foo->{"bar"};
EXPECT
OPTION fatal
Can't use a hash as a reference at - line 3.
########
# op.c
my @foo;
$main::foo = []; @$main::foo->[34];
EXPECT
OPTION fatal
Can't use an array as a reference at - line 3.
########
# op.c
my @foo;
$foo = []; @$foo->[34];
EXPECT
Using a hash as a reference is deprecated at - line 3.
Using a hash as a reference is deprecated at - line 4.
Using an array as a reference is deprecated at - line 5.
Using an array as a reference is deprecated at - line 6.
Using a hash as a reference is deprecated at - line 7.
Using a hash as a reference is deprecated at - line 8.
Using an array as a reference is deprecated at - line 9.
Using an array as a reference is deprecated at - line 10.
OPTION fatal
Can't use an array as a reference at - line 3.
########
# op.c
use warnings 'void' ; no warnings 'experimental::smartmatch'; close STDIN ;
Expand Down
17 changes: 5 additions & 12 deletions t/op/kvhslice.t
Expand Up @@ -8,7 +8,7 @@ BEGIN {

# use strict;

plan tests => 44;
plan tests => 40;

# simple use cases
{
Expand Down Expand Up @@ -162,20 +162,13 @@ plan tests => 44;
is (scalar @warn, 0, 'no warning in list context');
}

# deprecated syntax
{
my $h = \%h;
@warn = ();
ok( eq_array([eval '%$h->{a}'], ['A']), 'works, but deprecated' );
is (scalar @warn, 1, 'one warning');
like $warn[0], qr{^Using a hash as a reference is deprecated},
"correct warning text";
eval '%$h->{a}';
like($@, qr/Can't use a hash as a reference/, 'hash reference is error' );

@warn = ();
ok( eq_array([eval '%$h->{"b","c"}'], [undef]), 'works, but deprecated' );
is (scalar @warn, 1, 'one warning');
like $warn[0], qr{^Using a hash as a reference is deprecated},
"correct warning text";
eval '%$h->{"b","c"}';
like($@, qr/Can't use a hash as a reference/, 'hash slice reference is error' );
}
}

Expand Down

0 comments on commit 1f1ec7b

Please sign in to comment.