Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Question about this simple CGI

by Chaotic Jarod (Novice)
on Jul 17, 2013 at 19:51 UTC ( #1044879=perlquestion: print w/replies, xml ) Need Help??
Chaotic Jarod has asked for the wisdom of the Perl Monks concerning the following question:

I have an HTML page that interacts with this CGI script, however, whenever I run the cgi script it says there is a syntax error on the end_html line, Can anyone help me? This is for a term project.
#!/usr/bin/perl -w use CGI qw(standard); use DBI(); use DBD::mysql; use warnings; use strict; use diagnostics; my $q = CGI->new; my $name = $q->param('name'); my $day = $q->param('day'); my $number = $q->param('number'); print $q->header; $q->start_html( -title => "Add user", -bgcolor => #ffffff); $q->p("Data submitted"); $q->end_html; print $name; print "\n$day\n"; print $number;

Replies are listed 'Best First'.
Re: Question about this simple CGI
by toolic (Bishop) on Jul 17, 2013 at 19:55 UTC
    #ffffff is not correct syntax. Maybe change:
    $q->start_html( -title => "Add user", -bgcolor => #ffffff);


    $q->start_html( -title => "Add user", -bgcolor => 0xffffff);
      That fixed that, not it's telling me that $day and $number are both unitialized, any clue why?
        it's telling me that $day and $number are both unitialized, any clue why?

        They are not being sent from your html page probably. You can assign defaults like this;

        my $name = $q->param('name') || 'noname'; my $day = $q->param('day') || 'noday'; my $number = $q->param('number') || 'nonumber';

        The elements after the print line should be comma separated and you shouldn't be printing after the end_html

        print $q->header, $q->start_html( -title => "Add user", -bgcolor => "#ffffff"), $q->p("Data submitted"), $q->tt("$name<br/>$day<br/>$number"), $q->end_html;

        And you may wish to add this while developing to see any errors in the browser.

        use CGI::Carp 'fatalsToBrowser';
Re: Question about this simple CGI
by Old_Gray_Bear (Bishop) on Jul 17, 2013 at 19:56 UTC
    I rather suspect that -bgcolor => #ffffff should be -bgcolor => '#ffffff' (the value in quotes).

    Update: toolic was faster on the draw.

    I Go Back to Sleep, Now.


Re: Question about this simple CGI
by MidLifeXis (Monsignor) on Jul 17, 2013 at 20:32 UTC

    You are also not printing the start_html, p, or end_html data. s/;/,/.


Re: Question about this simple CGI
by Perlbotics (Chancellor) on Jul 17, 2013 at 20:41 UTC

    Other monks already gave you good advice (quote, initialise, print). Seeing, use DBI;, use DBD::mysql;, and the early stage of an Add user CGI script, I feel obliged to drop the obligatory link ;-)
    A term project should provide a good opportunity to train these skills right from the start.

    Looking into Dancer or Mojolicious might also be worthwhile?

Re: Question about this simple CGI
by CountZero (Bishop) on Jul 18, 2013 at 13:54 UTC
    As this is a term project, you should be very careful in what you write.

    So ask yourself (before the professor asks you):

    • Why do I use the -w flag in the hashbang line AND use warnings;
    • Why do I add parentheses in use DBI();


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      I use parens in use calls to explicitly show that nothing is getting imported from the module.


      It means your professor is either overly tricky or should think his/her questions through a bit more. In the case of DBI the parens do nothing except take up space and maybe mislead someone.

      ~>ack \@EXPORT @EXPORT = (); # we export nothing by default @EXPORT_OK = qw(%DBI %DBI_methods hash); # also populated by export_ok +_tags:

      Update: DERP, didn't see the whole thread. CountZero is asking a good question. The answer to which is... left as an exercise for the reader you.

Re: Question about this simple CGI
by kcott (Chancellor) on Jul 20, 2013 at 08:54 UTC

    G'day Chaotic Jarod,

    "... it says there is a syntax error on the end_html line,"

    You appear to have resolved the issue; however, you may still be wondering why the error was reported as being on the syntactically correct "$q->end_html;" line.

    Take a look at perldiag and search for:

    • syntax error

      (F) Probably means you had a syntax error. Common reasons include:

    This will explain why the error is often reported after the point where the actual error occurred.

    In this case, Perl ignores the comment you wrote (i.e. '#ffffff);') — as it obviously has no idea you didn't mean that to be a comment — and continues parsing your code until it reaches "$q->end_html;" where it gives up having found no valid end to '$q->start_html(...'.

    While you're learning Perl, you might consider using the diagnostics pragma which will provide verbose output regarding problems you encounter. Be aware that this is a debugging aid and should not be left in production code (and that would include what you submit for your project).

    Finally, if you're unable to resolve the error yourself, feel free to ask but do include the full error message when you post your problem.

    -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1044879]
Approved by toolic
Front-paged by sparkyichi
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-12-16 19:49 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (458 votes). Check out past polls.