Difference between revisions of "Perl"

From Leaky
Jump to: navigation, search
(Created page with '== Frameworks == * [http://www.catalystframework.org/calendar/2011/15 Catalyst Advent Calendar] == Modules == DBIx::Class modules * http://search.cpan.org/~frew/DBIx-Class/li…')
 
(Added a tip about buffering)
Line 11: Line 11:
 
* http://search.cpan.org/~ironcamel/Dancer-Plugin-DBIC/lib/Dancer/Plugin/DBIC.pm
 
* http://search.cpan.org/~ironcamel/Dancer-Plugin-DBIC/lib/Dancer/Plugin/DBIC.pm
 
* http://search.cpan.org/~flora/DBIx-Class-PassphraseColumn-0.02/lib/DBIx/Class/PassphraseColumn.pm
 
* http://search.cpan.org/~flora/DBIx-Class-PassphraseColumn-0.02/lib/DBIx/Class/PassphraseColumn.pm
 +
 +
== Tips ==
 +
 +
=== Mixing STDOUT and STDERR output in the right order ===
 +
 +
STDOUT and STDERR are block buffered when there is no terminal, or line buffered when there is a terminal. This has the unfortunate side effect of making output from print/warn mess up when running a script from cron when the same script works perfectly run from the command line.
 +
 +
Redirect STDERR to the same stream as STDOUT (equiv to 2>&1 in bash) - this isn't essential, but makes it easier to pipe the output through tail/mail/etc, or redirect the output to a logfile.
 +
 +
close(STDERR);
 +
open(STDERR, ">&STDOUT");
 +
 +
Make both streams non-buffering
 +
 +
select(STDERR);
 +
$| = 1;
 +
select(STDOUT);
 +
$| = 1;
 +
 +
Alternatively, instead of the select() above, you can use IO::Handle
 +
 +
use IO::Handle;
 +
STDERR->autoflush(1);
 +
STDOUT->autoflush(1);

Revision as of 10:17, 1 April 2015

Frameworks

Modules

DBIx::Class modules

Tips

Mixing STDOUT and STDERR output in the right order

STDOUT and STDERR are block buffered when there is no terminal, or line buffered when there is a terminal. This has the unfortunate side effect of making output from print/warn mess up when running a script from cron when the same script works perfectly run from the command line.

Redirect STDERR to the same stream as STDOUT (equiv to 2>&1 in bash) - this isn't essential, but makes it easier to pipe the output through tail/mail/etc, or redirect the output to a logfile.

close(STDERR);
open(STDERR, ">&STDOUT");

Make both streams non-buffering

select(STDERR);
$| = 1;
select(STDOUT);
$| = 1;

Alternatively, instead of the select() above, you can use IO::Handle

use IO::Handle;
STDERR->autoflush(1);
STDOUT->autoflush(1);