<?xml version="1.0" encoding="windows-1252"?>
<node id="998079" title="Re^3: Compilation error" created="2012-10-09 17:09:59" updated="2012-10-09 17:09:59">
<type id="11">
note</type>
<author id="861371">
kcott</author>
<data>
<field name="doctext">
&lt;p&gt;G'day [truthseeker66],&lt;/p&gt;
&lt;p&gt;++[toolic]'s answers are spot on. Here's some additional information.&lt;/p&gt;
&lt;blockquote&gt;&lt;em&gt;
"When do I use 'my' before an array and when don't I?"
&lt;/em&gt;&lt;/blockquote&gt;
&lt;p&gt;
While you're focussing on arrays with this specific question, the information provided applies equally to declaring other types of variables, e.g. &lt;c&gt;my $scalar&lt;/c&gt;, &lt;c&gt;my @array&lt;/c&gt; and &lt;c&gt;my %hash&lt;/c&gt;.
&lt;/p&gt;
&lt;p&gt;
So, as already stated, you declare your variable once then subsequently use it as many times as you want (without further declaration). If you declare a variable more than once you'll usually get a warning like this:
&lt;/p&gt;
&lt;code&gt;
$ perl -Mstrict -Mwarnings -e '
my $x;
my $x;
'
"my" variable $x masks earlier declaration in same scope at -e line 3.
&lt;/code&gt;
&lt;p&gt;&lt;em&gt;
&amp;#91;Advanced usage: there are ways to declare variables with the same name more than once within the same piece of code - that's rarely, if ever, needed and best avoided as it's confusing and will often lead to unexpected errors.&amp;#93;
&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
[http://perldoc.perl.org/functions/my.html|my] is the most frequently used way to declare variables - more in-depth information can be found in [http://perldoc.perl.org/perlsub.html|perlsub] (particularly under [http://perldoc.perl.org/perlsub.html#Private-Variables-via-my()|Private Variables via my()]).
&lt;/p&gt;
&lt;p&gt;
[http://perldoc.perl.org/perlsub.html|perlsub] also has a lot of information about other ways to declare variables, including [http://perldoc.perl.org/functions/local.html|local], [http://perldoc.perl.org/functions/our.html|our], [http://perldoc.perl.org/constant.html|constant] and [http://perldoc.perl.org/functions/state.html|state].
&lt;/p&gt;

&lt;blockquote&gt;&lt;em&gt;
"Does this code '{$a &lt;=&gt; $b}' work with only numbers?"
&lt;/em&gt;&lt;/blockquote&gt;
&lt;p&gt;
You can reverse the order of the sort by swapping &lt;c&gt;$a&lt;/c&gt; and &lt;c&gt;$b&lt;/c&gt;:
&lt;/p&gt;
&lt;code&gt;
$ perl -Mstrict -Mwarnings -E '
my @numbers = (2, 1, 3);
my @strings = qw{B A C};
say sort { $a &lt;=&gt; $b } @numbers;
say sort { $b &lt;=&gt; $a } @numbers;
say sort { $a cmp $b } @strings;
say sort { $b cmp $a } @strings;
'
123
321
ABC
CBA
&lt;/code&gt;


&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-861371"&gt;
&lt;p&gt;-- Ken&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
998032</field>
<field name="parent_node">
998040</field>
</data>
</node>
