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",

In reply to hash symbolic references by numele

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.