title: Perl Variable References in Hash / Foreach
body: <p>I would like to change variables stored in a hash, but I kept receiving the error:
  <br/>
  <code>&quot;Can't use the string (&quot;SCALAR(0x30f558)&quot;) as a SCALAR ref while &quot;strict refs&quot; in use at - line 14.</code></p>
  <br/>
  <p>My simplified code is as follows:</p>
  <pre><code>#!/usr/bin/perl
  
  use strict;
  use warnings;
  
  
  my $num = 1234;
  my $a = 5;
  
  my %hash = (\$num =&gt; &quot;value&quot;);
  
  foreach my $key (keys %{hash}){
      print &quot;Key: $key\n&quot;;
      #OPTION1: $a = $$key;
  }
  
  my $ref = \$num ;
  print &quot;Ref: $ref\n&quot;;
  #OPTION2: $a = $$ref ;
  
  print $a;
  </code></pre>
  <br/>
  <p>Running this prints:</p>
  <pre><code>Key: SCALAR(0x30f558)
  Ref: SCALAR(0x30f558)
  5
  </code></pre>
  <p>showing that both $key and $ref are pointing to the same variable - $num
  <br/>
  Also, the code on OPTION1 and OPTION2 are identical if $key and $ref are the same.
  <br/></p>
  <p>When I uncomment OPTION2, $a prints out as 1234.
  <br/>
  When I uncomment OPTION1, however, I receive the error shown above.</p>
  <br/>
  <br/>
  <p>QUESTION:
  How do I change $a to $num using the hash as I tried to do in OPTION1? And why will this not work as is?</p>
  <br/>
  <hr />
  <p>References:
  <a href="http://cpansearch.perl.org/src/CHIPS/perl5.004_05/t/pragma/strict-refs" rel="nofollow noreferrer">http://cpansearch.perl.org/src/CHIPS/perl5.004_05/t/pragma/strict-refs</a>
  <br/>
  I followed this code closely:</p>
  <pre><code>use strict 'refs' ;
  my $fred ;
  my $b = \$fred ;
  my $a = $$b ;
  </code></pre>
  <p>which posed no error until I introduced the hash.</p>
  <br/>
  <p>Thank you for your help.</p>
  <br/>
  <hr />
  <p>Original Code (doesn't work):</p>
  <pre><code>#User Defined - here are the defaults
  my $a = 122160;
  my $b = 122351;
  my $c = 'string';
  my $d = 15;
  my $e = 123528;
  #etc.
  
  #Create variable/print statement hash
  my %UserVariables = (
  \$a =&gt;  &quot;A: (Default: $a): &quot;,
  \$b =&gt;  &quot;B: (Default: $b): &quot;,
  \$c =&gt;  &quot;C: (Default: $c): &quot;,
  \$d =&gt;  &quot;D: (Default: $d): &quot;,
  \$e =&gt;  &quot;E: (Default: $e): &quot;,
  );
  
  #Allow user to change variables if desired
  foreach (keys %UserVariables){
      print $UserVariables{$_};
      chomp (my $temp = &lt;&gt;);
      print &quot;$_\n&quot;;
      $$_ = $temp unless ($temp eq '');
      print &quot;$temp\n&quot; unless ($temp eq '');
  };
  </code></pre>
  <p>Less efficient method that does work:</p>
  <pre><code>#Alternate Method without loops (not ideal)
  my $temp;
  print $UserVariables{\$a};
      chomp ($temp = (&lt;&gt;));
      $a= $temp unless ($temp eq '');
  print $UserVariables{\$b};
      chomp ($temp = (&lt;&gt;));
      $b= $temp unless ($temp eq '');
  print $UserVariables{\$c};
      chomp ($temp = (&lt;&gt;));
      $c= $temp unless ($temp eq '');
  print $UserVariables{\$d};
      chomp ($temp = (&lt;&gt;));
      $d= $temp unless ($temp eq '');
  print $UserVariables{\$e};
      chomp ($temp = (&lt;&gt;));
      $e= $temp unless ($temp eq '');
  </code></pre>