jawright has asked for the wisdom of the Perl Monks concerning the following question:
i'm sure there's more than a few glaring errors, as it's been some time since i tried my hand at perl..but, the biggest problem so far is that STDOUT isn't acting as if it is flushed. when i run this, i see nothing until i start to enter data. once i hit the first carriage return, i see both prompts. I type the rest of the data, hit ^D, and see no other output...even though the files get updated. I know it's a newbie question, and I've R'd the FM, but i still don't see it.#!/usr/bin/perl -p use strict; use Net::FTP; use POSIX qw(strftime); my $filedir='./'; my $localfile='index.html'; my $bkup='index.html.tmp'; my $ftpserver='127.0.0.1'; my $ftpdir=""; my $user='scott'; my $password='tiger'; my $insertpoint='<!--insert here-->'; my $dateformat="%A, %d %B %Y, %H:%M:%S"; my $datestring=strftime($dateformat,localtime); my $headline=''; my $post=''; my $ftpfl=0; chdir($filedir) or die "can't chdir:$!\n"; my $old_fh=select(STDOUT); $| = 1; select($old_fh); print "\nheadline:\n"; do{ if ($headline eq ''){ $headline=$_; print "post:\n"; }else{ $post.=$_; } }while(<STDIN>); print "headline: $headline\npost: $post\n"; open(ORIG,"< $localfile") or die "can't open $localfile:$!"; open(BKUP,"> $bkup") or die "can't open $bkup:$!"; select(BKUP); while(<ORIG>){ print BKUP $_ or die "can't write $bkup:$!"; if ($_ =~ m/$insertpoint/ && $post =~ /^[A-Za-z]+$/){ print BKUP "<div class=box><strong>$datestring $headline</stro +ng>\n"; print BKUP "<p class=post> $post</p></div>\n" or die "can't wr +ite to $bkup:$!"; $ftpfl=1; } } close(ORIG) or die "can't close $localfile:$!"; close(BKUP) or die "can't close $bkup:$!"; if ($ftpfl){ print "renaming...\n"; rename($localfile, "$localfile.orig") or die "can't rename $localf +ile to $localfile.orig:$!"; rename($bkup,$localfile) or die "can't rename $bkup to $localfile: +$!"; print "transferring...\n"; my $ftp=Net::FTP->new($ftpserver) or die "Can't connect:$@\n"; $ftp->login($user, $password) or die "can't login\n"; $ftp->cwd($ftpdir) or die "can't cwd to $ftpdir\n"; $ftp->put($localfile) or die "can't put $localfile\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: can not flush stdout
by derby (Abbot) on Jul 10, 2004 at 10:45 UTC | |
|
Re: can not flush stdout
by muba (Priest) on Jul 10, 2004 at 21:24 UTC | |
by Anonymous Monk on Jul 11, 2004 at 07:49 UTC |