Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP

When you go to board a plane at Heathrow, do the ground staff admit the passengers one at a time, ensuring that each is seated before the next boards, and sealing and re-opening the main doors between each? What a stupid and inefficient system that would be!!

The same thing applies when you're programming - outputs that you THINK that you're printing directly to screen, to file, to pipe, or (via a socket) to a process on another machine may in fact be waiting in the lounge - buffered - to be output with greater efficiency in a single block.

Should you wish to read from a newly written resource, then, you might have to wait. In the majority of cases it would not be a problem. Language libraries are usually built with a number of checkpoints at which data is "flushed" even if buffers aren't full. Such flushes commonly occur:
a) When a new line is generated (keep screen up to date)
b) When an input is about to be read (so that prompts can be seen!)
c) When the buffer reaches 4k of data
d) When the file handle is closed
e) When the program exits
and these account for most, but not all, of the cases the programmer needs to be aware of.

"But not all" I wrote. Here are some common exceptions:

In Tcl, the fconfigure command gives you channel by channel control. By default, flushing happens line by line (and so NOT when you're about to do a read) and this means you'll want to flush stdout after outputting a prompt with puts -nonewline. Otherwise, the prompt is only issued on the screen AFTER you've answered the question. [example]

In Perl, output is similarly flushed at new line, and in addition it is flushed at read. Good - no need for Tcl's flush. However, if you're generating a progress bar - either a series of dots or a line that's being overwritten time and again, then you'll need to flush. Have a look at autoflush and have a look at the special variable $| (that's dollar-pipe). Neither of these flushes itself - rather they add an extra flush at the end of each print. See
[/example].

Python has a similar arrangement to Perl, so it's quite rare that you need to flush the screen output - but again you DO need to do so with a progress bar. [example].

I describe PHP as being a "batch" or "batchlet" language - at least in its intended web use. So it doesn't have the interactive buffering issues associated with keyboard-to-screen in most uses. Actually, you have rather the opposite problem sometimes. In PHP, if you try to call the header function, or anything else that could effect the headers (even session_start can do it) after you have generated some output, you'll get an error message like Warning: Cannot modify header information - headers already sent by .... There *is* an ob_start function to turn ON output buffering but, really, best programming practise is to generate your headers early!

There are PHP examples broken here and working here. They include links to source.
(written 2007-04-06, updated 2009-08-31)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y115 - Additional Python Facilities
  [183] The elegance of Python - (2005-01-19)
  [208] Examples - Gadfly, NI Number, and Tcl to C interface - (2005-02-10)
  [239] What and why for the epoch - (2005-03-08)
  [463] Splitting the difference - (2005-10-13)
  [663] Python to MySQL - (2006-03-31)
  [672] Keeping your regular expressions simple - (2006-04-05)
  [753] Python 3000 - the next generation - (2006-06-09)
  [901] Python - listing out the contents of all variables - (2006-10-21)
  [1043] Sending an email from Python - (2007-01-18)
  [1149] Turning objects into something you can store - Pickling (Python) - (2007-04-15)
  [1305] Regular expressions made easy - building from components - (2007-08-16)
  [1336] Ignore case in Regular Expression - (2007-09-08)
  [1337] A series of tyre damages - (2007-09-08)
  [1876] Python Regular Expressions - (2008-11-08)
  [2407] Testing code in Python - doctest, unittest and others - (2009-09-16)
  [2435] Serialization - storing and reloading objects - (2009-10-04)
  [2462] Python - how it saves on compile time - (2009-10-20)
  [2655] Python - what is going on around me? - (2010-02-28)
  [2721] Regular Expressions in Python - (2010-04-14)
  [2745] Connecting Python to sqlite and MySQL databases - (2010-04-28)
  [2746] Model - View - Controller demo, Sqlite - Python 3 - Qt4 - (2010-04-29)
  [2764] Python decorators - your own, staticmethod and classmethod - (2010-05-14)
  [2765] Running operating system commands from your Python program - (2010-05-14)
  [2786] Factory methods and SqLite in use in a Python teaching example - (2010-05-29)
  [2790] Joining a MySQL table from within a Python program - (2010-06-02)
  [3089] Python regular expressions - repeating, splitting, lookahead and lookbehind - (2010-12-17)
  [3442] A demonstration of how many Python facilities work together - (2011-09-16)
  [3469] Teaching dilemma - old tricks and techniques, or recent enhancements? - (2011-10-08)
  [4085] JSON from Python - first principles, easy example - (2013-05-13)
  [4211] Handling JSON in Python (and a csv, marshall and pickle comparison) - (2013-11-16)
  [4298] Python - an interesting application - (2014-09-18)
  [4439] Json is the new marshall, pickle and cPickle / Python - (2015-02-22)
  [4451] Running an operating system command from your Python program - the new way with the subprocess module - (2015-03-06)
  [4536] Json load from URL, recursive display, Python 3.4 - (2015-10-14)
  [4593] Command line parameter handling in Python via the argparse module - (2015-12-08)
  [4709] Some gems from Intermediate Python - (2016-10-30)

T202 - Tcl/Tk - Tcl Fundamentals
  [3] Looking for a donkey - (2004-08-05)
  [210] Joining lists in Tcl. Indirect variables in Tcl. - (2005-02-12)
  [328] Making programs easy for any user to start - (2005-05-29)
  [349] Comments in Tcl - (2005-06-16)
  [362] The ireallyreallywanna operator - (2005-06-28)
  [782] Converting between Hex and Decimal in Tcl - (2006-06-28)
  [1282] Stringing together Tcl scripts - (2007-07-29)
  [1426] Buffering up in Tcl - the empty coke can comparison - (2007-11-10)
  [1469] Curley brackets v double quotes - Tcl, Tk, Expect - (2007-12-12)
  [2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [4324] Learning to program - variables and constants - (2014-11-22)
  [4453] Tcl variable names - no real limits! - (2015-03-10)

P210 - Perl - Topicalization and Special Variables
  [493] Running a Perl script within a PHP page - (2005-11-12)
  [639] Progress bars and other dynamic reports - (2006-03-09)
  [969] Perl - $_ and @_ - (2006-12-07)
  [1221] Bathtubs and pecking birds - (2007-06-07)
  [1232] Bathtub example - (2007-06-14)
  [1289] Pure Perl - (2007-08-03)
  [1444] Using English can slow you right down! - (2007-11-25)
  [1508] How not to write Perl? - (2008-01-15)
  [1704] Finding operating system settings in Perl - (2008-07-10)
  [1705] Environment variables in Perl / use Env - (2008-07-11)
  [1728] A short Perl example - (2008-07-30)
  [1829] Dont bother to write a Perl program - (2008-10-10)
  [1860] Seven new intermediate Perl examples - (2008-10-30)
  [1922] Flurinci knows Raby Lae PHP and Jeve - (2008-12-04)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [3449] Apache Internal Dummy Connection - what is it and what should I do with it? - (2011-09-19)
  [4301] Perl - still a very effective language indeed for extracting and reporting - (2014-09-20)
  [4395] Preparing data through a little bit of Perl - (2015-01-15)
  [4682] One line scripts - Awk, Perl and Ruby - (2016-05-20)
  [4700] Obfurscated code - it might work, but is it maintainable? - (2016-07-02)

H110 - PHP - HTML Web Page Data Handling
  [50] Current cost in your local currency - (2004-09-16)
  [589] Robust PHP user inputs - (2006-02-03)
  [789] Hot answers in PHP - (2006-07-02)
  [896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
  [1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)
  [1053] Sorting people by name in PHP - (2007-01-26)
  [1169] Emailing as HTML (Web Page) - PHP example - (2007-04-30)
  [1831] Text formating for HTML, with PHP - (2008-10-11)
  [2025] Injection Attack if register_globals in on - PHP - (2009-02-04)
  [2046] Finding variations on a surname - (2009-02-17)
  [2107] How to tweet automatically from a blog - (2009-03-28)
  [2135] What features does this visitors browser support? (PHP) - (2009-04-22)
  [3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
  [3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)

G997 - Well House Consultants - Newsletter Lead Articles
  [1000] One Thousand Posts and still going strong - (2006-12-18)
  [1065] Graham Ellis - an Introduction - (2007-02-05)
  [1224] Object Relation Mapping (ORM) - (2007-06-09)
  [1318] Well House Manor - feature comparison against the old place! - (2007-08-24)
  [1386] New software product for warmblooded programmers - (2007-10-10)
  [1488] New trainee laptop fleet for our Open Source courses - (2007-12-30)
  [1545] Letting new visitors know we provide training courses - (2008-02-19)
  [1600] Cambidge - Tcl, Expect and Perl courses - (2008-04-04)
  [1663] Python in an afternoon - a lecture for experienced programmers - (2008-06-01)
  [1754] Upgrade from PHP 4 to PHP 5 - the TRY issue - (2008-08-15)
  [1819] Calling base class constructors - (2008-10-03)
  [1912] Book now for 2009 - (2008-11-29)
  [2052] How was my web site compromised? - (2009-02-24)
  [2119] Make your business a DESTINATION business - (2009-04-05)
  [2253] Walks in and around Melksham, Wiltshire - (2009-06-21)
  [2370] C++, Python, and other training - do we use an IDE - (2009-08-21)
  [2425] Weekend and Christmas Promotion - Well House Manor Hotel, Melksham - (2009-09-26)
  [2538] Open Source Training Centre and Courses for 2010 - (2009-12-16)
  [2743] Public Open Source Training Courses running this summer and autumn in Melksham - (2010-04-27)
  [3202] Telling you something about us in just one line - (2011-03-15)


Back to
Government pressure on me to shut up after I make the papers
Previous and next
or
Horse's mouth home
Forward to
Wiltshire Circles
Some other Articles
Python GTK - Widget, Packing, Event and Feedback example
Overcrowded trains around Bristol
The Holiday - unlikely romantic comedy?
Wiltshire Circles
Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP
Government pressure on me to shut up after I make the papers
Function / method parameters with * and ** in Python
B-2-B Networking, 20th April. Useful for YOUR business?
Well House Manor, Melksham, Art Gallery
MySQL - Password security (authentication protocol)
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.info/mouth/1136_Buf ... d-PHP.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb