On 7/8/19 4:00 PM, Mike Small wrote:
Uri Guttman <u...@stemsystems.com> writes:
On 7/4/19 2:41 PM, Mike Small wrote:
A co-worker was trying to take some of the elements from gmtime's return
value. He did something like the following:
$ perl -E'$,="\t";say gmtime[1..5]'
that is calling gmtime with the argument of [1..5] which is an
arrayref. so the arg to gmtime is some large address number. it is
then parsed as the timestamp and broken out. garbage in, garbage out!
Thanks Uri! That makes perfect sense now that I see it explained.
that is one reason a rule i use is to always (mostly!) use () for the
args list to builtins. it eliminates funny arg parsing and also shows to
the reader there is a function call with no args. it would have caught
the error as:
gmtime()[1..5]
should be a syntax error (didn't check).
also a better way to slice out gmtime is to assign it to an list of
scalars or an array slice (untested):
(undef, @gmt_parts[1..5) ) = gmtime() ;
and you can pass the list from gmtime directly to strftime (in POSIX) to
get a formatted date.
and in scalar context, you get a formatted date too. slicing into gmtime
isn't needed that often.
all the notes above cover localtime as well. gmtime is the basis for
localtime and the only difference is dealing with the local timezone and
its offset from GMT (same as UTC).
remember, write your code as if the maintainer is a homicidal maniac who
knows where you live!!
uri
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/