Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Opens but Won't Write to file

by saintjames (Novice)
on Oct 30, 2009 at 20:52 UTC ( [id://804215]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to set up a UDP server which listens to port and writes data received to file. The program opens the file however it won't write to it? I know that it is receiving data because for debugging I have an additional print statement that prints the data received to the terminal. Code follows:

#!/usr/local/bin/perl -w use strict; $|=1; use IO::Socket::INET; if (scalar @ARGV < 2) { die usage(); } my($filenam,$portnum,$OUTDIR,$OUTFIL,$outfil,$begin,$MySocket,$text); my($YEAR,$MO,$DAY,$HOUR,$MIN,$MONTH,$yr,$timeofday,$damoyr); $filenam = $ARGV[0]; $portnum = $ARGV[1]; my(@NOW) = localtime(); ($YEAR,$MO,$DAY,$HOUR,$MIN) = @NOW[5,4,3,2,1]; $MONTH = $MO + 1; $YEAR = $YEAR + 1900; $yr = $YEAR - 2000; $damoyr = sprintf("%02d/%02d/%02d",$MONTH,$DAY,$yr); $timeofday = sprintf("%02d:%02d",$HOUR,$MIN); $OUTDIR = "/active/"; $outfil = $OUTDIR . $filenam; print "outfile is $outfil\n"; open(OUTFIL,">$outfil"); # Create a new socket $MySocket=new IO::Socket::INET->new(LocalPort=>$portnum, Proto=>'udp'); $begin="\nStarting UDP Receiver $damoyr $timeofday\n"; print OUTFIL "$begin"; # Keep receiving messages; while(1) { $MySocket->recv($text,128); print OUTFIL "$text#$damoyr $timeofday\n"; print "$text#$damoyr $timeofday\n"; } close(OUTFIL); sub usage { my $usage = <<EOF; Usage: $0 <filename> <portnum> - filename is name of log file in the logging directory - portnum is the UDP port number to listen to EOF return $usage; }

I see the output file newly opened each time I run this and I use a simple udpclient to send to the same port that this listens to and can see the output printed to the terminal but never the file. Any wisdom would be graciously accepted, thanks

Replies are listed 'Best First'.
Re: Opens but Won't Write to file
by Joost (Canon) on Oct 30, 2009 at 20:58 UTC
    open() and print() return a true value when they succeed. Either your open call isn't succeeding or your prints arent working (maybe you're running out of disk space?)

    In either case, you should ALWAYS check that open() succeeds, and it's probably a good idea to check print() too:

    open FH,">","whatever" or die "Can't open whatever: $!";
    is the standard procedure.

      I commend your suggestions, wrt the testing of the success (or otherwise) of calls, to the house.

      I would, however, debate your suggestion that ...your open call isn't succeeding... since the OP 'sees' a new file on each run - ergo, it strikes me that, as other, for more learned monks, have already pointed out elsewhere on this thread, the OP is suffering from buffering.

      A user level that continues to overstate my experience :-))
Re: Opens but Won't Write to file
by MidLifeXis (Monsignor) on Oct 30, 2009 at 21:30 UTC

    • Setting autoflush ($|=1) is not being done on OUTFIL. See select.
    • close(OUTFIL) will never be reached due to the while (1) {...}.
    With those items, it is possible that your output buffer is just not full, so the data is never flushed to disk. Add that to Joost's exhortations, and see what you come up with.

    --MidLifeXis

      Ignore select. The simplest way to turn off buffering on a handle is
      use IO::Handle qw( ); HANDLE->autoflush(1);

        Simplest, but costly. From the docs:

        use IO::Handle; # thousands of lines just for autoflush :-(

        I think that comment originates from someone rather well respected within the perl community.

        That could be avoided by a simple (untested):

        sub autoflush{ my $old = select $_[ 0 ]; $|=1; select $old; } ... autoflush $fh;

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        Aaah, yes. Old habits (this is a monastery, after all) die hard. I guess I need to update that habit.

        --MidLifeXis

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found