Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Perl's localtime with print to get the timestamp?

Tags:

I have used the following statements to get the current time.

  print "$query executed successfully at ",localtime;   print "$query executed successfully at ",(localtime);   print "$query executed successfully at ".(localtime); 

Output

 executed successfully at 355516731103960  executed successfully at 355516731103960  executed successfully at Wed Apr  7 16:55:35 2010 

The first two statements are not printing the current time in a date format. Third statement only giving the correct output in a date format.

My understanding is the first one is returning a value in scalar context, so it is returning numbers.

Then in the second print I used localtime in list context only, why it's also giving number output.

like image 344
kiruthika Avatar asked Apr 07 '10 11:04

kiruthika


People also ask

How do I print a time stamp in Perl?

Creating a Timestamp Timestamp can be created by creating a DateTime object and then calling the now constructor. my $datetime = DateTime->now; print "$datetime\n" ; A DateTime object can also be created by providing all the details part wise like date, hour, minute, second, etc.

How do I change the date format in Perl?

use v5. 10; use POSIX qw(strftime); my $date = '19700101'; my @times; @times[5,4,3] = $date =~ m/\A(\d{4})(\d{2})(\d{2})\z/; $times[5] -= 1900; $times[4] -= 1; # strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) say strftime( '%d-%b-%Y', @times );

What is Perl Localtime?

In Perl, localtime is defined as a time function which displays the date and time for the local time zones that usually converts a specified time into a certain list of time elements which are numerical data types for all the elements in the list returned by this localtime() function in Perl.


2 Answers

Perhaps the most important thing you can learn for programming in Perl, is context. Many built-in subroutines, and operators, behave differently depending on the context.

print "$query executed successfully at ", localtime, "\n"; # list context print "$query executed successfully at ",(localtime),"\n"; # list context print "$query executed successfully at ". localtime, "\n"; # scalar context print "$query executed successfully at ".(localtime),"\n"; # scalar context  print "$query executed successfully at ", scalar  localtime, "\n"; # scalar context print "$query executed successfully at ", scalar (localtime),"\n"; # scalar context 

This can be made clearer by splitting up the statements.

my $time = localtime; # scalar context print "$query executed successfully at $time\n";  my @time = localtime; # list context print "$query executed successfully at @time\n"; 
like image 77
Brad Gilbert Avatar answered Oct 21 '22 10:10

Brad Gilbert


The return value of localtime depends on the context. In list context it is a 9-element list, while in scalar context it is a ctime(3) value:

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; my $now_string = localtime;  # e.g., "Thu Oct 13 04:54:34 1994" 

Now, print provides list context for its arguments. That's why print localtime outputs the numbers from the list (without any separator by default). You can force scalar context on localtime using either the . concatenation operator (like in your 3rd statement) or using scalar:

print "".localtime; print scalar localtime; 
like image 24
Eugene Yarmash Avatar answered Oct 21 '22 10:10

Eugene Yarmash