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

I'm trying to insert an HTML comment in the head tag of a doc generated by CGI.pm, but I'm getting and error and I can't see what I'm doing wrong.
#!/usr/local/perl-5.8.7/bin/perl -T use warnings; use strict; use CGI; use CGI::Carp qw/fatalsToBrowser warningsToBrowser/; my $query = new CGI; print $query->start_html( -head=>comment('commentary in the <head>'), ); print $query->end_html;
gives
<h1>Software error:</h1> <pre>Undefined subroutine &amp;main::comment called at /usr/local/apac +he-1.3.33/cgi-bin/tryCommentInHead.cgi line 7. </pre> <p> For help, please send mail to this site's webmaster, giving this error + message and the time and date of the error. </p> [Mon Aug 14 15:00:08 2006] tryCommentInHead.cgi: Undefined subroutine +&main::comment called at /usr/local/apache-1.3.33/cgi-bin/tryCommentI +nHead.cgi line 7.

Replies are listed 'Best First'.
Re: CGI.pm: HTML comment in <head>
by friedo (Prior) on Aug 14, 2006 at 20:12 UTC
    The error message you're getting is telling you that the subroutine &main::comment is undefined, which is true, because you're not importing the function from CGI.pm. To do that, you need to do:

    use CGI qw/:standard/;

    But it looks like you're planning to use the OO style instead, so instead of importing, you can just use $query->comment() instead of comment().

      Right, of course. End day now.
Re: CGI.pm: HTML comment in <head>
by Fletch (Bishop) on Aug 14, 2006 at 20:12 UTC

    As the error message says, you're trying to call an undefined subroutine. Since you didn't import comment from CGI explicitly (or via a tag like :standard) you need to call it as a method on your $query instance instead: $query->comment( "OMGWTFBBQ?" )

    Update: Gah, the tag's :standard not :common.