?
Solved

PERL Flushing

Posted on 2003-03-04
4
Medium Priority
?
768 Views
Last Modified: 2012-08-14
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

0
Comment
Question by:vaultworld
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Expert Comment

by:periwinkle
ID: 8068377
for example 1, I believe that you need to include \n in the string in order for it to be seen.
0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 8069913
## Open a file and write to it.
open (FILEHANDLE, ">third.txt") or die ("Cannot open first.txt");

select((select(FILEHANDLE), $| = 1)[0]);

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);

0
 
LVL 20

Expert Comment

by:jmcg
ID: 9692784
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question