program : testblocks.pl
#!/usr/bin/perl
use strict;
use warnings;
print "Hi";
BEGIN {
warn "BEGIN";
}
END {
warn "END";
}
INIT {
warn "INIT";
}
CHECK {
warn "CHECK";
}
UNITCHECK {
warn "UNITCHECK";
}
print "Hello";
Output:
BEGIN at testblocks.pl line 8.
CHECK at testblocks.pl line 17.
INIT at testblocks.pl line 14.
UNITCHECK at testblocks.pl line 20.
END at testblocks.pl line 11.
HiHello
Now if I use, \n in print statements like below
print "Hi\n";
...
print "Hello\n";
Output is:
BEGIN at testblocks.pl line 8.
CHECK at testblocks.pl line 17.
INIT at testblocks.pl line 14.
Hi
UNITCHECK at testblocks.pl line 20.
Hello
END at testblocks.pl line 11.
I am unable to understand the issue here. Any inputs or suggestions appreciated.
It looks like a print buffer issue. When you include a newline in your print, buffers get flushed right away. In your first case, without a newline, they get flushed when they are full, or when your program exits.
As dgw comments, since
warngoes to STDERR and print to STDOUT, they are also not in the same buffer.