This might seem simple to alot of you old hash veterans, but I keep hitting this error even though I know it lurks there. I'm often finding myself, using hashes to hold identifiers to objects, especially in Tk, where I want to create a large set of non-anonymous objects in loops. So almost everytime I try to set up my loops, I run into the "Can't use string ("1") as a HASH ref while "strict refs" in use at....." error.

So I've read all the stuff on auto-vivication, and similar posts. What I end up doing is falling back on a little trick I've stumbled upon, and just resort to using it to get around the error. My question is:

Is it correct to think about it this way, or am I just lucky? :-)

My idea is you can't create "depth" in a hash, by assigning a value to it. The code below shows the basic idea.

Usually I have a base item as a hash, like $box{$X}{$Y}{$z}. Now the problem comes when you want to attach data to it, if the "depth" for what you want to assign a value to dosn't exist yet, it gives an error; but if you "artificially" create the depth, even if it's undef, it all works well.

So in the code, I want to add a {'text'} key to the hash, but it won't let me, until I create a fake key {'rect'} at that level, then all goes well. But then my "base" turns from $box{$X}{$Y}{$z} to $box{$X}{$Y}{$z}{'rect'}. This is just an annoyance, but it always seems to work. Does anyone see pitfalls in this approach.?

#!/usr/bin/perl use warnings; use strict; # demonstrates strict hash refs # Commonly seen error # Can't use string ("1") as a HASH ref while "strict refs" in use at . +/z line my %box; my $x = 10; my $y = 10; my $z = 1; foreach my $X ( 0 .. $x ) { foreach my $Y ( 0 .. $y ) { # you cannot create a new hash depth by assigning # a value to it $box{$X}{$Y}{$z} = 1; #dosn't work and causes err +or below # $box{$X}{$Y}{$z}{'rect'} = 1 ; #works # $box{$X}{$Y}{$z}{'rect'} = undef ; #works $box{$X}{$Y}{$z}{'text'} = 2; #this gives strict refs error + } }

I'm not really a human, but I play one on earth. flash japh

In reply to The "no strict refs" problem revisited. by zentara

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.