4

I've found a variety of information regarding autoflushing in Perl, but can't find any mention of a line formatted exactly like this:

STDOUT->autoflush(1);

This is included in a program I'm analyzing, and I want to understand better what it does. There is also this at the beginning of the program:

use IO::Handle;

...which I think might be related, but it also doesn't mention this method in its documentation.

Stephen
  • 8,508
  • 12
  • 56
  • 96

1 Answers1

7

The method is listed in IO::Handle which you noticed

...
$io->autoflush ( [BOOL] )                         $|
...

whereby $| is set/unset, and it sends you to $| in perlvar for explanation

If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel.

As of v5.14 you don't have to use IO::Handle for this as IO::File is required when needed.

This method is special in that it turns autoflush on even without value supplied,

$io->autoflush;  # turned on ($| gets set)

while other related ones don't change the current value when called without argument.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • To clarify, my confusion is regarding how the method becomes available on STDOUT (apparently automatically just by using IO::Handle). The documented case you mention is just on an object $io which is different, it seems to me. – Stephen Jan 24 '18 at 00:02
  • 2
    @Stephen Follow the link "_As of v5.14_", it does explain it. I added a comment to highlight – zdim Jan 24 '18 at 00:04
  • OK thanks, I think I see how everything comes together now. – Stephen Jan 24 '18 at 00:28
  • BTW, what is "block buffering"? It says that when writing to pipes STDOUT will be block buffered. – Stephen Jan 24 '18 at 00:30
  • Nevermind got my answer here: https://stackoverflow.com/questions/22008273/what-do-chunk-block-offset-buffer-and-sector-mean – Stephen Jan 24 '18 at 00:31
  • 2
    @Stephen In a rush here, in short: (1) `STDOUT` (and Co) are `IO::File` objects, see with the oneliner: `perl -wE'say *{$main::{STDOUT}}{IO}'` (2) Perl output is buffered differently depending on where it goes; normally for terminal it's line buffered while for pipes (and files) it goes in whole blocks (I think normally 64kB these days) – zdim Jan 24 '18 at 00:39
  • @Stephen More generally, this works for any filehandle. So `open my $fh,...` and call `IO::File` methods on `$fh` and the module is loaded and it works. All filehandles are `IO::File` in newer Perls -- I think they get `bless`ed in `IO::File` when created but I couldn't find this in documentation. At any rate, the quoted docs specify that they are objects at least once the need arises. – zdim Jan 24 '18 at 09:55
  • @Stephen Methods from `IO::File` all work on _file-related_ filehandles. For others (say, sockets) look for methods in `IO::Handle`. The `IO::File` inherits from `IO::Handle` and from `IO::Seekable` (file related ones). Most methods, by far, come from `IO::Handle` though. – zdim Jan 24 '18 at 10:03
  • Re "*my confusion is regarding how the method becomes available on STDOUT*", File handles are blessed into IO::Handle or IO::File depending on your version of Perl. (`perl -MScalar::Util=blessed -E'say blessed(*STDOUT{IO})'`) – ikegami Jan 24 '18 at 16:09