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

Monks,
I'm feeling like an idiot. I can't get the date to redirect to the file. It keeps printing out to the screen. I'm sure it's something obvious. Can someone put me out of my misery?
#!/usr/bin/perl -w use strict; my $date = system("date +%x_%T"); open (FILE, ">>output.txt") or die "Can't open output.txt\n"; print FILE "$date Script worked correctly\n"; close (FILE);
Thanks,
Dru
Another satisfied monk.

Replies are listed 'Best First'.
Re: Getting The Date Command to Redirect to File
by japhy (Canon) on Nov 22, 2002 at 16:29 UTC
    That's because system() doesn't return the program's output; it merely executes a program (and what that program does with its output is its own thing).

    You'd want backticks (``) or qx(): chomp(my $date = qx(date +%x_%T)); but there's no need to call the 'date' program when Perl has things to do it for you. See POSIX for the strftime() function:

    use POSIX 'strftime'; my $date = strftime "%x_%T", localtime;

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Getting The Date Command to Redirect to File
by fglock (Vicar) on Nov 22, 2002 at 16:26 UTC

    use backticks:

    my $date = `date +%x_%T`; print " date is " . `date +%x` . " until midnight\n"; date is 11/22/02 until midnight
Re: Getting The Date Command to Redirect to File
by vek (Prior) on Nov 22, 2002 at 16:30 UTC
    system just returns success or failure, you cannot capture the output of the command you are running. Try backticks:
    my $date = `date +%x_%T`;
    FWIW - you don't have to have the shell involved to do what you are attempting, there are many date functions/modules.

    -- vek --
Re: Getting The Date Command to Redirect to File
by dru145 (Friar) on Nov 22, 2002 at 16:52 UTC
    thanks everyone. Can someone demote me to level 2 :-)

    Thanks,
    Dru
    Another satisfied monk.