Awesome Corion, great answer! And you've pinned the tail on the donkey--I was overconfident in my knowledge and thought I knew what I was doing when I didn't :-)
However, now I'm wondering why it ever works at all! In my test I did this: #!/usr/bin/perl
use Data::Dumper;
my $arg = shift;
my %actualhash = eval $arg;
print Dumper \%actualhash; #old bad way
my %badhash = ('r,2,g,2,w,1'); #if you don't think too hard, this look
+s correct.
print "\n%badhash:\n";
print Dumper \%badhash;
print $@;
my %hash = ('r',2,'g',2,'w',1);
print "\n%hash:\n";
print Dumper \%hash;
#print $@; unclutter output for posting to perlmonks--it just repeats
+the first error, of course
my @array = qw/r 2 g 2 w 1/;
my %newhash = @array;
print "\npassing through qw:\n";
print Dumper \%newhash;
#print $@;
Which yields:
rasto@frodo:~/cheat$ ./test.pl 'r,2,g,2,w,1,q,1'
$VAR1 = {};
%badhash:
$VAR1 = {
'r,2,g,2,w,1' => undef
};
Can't find string terminator "," anywhere before EOF at (eval 1) line
+1.
%hash:
$VAR1 = {
'w' => 1,
'r' => 2,
'g' => 2
};
passing through qw:
$VAR1 = {
'w' => '1',
'r' => '2',
'g' => '2'
};
So indeed, when I saw:
$VAR1 = {
'r,2,g,2,w,1' => undef
};
It became obvious that I had a syntax error and I quickly saw the problem--fuzzy thinking on my part :-)
But what's weird then is why it works at all, and why it stops working when adding a 'q'. I'm really quite curious now, although for my practical purposes it is only an intellectual excersize.
I'm thinking for convenience I'll just pass it through 'qw' as I did in the test above, which will actually make it more convenient to type my arguments, lol! ;-)
Thanks again for the excellent answer that taught me a problem solving skill, as opposed to simply answering :-D |