esharris has asked for the wisdom of the Perl Monks concerning the following question:

In the following code, what is wrong with the 2nd application of CGI::header?

#!/usr/bin/perl -w use strict; use CGI qw/:standard/; my @a; my $i = 0; eval { print "\nTake ", $i++, ",\n", header, "\n"; print "\nTake ", $i++, ",\n", header @a, "\n"; print "\nTake ", $i++, ",\n", header (@a), "\n"; print "\nTake ", $i++, ",\n", header (), "\n"; }; if ($@) { print "I died: $@\n"; }

I thought each header call would produce the same output. However, the 2nd call produces an empty Content-Type string.

Replies are listed 'Best First'.
Re: passing parameters to CGI::header
by Aristotle (Chancellor) on Aug 13, 2004 at 12:41 UTC

    Deparse to the rescue:

    BEGIN { $^W = 1; } use CGI (':standard'); use strict 'refs'; my @a; my $i = 0; eval { do { print "\nTake ", $i++, ",\n", header(), "\n"; print "\nTake ", $i++, ",\n", header(@a, "\n"); print "\nTake ", $i++, ",\n", header(@a), "\n"; print "\nTake ", $i++, ",\n", header(), "\n" } }; if ($@) { print "I died: $@\n"; }

    On the third and fourth lines, the parens disambiguate the syntax. On the first line, the immediately following comma is taken to mean that you're not passing any parameters. On the third line, there is nothing that tells the compiler that you mean header(@a), "\n" — it looks as though you're saying header(@a, "\n").

    The fact that this is happening with &CGI::header is a red herring, btw. Perl always interprets such cases in this fashion. The same would happen regardless of which function you use (except for those with restrictive prototypes).

    Makeshifts last the longest.