Is my understanding correct when I state the following:
It is wasting CPU cycles to test $ARGV (i.e. the file-name) for some condition inside a while(<>) loop. It is more efficient to test the file-name first, and then process each line accordingly inside a while() loop. This way it is not redundantly checking the file-name every time it grabs a line of data.
Or does the diamond operator do some magic to make this just as efficient as the latter?
Is it wasting CPU cycles?
You are running Perl. Which runs on a VM. Do you have any idea how many pointer dereferences a simple lookup of a global variable implies? Why would you care about cycles? /s
Although the
<>
operator does imply a fair amount of magic, this does not optimize your loops. Therefore, inthe
ne
check will be executed for every single line. If you are optimizing for code size, or development time, this is totally acceptable. If you are optimizing for the total number of Opcodes executed, it may sometimes be cheaper to do the opening of the files explicitly:I personally find this form to be more elegant (single assignment form), and it isn't that much longer anyway. But in a quick oneliner, I'd use the first solution because my script could had already run a thousand times while I'd still be writing the longer form.