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

Okay, I have some code where essentially I'm saying hello to a client and giving the current time. But about 20% of the time instead of printing $date it doesn't print anything. I put the following code in and I can't believe the results.
print "Forked child printing CMDSTART to client\n"; $date=`date`; print "\$date $date"; &S_print ("[$client_ipnum] $client_host $date connection attem +pt logged\n"); print "\$date $date"; &S_print ("This is a private server. All connections are logge +d\n"); &S_print ("username:\n"); print "sent username:\n";

S_print prints to the socket and print is just going to stdout. Here is the clip from stdout:
Forked child printing CMDSTART to client
$date $date sent username:
Not only does it look like it printed $date instead of it's value then it didn't print the next one and skipped ahead to print sent username. Then on the next run it worked okay:
Forked child printing CMDSTART to client
$date Thu Mar 13 00:25:32 CST 2003
$date Thu Mar 13 00:25:32 CST 2003
sent username:
un received: usernamex
pw received: start
Accepted connection
Am I going crazy? Can anyone make any sense out of this?

perl -v
This is perl, v5.8.0 built for i386-linux-thread-multi

Replies are listed 'Best First'.
Re: $date in print is printing \$date ???
by crenz (Priest) on Mar 13, 2003 at 08:00 UTC

    Not only does it look like it printed $date instead of it's value

    Not quite. You have two statements that read

    print "\$date $date";

    In those cases where $date contains the output from date (including trailing newline!), the output will look like

    $date Thu ...
    $date Thu ...

    When date fails, $date is empty and the output of the two prints is glued together:

    $date $date

    As to why date fails I don't know, and since you are not checking the return codes of the system call, you don't know also ;-).

    I suggest that in this case, don't use date at all. Check out localtime, it should do what you want

    Update: If you're printing the output to a log file, use gmtime instead. Example:

    print scalar localtime;
    print scalar gmtime;

      You're exactly right. I misread the two $dates. So now the only mistery is why the `date` isn't returning anything.
Re: $date in print is printing \$date ???
by drake50 (Pilgrim) on Mar 13, 2003 at 07:43 UTC
    By changing the $date=`date` line to $date = localtime() the code now seems to work 100%. Whereas before it worked somewhere around 75%.
    There is something really strange out this...
      Not really. Perl doesn't know anything about date.
      delete $ENV{path}; die `date`;
      date is an external program, and as such, depends on the ENVironment. It is a very bad idea to do things like `date`. This ain't shell programming, this is perl, and perl has gmtime() and localtime() which in scalar context return a neato time string, as in
      perl -le die~~gmtime perl -le die~~localtime
      Now that's called portability.


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
      ** The Third rule of perl club is a statement of fact: pod is sexy.