in reply to How to validate %hash = ( 'a' => 'b', ...);

Under use warnings; it gives a

Odd number of elements in hash assignment at ...

warning so define $SIG{ __WARN__ } to take some action.

Update: Here's a code example.

use strict; use warnings; $SIG{ __WARN__ } = sub { die q{Got a warning} }; my %hash = ( 'accents' => 'utf-8', 'accounting' => 'money', 'bank' => 'money'. 'ing' => 'money', 'perls' => 'perl', );

Update 2: Further updated code as per Camel Book, 4th edn. pp 1041 so as to get the actual warning message

use strict; use Carp qw{ carp croak confess cluck }; use warnings; $SIG{ __WARN__ } = sub { confess q{Got a warning: @_} }; my %hash = ( 'accents' => 'utf-8', 'accounting' => 'money', 'bank' => 'money'. 'ing' => 'money', 'perls' => 'perl', );

Output

Got a warning: @_ at xxxzzz line 5. main::__ANON__('Odd number of elements in hash assignment at x +xxzzz line 6.\x{a}') called at xxxzzz line 6

Update 3: The sub { confess q{Got a warning: @_} }; should of course have been sub { confess qq{Got a warning: @_} }; so that the @_ interpolates. The output then becomes

Got a warning: Odd number of elements in hash assignment at xxxzzz lin +e 6. at xxxzzz line 5. main::__ANON__('Odd number of elements in hash assignment at x +xxzzz line 6.\x{a}') called at xxxzzz line 6

Thus the perils of rushing to post before dashing out for an appointment :-/

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: How to validate %hash = ( 'a' => 'b', ...);
by RCH (Sexton) on Sep 12, 2016 at 04:20 UTC

    My thanks to all those who suggested

    use warnings;

    and special thanks for the extra

    use warnings; $SIG{ __WARN__ } = sub { confess q{Got a warning: @_} };