numele has asked for the wisdom of the Perl Monks concerning the following question:
I have a piece of code that works, but when I add use strict I get an error "Can't use string ("0") as a HASH ref while "strict refs" in use"
I comment out use strict and its fine.
#!/usr/local/bin/perl use strict; #use warnings; my %input; $input{"1"} = 0; $input{"2"} = 1; $input{"3"}->{a} = "ah"; $input{"3"}->{b} = "bee"; $input{"4"} = 'string'; jscript(%input); sub jscript { my %input = @_; my $total = scalar(keys(%input)); my $subtotal = $total--; my $cnt = 0; my $count = 0; my ($j,$NEST); print "Javascript literal\n\[ "; foreach my $number (sort keys %input) { $count++; unless ($input{$number} =~ /HASH/) { if ($count < $total) { print "\"$input{$number}\"\, "; } else { print "\"$input{$number}\""; } } foreach my $subject (keys %{ $input{$number} }) { if ($cnt == 0 ) { $j = '"'; } if ($cnt > 0 ) { $j = ', "';} $NEST .= "${j}${subject}\" : \"$input{$number}{$subject}\" +"; $cnt++; } if ($cnt > 0 && $count < $subtotal) { print "\{ $NEST \}\, "; $cnt = 0; $total++; $NEST = ""; } elsif ($cnt > 0 && $count == $subtotal) { print "\{ $NEST \} "; $cnt = 0; $NEST = ""; } } print " \]\n"; }
Script output without strict, which is perfect
Javascript literal [ "0", "1", { "a" : "ah", "b" : "bee" }, "string" ]
Script output with strict
Javascript literal Can't use string ("0") as a HASH ref while "strict refs" in use at ./r +.pl line 37. [ "0",
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hash symbolic references
by Eily (Monsignor) on Jan 29, 2015 at 00:16 UTC | |
by numele (Sexton) on Jan 29, 2015 at 00:26 UTC | |
by Mr. Muskrat (Canon) on Jan 29, 2015 at 18:35 UTC | |
|
Re: hash symbolic references
by ww (Archbishop) on Jan 28, 2015 at 23:55 UTC |