$fin = open_or_throw('<', $file); process_conf($fin); # no close() needed } use Carp; sub open_or_throw { my ($mode, $filename) = @_; open my $h, $mode, $filename or croak "Could not open '$filename': $!"; return $h; } Emulation in other programming languages C++ The C++ Standard Library's associative containers (std::unordered_map and std::map) use operator[] to get the value associated to a key. If there is nothing associated to this key, it will construct it and value initialize the value. For simple types like int or float, the value initialization will be zero initialization. std::unordered_map<std::string, std::vector<int>> a; a["the answer"].push_back(42); // Autovivifies the a["the answer"] vector and then appends to it. Another example of counting occurrences of strings: std::unordered_map<std::string, int> counts; while (auto& s = GetNextString()) { counts[s]++; // creates counts[s] if it doesn't exist and set to zero, then increment. } A similar trick can be achieved with the insert() method, which returns an iterator to the element associated to the key, even if it already exists. Python Python's built-in dict class can be subclassed to implement autovivificious dictionaries simply by overriding the __missing__() method that was added to the class in Python v2.5. There are other ways of implementing the behavior, but the following is one of the simplest and instances of the class print just like normal Python dictionary objects. >>> class Tree(dict): ... def __missing__(self, key): ... value = self[key] = type(self)() ... return value >>> # Common names by class, order, genus, and type-species >>> common_names = Tree() >>> common_names['Mammalia']['Primates']['Homo']['H. sapiens'] = 'human being' >>> common_names {'Mammalia': {'Primates': {'Homo': {'H. sapiens': 'human being'}}}} >>> # Famous quotes by play, act, scene, and page >>> quotes = Tree() >>> quotes['Hamlet'][1][3][3] = 'This above all: to thine own self be true.' >>> quotes {'Hamlet': {1: {3: {3: 'This above all: to thine own self be true.'}}}} Ruby Ruby hashes can take a block specifying an object to be returned for non-existing indexes. These can be used to implement autovivificious maps. irb(main):001:0> tree = proc { Hash.new { |hash, key| hash[key] = tree.call } } => #<Proc:0x007fda528749a0@(irb):1> irb(main):002:0> lupin = tree.call => {} irb(main):003:0> lupin["express"][3] = "stand and deliver" => "stand and deliver" irb(main):004:0> lupin => {"express"=>{3=>"stand and deliver"}} Java Java Map has a method computeIfAbsent that can be used to emulate autovivificous maps. public static <K,V> Function<K, V> defaultDict(Map<K, V> map, Supplier<? extends V> supplier) { return key -> map.computeIfAbsent(key, k