title: How do I disable autovivification in Perl?
body: <p>Suppose you have a HUGE application &quot;develoopped&quot; ;) by a big team.
  Here is a simplified model of the potential disaster that may occur when somebody checks too deep in a data structure.
  If not possible to disable autovification completely or in scope, how to work around this?
  Thank you very much :) !!!!</p>
  <pre><code>use strict; use warnings;use Data::Dumper;
  
  my $some_ref = {akey=&gt;{deeper=&gt;1}};
  print Dumper($some_ref );
  if($some_ref-&gt;{deep}{doot} == 1){
      print 'too deep '.$/;
  }
  
  if($some_ref-&gt;{deep}){
      print 'Already in a deep doot'.$/;
  }
  
  print Dumper($some_ref );
  </code></pre>
  <p>This outputs the following:</p>
  <pre><code>$VAR1 = {
            'akey' =&gt; {
                        'deeper' =&gt; 1
                      }
          };
  Use of uninitialized value in numeric eq (==) at autovivify_test.pl line 5.
  Already in a deep doot
  $VAR1 = {
            'deep' =&gt; {},
            'akey' =&gt; {
                        'deeper' =&gt; 1
                      }
          };
  </code></pre>
  <p>Yes I know there is a warning, but... it may be too late.</p>
  <p>It will help to say that my hashref references a tied HASH.</p>
  <p>May be if I implement a good FETCH method which checks for checks deeper in the structure, I will solve easily my problem?</p>
  <hr />
  <p>I looked at <a href="http://search.cpan.org/%7Ekvail/Tie-StrictHash-1.0/StrictHash.pm" rel="nofollow noreferrer">Tie::StrictHash</a>,<a href="http://perldoc.perl.org/5.8.8/Tie/Hash.html" rel="nofollow noreferrer">Tie::Hash</a> and <a href="http://perldoc.perl.org/perltie.html#Tying-Hashes" rel="nofollow noreferrer">perltie</a>.
  Here is simplified version of my solution:</p>
  <pre><code>#!/usr/bin/env perl;
  #test_tie.pl
  
  package StrictHash;
  use strict; use warnings;
  use Tie::Hash;
  our @ISA = qw(Tie::StdHash);
  use Carp;
  
  sub TIEHASH {
      my $class = shift;
      my $hash = bless {@_}, $class;
      return $hash;
  }
  ##========================================================================
  ## FETCH fails if applied to a member that doesn't exist.
  ##========================================================================
  sub FETCH {
      my ($hash, $key) = @_;
      Carp::confess &quot;key '$key' does not exist&quot; unless exists $hash-&gt;{$key};
      return $hash-&gt;{$key};
  }
  ##========================================================================
  package main;
  use strict;use warnings;use Data::Dumper;
  #Imagine StrictHash is in ./StrictHash.pm
  #use StrictHash;
  my %hash;
  tie %hash, 'StrictHash', akey =&gt; {deeper=&gt;1} ;  
  
  my $some_ref =\%hash;
  print Dumper($some_ref );
  if($some_ref-&gt;{deep}{doot} == 1){
      print 'too deep '.$/;
  }
  </code></pre>
  <p>What I achieved is to touch only one place in the app.
  Now all Places like if($some_ref-&gt;{deep}{doot}) will cause die with stack-trace.
  So I will easily find them and correct them.
  And new writings of this kind will NOT be possible.
  Perl is good for big apps too, you just need to know more ;).</p>
  <p>Thank you all!
  I hope this helps others too.</p>