1. Shlomi Fish
  2. perl-begin


Shlomi Fish  committed 9453d23

Add the initializing-arrays-using-arrayrefs.

  • Participants
  • Parent commits 2d23e24
  • Branches default

Comments (0)

Files changed (3)


View file
         - shell code injection.
         - cross-site-scripting / HTML injection.
         - SQL injection.
+    - initialising arrays (not array refs) using [...] and hashes (not
+    hashrefs) using { ... }.


View file
         - assignment to a different value in the middle of the subroutine.
     - grep !/\A\./ for getting rid of "." and ".." in readdir().
     - long lines in source code.
-    - initialising arrays (not array refs) using [...] and hashes (not
-    hashrefs) using { ... }.
     - assigning a list to a scalar.
 * #perl FAQ:

File src/tutorials/bad-elements/index.html.wml

View file
+<item id="init_arrays_from_arrayrefs" h="Initializing Arrays and Hashes from Anonymous References">
+Some beginners to Perl are tempted to use the anonymous array reference
+constructor (<tt>[ … ]</tt>) to initialise array variables, or alternatively
+anonymous hash references (<tt>{ … }</tt>) to initialise hash variables, like
+my @arr = [1 .. 10];
+my %uk_info = { continent =&gt; "Europe", capital =&gt; "London", };
+However, these reference constructors actually create a single scalar that
+contains a reference and, as a result, in the case of the array, one will have
+a single element array, and in case of the hash, one will have an error
+with a hash that was initialised only with a single key (that was converted
+to a non-sensical string).
+Array and hash variables should be initialized using lists enclosed in
+my @arr = (1 .. 100);
+my %uk_info = ( continent =&gt; "Europe", capital =&gt; "London", );
+For more information about the difference between references and aggregate
+variables, refer to our <a href="$(ROOT)/topics/references/">references</a>
 #include "bad-elements-sources.wml"