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

OK, what is going on here. I've been playing with Net::SMTP::Server, which is kind of fun, problem is, the outfile in the below code gets nothing, nada, blank (it's created but blank). The print to STDOUT works fine though...
#!/usr/bin/perl use strict; use Net::SMTP::Server; use Net::SMTP::Server::Client; open (OUT => ">outfile")|| die "Could not open $!"; print OUT "HELLO"; my $server=new Net::SMTP::Server("localhost","25"); while (my $conn=$server->accept()){ my $client=new Net::SMTP::Server::Client($conn) || die "Unable to handle client connection: $!"; $client->process; my $msg=$client->{MSG}; print OUT "$msg\n"; print $msg; }
sob

Replies are listed 'Best First'.
Re: File Handle wierdness
by the_slycer (Chaplain) on Nov 22, 2000 at 11:40 UTC
    I should also mention, even the "print OUT "HELLO"" does not appear in the file

      The "HELLO" won't be flushed to the file until you write more or close the file handle. You might want to do:

      { my $old= select(OUT); $|= 1; select($old); }
      or
      use IO::Handle; ... OUT->autoflush(1);
      so that you program hanging on accept() doesn't fool you, for example.

              - tye (but my friends call me "Tye")
Re: File Handle wierdness
by geektron (Curate) on Nov 22, 2000 at 11:40 UTC
    you're not opening a filehandle correctly.

    open( OUT, ">outfile" );

    if you're planning to blow away the contents of the file each time you run your script, or

    open( OUT, ">>outfile" );

    if you want to update your code.

    i'm surprised you're getting a filehandle opened at all . . .

    UPDATE: i stand corrected. tye has the right idea - the filehandle is blocking. i had to do similar 'magic' on a project.

    close the filehandle, then re-open it. that'll flush the buffer, and you should see ( at least ) the 'hello' string.

      the_slycer is correct. From perlman:perldata:

      It is often more readable to use the => operator between key/value pairs. The => operator is mostly just a more visually distinctive synonym for a comma, but it also arranges for its left-hand operand to be interpreted as a string--if it's a bareword that would be a legal identifier. This makes it nice for initializing hashes:

      Since OUT is a legal bareword he's OK.

      But, that's what I've done. The => does the same as the comma - unless I'm really missing something. I find it clearer to read that way.