You appear to be using a boatload of lexicals, all defined at the top of the script. This is only a marginal improvement over using global variables. My personal preference is to introduce them just before they are needed, and if possible, enclosed in blocks so that they can go out of scope quickly (when no longer needed).

For instance it would be better to say

my ($min, $hour, $day, $mon, $year) = (localtime(time))[1..5];
instead of
(undef, $min, $hour, $day, $mon, $year) = localtime(time);
What you're doing here is taking an array slice out of what localtime returns you, so you're saying explicitly what you're interested in. This way you can get rid of the undef, and introduce the lexicals at their point of initialisation. By the way, if you call localtime without any parameters, it will use time by default, so that's a little redundant. My preference is to just call localtime. Less clutter.

But then all you do with them is produce a datestamp:

$starttime = "$mon"."$day"."$hour"."$min"."$year";
here you are performing a series of null interpolations. You are better off saying
$starttime = "$mon$day$hour$min$year";
hopefully you'll think that's a little cleaner.

But all of that is pretty moot, because a better way to generate timestamps (better because it obviates the need for a slew of lexicals) is to use an anonymous sub to diddle the localtime values on the fly.

my $starttime = sprintf '%4d%02d%02d%02d%02d', sub { $_[5]+1900, $_[4]+1, $_[3], $_[2], $_[1] }->(loc +altime); }

Hint: $_[3] et al. are scalars coming into the anonymous sub (via @_). These parameters are coming in from localtime. The subroutine is returning a list of scalars (reordering and munging various elements on the fly). The returned list is fed to sprintf and it just so happens that the list dovetails nicely into the format string.


In reply to Lexicals and timestamps by grinder
in thread stop me from embarrassing myself by blueflashlight

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.