When using $_ keep in mind that it may change when you're not expecting it to. For example, if a sub gets called between setting $_ and using it, there's a fair chance that $_ will have changed. apotheon's rule of thumb is a good defence against that. Using a well named variable also has the huge advantage that it conveys intent which makes code more robust, easier to understand and easier to maintain.
In the OP's code there is pretty much no chance that $_ is going to change unexpectedly as it stands, but for clarity a loop variable helps:for my $fileIdx (1 .. 5) { my $fileName = "$temp$fileIdx"; open $file, '>', fileName or die "Failed to create fileName: $!\n +"; ... }
In reply to Re^2: foreach loop and creating files with "$_"
by GrandFather
in thread foreach loop and creating files with "$_"
by james28909
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |