in reply to stop me from embarrassing myself
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
instead ofmy ($min, $hour, $day, $mon, $year) = (localtime(time))[1..5];
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.(undef, $min, $hour, $day, $mon, $year) = localtime(time);
But then all you do with them is produce a datestamp:
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.$starttime = "$mon$day$hour$min$year";
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
(tye)Re: Lexicals and timestamps
by tye (Sage) on Feb 27, 2001 at 21:04 UTC | |
by blueflashlight (Pilgrim) on Feb 28, 2001 at 00:06 UTC | |
Re: Lexicals and timestamps
by blueflashlight (Pilgrim) on Feb 28, 2001 at 00:04 UTC |