If you have to simply copy the value from one variable to another just because the value has changed, you're probably going to far. (Your first example.)
If, however, some transformation was applied, might as well use an accurate variable name for the transformed value. (Your second example.)
It's often that one must deal with file name, file paths and absolute paths in the same piece of code. I use the following convention to distinguish them:
As such, you'll find me doing
while (defined( my $fn = readdir($dh) )) { my $qfn = "$dir_qfn/$fn"; ... }
No point in using the same variable. The same goes for your second example.
sub load { my ($pkg) = @_; my $qfn = $pkg =~ s{::}{/}gr . '.pm'; ... }
Why would you use the same var? To save memory? Perl might not be the best choice of language if you think that's important.
In your first example, you call the variable "dir", but you would already have a variable by that name in practice.
for my $qfn (glob("\Q$dir_qfn\E/*")) { stat($qfn) or do { warn("Skipping \"$qfn\": Can't stat: $!\n"); next; }; next if !-d _; ... }
You could also use the following:
for my $subdir_qfn (glob("\Q$dir_qfn\E/*")) { stat($qfn) or do { warn("Skipping \"$subdir_qfn\": Can't stat: $!\n"); next; }; next if !-d _; ... }
Sure, it might not be a subdir, but you could think of it as a subdir candidate. Adding another variable to the mix wouldn't help.
If someone wanted to assist on not putting the value in $subdir_qfn unless the name matches perfectly, one could use the following:
for my $subdir_qfn ( grep { if (stat($_)) { -d _ } else { warn("Skipping \"$_\": Can't stat: $!\n"); 0 } } glob("\Q$dir_qfn\E/*") ) { ... }
In reply to Re: Coding style: truth of variable name
by ikegami
in thread Coding style: truth of variable name
by perlancar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |