Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Dateless in Denver...

by perlNinny (Beadle)
on Aug 11, 2006 at 16:24 UTC ( #566878=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://566878]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2022-08-18 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?