{"id":315,"date":"2023-05-12T11:33:47","date_gmt":"2023-05-12T07:33:47","guid":{"rendered":"https:\/\/kidds.co.za\/?p=315"},"modified":"2023-05-12T11:34:32","modified_gmt":"2023-05-12T07:34:32","slug":"perl-get-browser-to-render-html-instantly-while-script-runs","status":"publish","type":"post","link":"https:\/\/kidds.co.za\/index.php\/2023\/05\/12\/perl-get-browser-to-render-html-instantly-while-script-runs\/","title":{"rendered":"Perl get browser to render HTML instantly while script runs"},"content":{"rendered":"\n<p>Simply copy this code into the top of your Perl script and enjoy. All print statements will go straight to users browser and render instantly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><sub>$|++;\nprint \"Content-type: text\/html\\r\\n\";\nprint \"Content-Encoding: plain\\r\\n\\r\\n\";\n&amp;load_buffer; \nsub load_buffer {\n        my $cmd = qq{perl -e \"\\$|++; print ' ' and sleep(1) for 1..1\"};\n        open my $PROC, '-|', $cmd or die $!;\n        while (sysread $PROC, $buffer, 1) { print \" \", ' ' x 1000, \"\\n\";}\n}<\/sub>\n\n<\/code><\/pre>\n\n\n\n<p><strong>The explanation<\/strong><\/p>\n\n\n\n<p>$|++; #This is needed to disable buffering<br>print &#8220;Content-type: text\/html\\r\\n&#8221;;<br># This header is a quick hack to deny browser to gzip\/deflate your output<br>print &#8220;Content-Encoding: plain\\r\\n\\r\\n&#8221;;<br>&amp;load_buffer; #keeps the browser window printing progress while script runs<\/p>\n\n\n\n<p>sub load_buffer {<br>my $cmd = qq{perl -e &#8220;\\$|++; print &#8216; &#8216; and sleep(1) for 1..1&#8221;};<br>open my $PROC, &#8216;-|&#8217;, $cmd or die $!;<br>while (sysread $PROC, $buffer, 1) { print &#8221; &#8220;, &#8216; &#8216; x 1000, &#8220;\\n&#8221;;}<\/p>\n\n\n\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Simply copy this code into the top of your Perl script and enjoy. All print statements will go straight to users browser and render instantly. The explanation $|++; #This is needed to disable bufferingprint &#8220;Content-type: text\/html\\r\\n&#8221;;# This header is a quick hack to deny browser to gzip\/deflate your outputprint &#8220;Content-Encoding: plain\\r\\n\\r\\n&#8221;;&amp;load_buffer; #keeps the browser window [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-315","post","type-post","status-publish","format-standard","hentry","category-perl"],"_links":{"self":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=315"}],"version-history":[{"count":2,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/315\/revisions"}],"predecessor-version":[{"id":317,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/315\/revisions\/317"}],"wp:attachment":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}