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

    When $number is equal to 1 in keys %{ $input{$number} }, you end up with keys %{ $input{"1"} } which is keys %{"0"}, so you are trying to read the keys of a hash named 0. Under strict, perl won't let you use a string as a ref. Luckily %{"0"} is unlikely, so when you want the keys of that hash, perl finds none, and it does what you want unless strict is present. But with $number equal to 4, you will try to look into %string which may really well exist, and you'll be using the values of an existing hash by mistake... oups!

    Before looking at the content of the hash, you must check that it actually is one: if (ref $input{$number} eq 'HASH').

    So here strict did not tell you about something that did not go as you wanted, it warned you about a disaster ready to happen. And warnings helps a lot too.

    Now you know how to correct that code. But do you actually need it? It look like you're trying to output JSON from perl data. If your input data structure is a hash and you want an array you'll have to make a conversion (@list = map $hash{$_}, sort keys %hash;), but you may get just what you want and need with a working module. Try that on the command line (if you're on windows, you may want to replace the ' by "):perl -MJSON -MData::Dumper -e '$data = [0, 1, 2, { a => 1, b => 2}, qq<String>]; print Dumper $data; print encode_json $data;'

      I'm an idiot! I just needed to verify a hash content as you stated and now I can use strict. Thank you!

      if (ref $input{$number} eq 'HASH') {

        Pay more attention to Eily's last paragraph because it is the most important thing to take away from that post. Don't roll your own JSON module for any purpose other than to learn how it works.

Re: hash symbolic references
by ww (Archbishop) on Jan 28, 2015 at 23:55 UTC

    When commenting out use strict; (or, for that matter, when turning off warnings) makes your code appear to be clean, untroubled and, in your view, capable of producing "perfect (output) ...

    Be suspicious; BE VERY SUSPICIOUS

    That probably means there really is something very wrong that could jump up a bite you someday!