Shlomi Fish avatar Shlomi Fish committed 61acad8

Add the conditional my to bad-elements.

Comments (0)

Files changed (3)

               .
               push @array_of_arrays, \@array;
           }
+    - Conditional "my"s:
+        - my $x if EXPR();
 * Add to '/bad-elements/':
     - Using one variable for two different things
         - assignment to a different value in the middle of the subroutine.
-    - Conditional "my"s:
-        - my $x if EXPR();
 
 * #perl FAQ:
     - references.

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

 
 </item>
 
+<item id="conditional_my_decls" h="Conditional my declarations.">
+
+<p>
+It is not a good idea to append a trailing if statement modifier to a
+declaration of a lexical variable using <tt>my</tt>:
+</p>
+
+<bad_code>
+my $var = VALUE() if (COND());
+
+my ($var1, @array2) if (COND());
+</bad_code>
+
+<p>
+This code might compile and appear to run but you probably want to declare
+a lexical variable for the rest of its scope. If you need to assign to it
+conditionally, then do it in a separate statement:
+</p>
+
+<pre>
+my $var;
+
+if (COND())
+{
+    $var = VALUE();
+}
+</pre>
+
+</item>
+
 </main_list>
 
 #include "bad-elements-sources.wml"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.