perlNinny has asked for the wisdom of the Perl Monks concerning the following question:

Dear PerlMonks, I have been left without a date. Please help me:
#!/bin/perl my @keys =(Hi,There); my %views = (Hi,BYE,There,HERE); my $mdyt = system("date '+%m/%d/%Y %H:%M:%S'"); print $mdyt; foreach my $data (@keys) { print FILE $data.",".$views{$data}.",".$mdy +t."\n";}
It displays the date correctly on the screen but I get this in the file:
Hi,BYE,0
There,HERE,0

Dateless in Denver

Replies are listed 'Best First'.
Re: Dateless in Denver...
by Thelonius (Priest) on Aug 11, 2006 at 16:35 UTC
    my $mdyt = `date '+%m/%d/%Y %H:%M:%S'`;
    It displays correctly because the date command is outputting it to stdout, not because of "print $mdyt".

    You could also say (a bit more efficient)

    use POSIX; my $mdyt = strftime('+%m/%d/%Y %H:%M:%S', localtime(time));
      The '+' character is how the date command recognizes the format, POSIX::strftime will just return a literal '+' character so:
      use POSIX 'strftime'; my $mdyt = strftime '%m/%d/%Y %H:%M:%S', localtime;
Re: Dateless in Denver...
by jhourcle (Prior) on Aug 11, 2006 at 16:33 UTC

    Yep, it prints just fine on the screen. In fact, if you comment out 'print $mdyt', it prints just fine, too.

    You're calling 'system', which runs a command, and returns the exit status (0, no failure). It's the call to system which is printing the date to your screen, not the print statement.

    You want:

    my $mdyt = `date '+%m/%d/%Y %H:%M:%S'`;

    Update: Oh ... and I'm guessing that you've trimmed down the example from your real code, as you never open FILE.