in reply to Re^2: How to split a non even number of string elements into a hash
in thread How to split a non even number of string elements into a hash [RESOLVED]

... I do not really want to silence warnings. There are there for some reason, even if you silence them temporarily and you enable them after.

And, of course, the reason in this case is to warn you about an unpaired key in a set of key/value pairs. But you want to accept an unpaired key, so if the oddball can only be at the end of the input string and if its default value is undef:

c:\@Work\Perl\monks>perl -le "use strict; use warnings; use Data::Dumper; ;; my %hash; ;; my $str = 'one 1 two 2 three 3 odd_element'; { no warnings 'misc'; %hash = split / /, $str; } print Dumper \%hash; " $VAR1 = { 'odd_element' => undef, 'three' => '3', 'one' => '1', 'two' => '2' };
and you're done. KISS.

Update: Actually, the following is even KISSier and less messy:

my %hash = do { no warnings 'misc'; split / /, $str; };
I don't know why I didn't use it to begin with.


Give a man a fish:  <%-{-{-{-<