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

I'm currently trying to write some CGI scripts for a website that i'm developing for work. I'm a little new at this, so everything is a 'suck it and see' approach.

Anyway, I've gotten so far, but am stuck on the following. Its a (rather simple) CGI script called from a html form.

#!/usr/bin/perl use CGI; my $query= new CGI; my $pdb_file = $query->param('pdbcode'); print $query->header(-type => 'text/html'); print $query->start_html(-title => "Secondary Structure Analysis"); MainProgram($pdb_file); $query->end_html( ); ####################################################### # 'main' subroutine sub MainProgram { my($pdb_file) = @_; my $dssp = ".dssp"; my $dsspdir = "http:/mywebsite/datafiles/"; my $pdbcode = substr($pdb_file, 0, 4); my $dssp_file = "$dsspdir$pdbcode$dssp"; print $query->('dssp file used is $dssp_file'); }
When I submit data to the preceding form, this script returns a blank page. And when I try running the script from the command line I get the following error

Not a CODE reference at ./script.cgi line 45.

Could anyone please suggest as to what might be going on? Any help much appreciated.

Replies are listed 'Best First'.
Re: CGI script query
by holli (Abbot) on Jun 08, 2005 at 13:43 UTC
    Is that all your code? Currently it has only 33 lines, so I am a bit puzzled.

    Update:
    The Problem is this line:
    print $query->('dssp file used is $dssp_file');
    You must replace it with a simple print
    print "dssp file used is $dssp_file";
    Note the use of double quotes here. With single quotes your variable will not be interpolated.

    Or use one of the modules output methods.
    print $query->h1("dssp file used is $dssp_file");


    holli, /regexed monk/
Re: CGI script query
by tlm (Prior) on Jun 08, 2005 at 13:44 UTC

    perl is complaining because the syntax $q->(...) treats $q as a code ref, when it isn't. I'm not sure what you wanted to do with the $q->(...) expression, but I think that what you want is simply:

    print "dssp file used is $dssp_file<br>";
    Note the double quotes, so that you get variable interpolation.

    the lowliest monk

Re: CGI script query
by cool_jr256 (Acolyte) on Jun 08, 2005 at 13:48 UTC
    Here is your mistake:
    instaed of
    print $query->("dssp file used is $dssp_file");
    do this
    print $query->body("dssp file used is $dssp_file");
    Oh and btw use "" not ''
Re: CGI script query
by monarch (Priest) on Jun 08, 2005 at 13:43 UTC
    During development it helps to add the following line to your CGI script:
    use CGI::Carp qw( fatalsToBrowser );
    This will, hopefully, cause an error to be sent to your web browser if something went wrong with your script.

    Next, the line in your code:

    print $query->('dssp file used is $dssp_file');
    makes me wonder if you didn't mean something like this:
    print $query->p('dssp file used is $dssp_file');
    .. at least ->p() is a function implemented in CGI (it wraps <p>...</p> tags around your text).. I don't know what the behaviour of the ->() call would be from your CGI object $query.

    Finally, as holli mentions, you appear to be missing lines of code, not least because I couldn't count 45 lines there to find your error.. or at least look at the 2 or 3 preceeding lines that may have caused the error.

    PS: did you add use strict; to your script..

    Considered (holli): force insert "Update notification". author changed original content and did not react to a /msg
    Unconsidered (holli): Enough keep votes (Keep/Edit/Delete: 6/12/0)

      To the OP, note well that monarch wrote "during development." I.e., disable fatalsToBrowser in your production code, because it is a "huge security hole".

      the lowliest monk

        Oh right, thanks very much. I appreciate everyones help. Got it sorted now.