15

I am receiving the following server error on a perl script:

malformed header from script. Bad header=: youtube_perl.pl,

Here is my source code:

#!"C:\XAMPP\perl\bin\perl.exe" -T

use strict;
use warnings;

use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use WWW::Mechanize;

my $q = CGI->new;

my $url = 'http://www.youtube.com';

my $mechanize = WWW::Mechanize->new(autocheck => 1);

$mechanize->get($url);

my $page = $mechanize->content();

print $page;
cigien
  • 57,834
  • 11
  • 73
  • 112
nicktendo
  • 681
  • 2
  • 11
  • 25

2 Answers2

29

Figured it out. Had to add the following before I attempted to print the page:

print "Content-type: text/html\n\n";

I guess perl can not print html pages without defining the header first.

Sotiris
  • 38,986
  • 11
  • 53
  • 85
nicktendo
  • 681
  • 2
  • 11
  • 25
  • 3
    @user589294 - No. Perl can print them. But they won't be valid HTTP responses. So if you want your HTML page printed by Perl to be transported over HTTP you need to actually print a full HTTP response - got nothing to do with Perl – DVK Jan 25 '11 at 18:13
  • 7
    Of course Perl can print HTML pages without a header! You can't write a CGI script without printing a header, though. Better is: `print $q->header();`. See [perldoc CGI](http://perldoc.perl.org/CGI.html). – mscha Jan 25 '11 at 18:13
  • 1
    You should use `\x0D\x0A` rather than `\n` as the EOL when speaking HTTP. HTTP specifies that lines should be terminated with CR-LF whereas `\n` is the end of line marker for your current environment, `\n` just happens to be `\x0D\x0A` for you because you're running on Windows. – mu is too short Jan 25 '11 at 21:44
  • Thank you! This worked! What is strange is our old scripts were working without this, and one day stopped work. Could have been a server update. Not sure. Any ideas? – Xonatron Jan 12 '19 at 20:03
  • in bash/shell \n signs are in echo not interpreted but echo adds \n automatically, so it is needed one more echo command for the output – FantomX1 Feb 18 '21 at 09:57
3
print "Content-type: text/html\n\n";

Use \n\n without this it will not print anything it will give:

Malformed header from script error

In your error log file.

Ethan Field
  • 4,646
  • 3
  • 22
  • 43
sahil
  • 31
  • 1