How anal are you regarding putting values that are consistent with variable names? Which one do you prefer?

# 1A. not very anal, but more concise for my $dir (glob "*") { # for a brief moment, $dir might not hold a directory's name next unless -d $dir; ... }
# 1B. more "correct", with additional related variable for my $dir0 (glob "*") { # a "0" suffix is my convention for variables that contain an unpr +ocessed value next unless -d $dir0; my $dir = $dir0; ... }
# 1C. more "correct", with additional variable and concept for my $entry (glob "*") { next unless -d $entry; my $dir = $entry; ... }
Another example:
# 2A. not very anal sub load { # actually, module can be in the form of "Foo::Bar" or "Foo/Bar.pm +". # but will be canonicalized to "Foo/Bar.pm" form my $module = shift; if ($module =~ /\A\w+(::\w+)*\z/) { $module = join("/", split /::/, $module) . ".pm"; } ... }
# 2B. sub load { my $arg = shift; my $module_pm; if ($arg =~ /\A\w+(::\w+)*\z/) { $module_pm = join("/", split /::/, $arg) . ".pm"; } else { $module_pm = $arg; } ... }

In general, if the content of variable becomes more precise, or change slightly, and then the variable gets mentioned again a few times, do you usually use a new variable? Input validation is the bread and butter of a programmer; do you usually put prevalidated value into $thing, knowing that at some point it is not yet or not certainly that thing?

EDIT 1: Changed next if to next unless.


In reply to Coding style: truth of variable name by perlancar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.