Shlomi Fish  committed 41d6ebd

Add to bad-elements.

  • Participants
  • Parent commits 5e6fad0

Comments (0)

Files changed (3)

     hashrefs) using { ... }.
     - long lines in source code.
     - grep !/\A\./ for getting rid of "." and ".." in readdir().
+    - assigning a list to a scalar.
     - Regular expressions starting or ending with .*? or .*
     - Using one variable for two different things
         - assignment to a different value in the middle of the subroutine.
-    - assigning a list to a scalar.
     - Recursively iterating over directories and sub-directories instead of
     using File::Find, F::F::O, File::Next, etc.

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

+<item id="assigning_list_to_scalar" h="Assigining a List to a Scalar Variable">
+Normally, assigning from a function or an expression that returns a list
+to a scalar variable, will not yield what you want:
+my $characters = split(//, $string);
+This will cause the list as returned by split to be evaluated in scalar
+context, and to return a single (and not very meaningful) scalar item.
+You normally want one of those:
+my @characters = split(//, $string);
+my $chars_aref = [ split(//, $string) ];
+my $num_chars = () = split(//, $string); \# Use length instead in this case.
+A lot of the confusion stems from the fact that people expect arrays in Perl
+to be contained directly in scalars. For more information about that,
+consult <a href="$(ROOT)/topics/references/">our page about references</a>.
 #include "bad-elements-sources.wml"