title: How can I store hash paths to reuse them in perl?
body: <p>Lets say I have a huge amount of elements nested in an hash (from a JSON). And I want to have getters and setters for them. For example:</p>
  
  <pre><code>sub set_element_x{
  
      my ($self, $hash_ref, $value) = @_;
  
      $hash_ref-&gt;{'path1'}-&gt;{'path2'}-&gt;{'x'} = $value;
  
      return;
  }
  
  sub get_element_x{
  
      my ($self, $hash_ref, $value) = @_;
  
      return $hash_ref-&gt;{'path1'}-&gt;{'path2'}-&gt;{'x'};
  }
  </code></pre>
  
  <p>Is there a standard way of handling this paths so that you only have to change them once in case something changes in the JSON structure?</p>
  
  <p>I want to add some more information because I think I did not explain my self properly.</p>
  
  <p>I want to be able to store in a common place this piece of code to access a value inside the JSON:</p>
  
  <pre><code>-&gt;{'path1'}-&gt;{'path2'}-&gt;{'x'}
  </code></pre>
  
  <p>Which in the future may change to:</p>
  
  <pre><code>-&gt;{'path1_1'}-&gt;{'path2_1'}-&gt;{'path3_1'}-&gt;{'x_1'}
  </code></pre>
  
  <p>PD: If I use the piece of code provided by @stevieb 's answer I'm not able to save the structure that I want. Adding this two lines:</p>
  
  <pre><code>print Dumper($href) . "\n";
  print to_json($href) . "\n";
  </code></pre>
  
  <p>Yields to:</p>
  
  <pre><code>$VAR1 = {
            'PATH1' =&gt; {
                         'PATH2' =&gt; {
                                      'a' =&gt; 'aaa',
                                      'b' =&gt; 'bbb',
                                      'y' =&gt; 'yyy',
                                      'z' =&gt; 'zzz'
                                    }
                       }
          };
  {"PATH1":{"PATH2":{"a":"aaa","b":"bbb","y":"yyy","z":"zzz"}}}
  </code></pre>
  
  <p>So to sum up. I want to be able to store the hash path of a complex structure with a number of keys which is not fixed.</p>