Re: foreach loop and creating files with "$_"by xyzzy (Pilgrim)
|on Jul 22, 2014 at 03:58 UTC||Need Help??|
First one quick note:
In other words your assignment in the first line does nothing since the value gets clobbered when you use open
On to your question. I quickly glanced at the node and the author is basically saying that sometimes it is unclear what $_ is supposed to represent. Whether or not this is the case depends on the context that you use it in. For instance:
It's fairly obvious that each operation of the loop is performed on each line in @lines. However, this may not be the case if you have a longer, more complex loop, and will not work at all if you have nested loops:
There are also times where I will take advantage of $_ to perform a bunch of operations on a single variable in a sort of pseudo-loop:
You didn't hear it from me, but the best part about Perl is that once you understand how the lingo works, you can use it any way you damn please. Unless you work with people who won't understand what you're saying, and you want them to understand what you're saying. Then you have turn to these annoying things called "Best Practices".
$,=qq.\n.;print q.\/\/____\/.,q./\ \ / / \\.,q. /_/__.,q..
Happy, sober, smart: pick two.