Perl Printing Buffering

I have the following Perl code:

STDOUT->autoflush(1);

foreach(...)
{
    ...

    foreach(...)
    {
        print("Processing $folder");
        $|=1;
        process($folder);
    }
    ...
}

but the print statement only works in the first iteration of the loop and after that prints nothing. Any idea why?

EDIT: I found the reason and added it in response. The solution was:

I added the following line inside and it worked:

select STDOUT;

I think the code in process () function should have changed the default output buffer. It was written by someone else!

I'm not sure if this is a problem with Perl, which allows it, or a developer who has not changed it by default.

The last code looked like this:

foreach(...)
{
    ...

    foreach(...)
    {
        select STDOUT;

        print("Processing $folder");
        $|=1;
        process($folder);
    }
    ...
}

Thanks everyone ...

+3
source share
3 answers

Good detective work to keep track of this problem!

.

, select() process(), IO:: Handle STDOUT:

use IO::Handle;

foreach(...)
{
    ...

    foreach(...)
    {
        STDOUT->printflush("Processing $folder");

        process($folder);
    }
    ...
}
+10

:

select STDOUT;

, process() . , - !

, Perl, , .

:

foreach(...)
{
    ...

    foreach(...)
    {
        select STDOUT;

        print("Processing $folder");
        $|=1;
        process($folder);
    }
    ...
}

...

+4

My code is as follows:

#!/usr/bin/perl -w

use strict;
use warnings;
use sigtrap qw/handler signal_handler normal-signals/;
use feature qw(say);

my $datetime;
$datetime = localtime ();

say "tester started $datetime";

while ( 1 ) {
    select STDOUT;
    $datetime = localtime ();
    say "tester output every second $datetime";
    $|=1;
    sleep ( 1 );
}

sub signal_handler {
    die "\nCaught a signal $!\n";
}
0
source

Source: https://habr.com/ru/post/1714206/


All Articles