<?xml version="1.0" encoding="windows-1252"?>
<node id="983867" title="Re: Variable scope headaches" created="2012-07-26 12:29:37" updated="2012-07-26 12:29:37">
<type id="11">
note</type>
<author id="170442">
jdporter</author>
<data>
<field name="doctext">
&lt;p&gt;
Trying to use a variable as a variable name (i.e. "symbolic references") is generally considered a bad idea.
&lt;/p&gt;&lt;p&gt;
There is an item in the [doc://perlfaq] about this: [http://perldoc.perl.org/perlfaq7.html#How-can-I-use-a-variable-as-a-variable-name?|How can I use a variable as a variable name?]. I highly suggest you read this.
&lt;/p&gt;&lt;p&gt;
It is considered critical enough that it is explicitly detected and forbidden by [doc://strict|use strict;] - the inclusion of which is universally considered good practice.
&lt;/p&gt;&lt;p&gt;
Instead, use a hash variable:
&lt;/p&gt;

&lt;code&gt;
  my %tag_value;

  foreach ( @tagvalue ) {
    if ( /([\w,_]+):([0-9]+)/ ) {
      $tag_value{$1} = $2;
    }
    else {
      die "Failed to assign value to $1\n";
    }
  }
&lt;/code&gt;

&lt;p&gt;
By the way I think your [doc://die] statement is going to have a problem, in that &lt;c&gt;$1&lt;/c&gt; will not be set (at least not to what you think) if it ever gets executed.  If the pattern match (regex) fails to match, then none of the positional variables (&lt;c&gt;$1&lt;/c&gt;, etc.) get set.
Instead, you might say something like:
&lt;/p&gt;


&lt;code&gt;
      die "Failed to match tag/value in '$_'\n";
&lt;/code&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-170442"&gt;
I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies[523120|.]
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
983857</field>
<field name="parent_node">
983857</field>
</data>
</node>
