Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PERL Flushing

Posted on 2003-03-04
4
Medium Priority
?
769 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
3 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 85

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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
Suggested Courses

580 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