DelphiFAQ Home Search:
General :: Programming :: perl
General questions about perl.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Obtaining the time and time in different formats

The localtime function is described in the perlfunc.pod as: "[converting] a time as returned by the time function to a 9-element array with the time analyzed for the local time zone." It returns different values, depending on whether it is called in list or scalar context.

Using it as an argument to the print function, which takes its arguments in list context, returns an array of values. Therefore, it is similar to printing an array or list. You will get a concatenated string of numbers as each element of print's argument list is stringified before printing. (You can use the join function to separate the values. See example with next paragraph.)

Assigning the result of the function to a scalar, or forcing scalar context with the scalar operator, will return a date/time string. Similarly, using the substr() function (to get a substring) is also scalar context. The line containing 'substr' below gets the 1st 3 chars of localtime's scalar context returned string:

print localtime();                       # returns 1319121089952521
 print join (" ",localtime() );           # returns 13 19 12 10 8 99 5 252 1
 print scalar localtime();                # returns Fri Sep 10 12:19:13 1999
 $datetime = localtime();                 # assignment is scalar context
 print $datetime;                         # returns Fri Sep 10 12:19:13 1999
 

Generated 20:01:57 on Jun 26, 2017