Link to home
Start Free TrialLog in
Avatar of vaultworld
vaultworld

asked on

PERL Flushing

I'm creating an example for intro to programming.  When I run the example I don't get what I expect with flushing turned on.

First example:
#!/usr/bin/perl
$| = 1;
## Open a file and write to it.
open (FILEHANDLE, ">third.txt") or die ("Cannot open first.txt");
print FILEHANDLE "Hey what are you looking at";
# A Stalling technique, look at the file now and see what's in it
print qq{Look at the contents of third.txt then hit Enter\n};
$prompt = <STDIN>;
close (FILEHANDLE);
# A Stalling technique, look at the file now and see what's in it
print qq{Look at the contents of third.txt then hit Enter\n};
$prompt = <STDIN>;

When I look at the file after printing "Hey what are you looking at" the file is still empty.  I would think with flushing on it would have been force to write it.

Second Example:
Now when I rewrite it using autoflush it works as I would think.

Where right after printing its in the file.
#!/usr/bin/perl
use IO::File;
## Open a file and write to it.
open (FILEHANDLE, ">third.txt") or die ("Cannot open first.txt");
autoflush FILEHANDLE 1;
print FILEHANDLE "Hey what are you looking at";
# A Stalling technique, look at the file now and see what's in it
print qq{Look at the contents of third.txt then hit Enter\n};
$prompt = <STDIN>;
close (FILEHANDLE);
# A Stalling technique, look at the file now and see what's in it
print qq{Look at the contents of third.txt then hit Enter\n};
$prompt = <STDIN>;

Why doesn't $| = 1 work like autoflush FILEHANDLE 1

Avatar of periwinkle
periwinkle
Flag of United States of America image

for example 1, I believe that you need to include \n in the string in order for it to be seen.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nothing has happened on this question in over 7 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by ozo.

Please post any comments here within the next seven days. Moderators check comments here before acting on the recommendation. Experts: silence will likely be taken as assent.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer