How to write the current timestamp in a Perl file?

How do I write the current timestamp in a Perl file?

I have made a file named which will print the current timestamp. The file is given below:

@timeData = localtime(time);
print "@timeData\n";

Now I am trying to redirect the output of this file into another text file. The script is below:

@myscript = "/usr/bin/";
@myfile = "/usr/bin/output_for_myperl.txt";
perl "myscript" > "myfile\n";

While running this I am getting below error:

perl String found where operator expected at line 4, near "perl "myscript"" (Do you need to predeclare perl?) syntax error at line 4, near "perl "myscript"" Execution of aborted due to compilation errors.


Another tip. If you want to control the format of the timestamp, I usually throw in a subroutine like the following. This will return a scalar in the format of "20120928 08:35:12".

sub getLoggingTime {

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    my $nice_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",
    return $nice_timestamp;

Then change your code to:

my $timestamp = getLoggingTime();

You need a file-handle to write to a file:


use strict;
use warnings;

my $timestamp = localtime(time);

open my $fh, '>', '/tmp/file'
   or die "Can't create /tmp/file: $!\n";

print $fh $timestamp;

close $fh;

Some documentation: open, Leaning Perl

Another solution is a script without a filehandle, just a print, then on the command line:

./ > new_date_file

Need Your Help

ifconfig command not found

linux centos command ifconfig

I've just installed CentOS7 as a virtual machine on my mac (osx10.9.3 + virtualbox)

How to make an UIPickerView with a Done button?

ios swift uipickerview

I am having difficulties to make an UIPickerView with a done button to appear when the users taps a UITextField. This is my code so far. Everything builds fine, but when I tap the text field, the