A minor quibble with the way you generate timestamps (in the LocalTime subroutine).
- I don't like little subroutines that call print themselves. I prefer to return
a string, and have the parent code call print. This lets you fold the call into a
list of other things that are also passed to print. One of these days you'll want to
fetch what LocalTime produces, but not have it print out anything. At that
point you will be forced to either clone the code into a separate routine (and if
you're smart you'll refactor the current routine in terms of that new routine), or
else you'll have to pass an ugly mode param that tells the code whether it should
print out or just return the string.
- Use the ISO date format.
- Call localtime once.
Putting it all together, you get something like:
sub timestamp {
sprintf '%4d-%02d-%02d:%02d:%02d:%02d',
sub{ $_[5]+1900, $_[4]+1, @_[reverse 0..3] }->(localtime)
}
Note that this string is not perfect as it does not encode the local time zone. That,
of course, is left as an exercise to the reader.
-- g r i n d e r
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|