11

I have a strange behaviour with Perl's say function in FCGI mode. Newlines won't be append. Why does this happen?

Sample code:

#!/usr/bin/perl -wT
use strict;
use warnings;
use utf8;
use feature qw( say );
use FCGI ();
use CGI qw( header );
my $cnt = 0;
my $req = FCGI::Request();
while ( $req->Accept() >= 0 ) {
    $cnt++;
    print header( -type => 'text/plain', -charset => 'utf-8' );
    say "Hello, world #$cnt";
    print "\n";
    print "$$\n"
    print 'Test 1234';
}

Expected result (and actual result via console):

Content-Type: text/plain; charset=utf-8

Hello, world. #1

6712
Test 1234

Actual result via Apache/FCGI:

Content-Type: text/plain; charset=utf-8

Hello, world. #3
6709
Test 1234

Some software info...

Debian Wheezy x86_64 Apache/2.2.22-11 mod_fcgid/1:2.3.6-1.1 Perl/5.14.2-12 FCGI.pm/0.75-1+b1

burnersk
  • 3,320
  • 4
  • 33
  • 56

1 Answers1

12

Unfortunately the implementation of the say() function requires the filehandle to support the $\ variable. Basically say is equivalent to writing:

{ local $\ = "\n"; print LIST }

Using FCGI, your STDOUT is replaced by a tied filehandle, which doesn't support $\. This means that say() doesn't work as intended. Whether this is a bug in FCGI or in say() seems to be debatable.

pmakholm
  • 1,488
  • 8
  • 23
  • As for a solution I think it is easiest just to say "Don't do that then". The alternative is to get FCGI fixed. Adding `unshift @_, $\ if defined $\\` after [line 264 of FCGI.PL](https://metacpan.org/source/FLORA/FCGI-0.74/FCGI.PL#L265) might do the trick. But there is a chance that it will break a lot of code depending on FCGI not respecting $\. – pmakholm Sep 10 '12 at 10:16
  • 1
    A simple workaround would be to re-implement `say`: `use subs qw/say/; sub say { print @_, "\n" };` – Zaid Sep 10 '12 at 11:53
  • @IlmariKaronen Correct. Good catch. – pmakholm Sep 10 '12 at 12:56