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

My script when tested shows it's error free, and it runs through the web browser without producing any errors as well. However, it doesn't print my $tags to browser and it doesn't send the email. Does anyone see why?

Thanks so much!

my $adminmail = 'venimfrogtongue@earthlink.net'; my $sendmail = '/usr/lib/sendmail'; use strict; my %form = ( author => undef, distributor => undef, copyright => undef, keywords => undef, description => undef, distribution => undef, robots => undef, language => undef, generator => undef, rating => undef, abstract => undef, usermail => undef, request => undef, ); use CGI; my $query = CGI->new; print $query->header; %form = %{$query->Vars}; open(HEADER, "header.txt") or die("Cannot open file: $!\n"); while (<HEADER>) { print "$_\n"; } close(HEADER); print <<"EOH" <table width="50%" border="0" height="296"> <tr> <td height="58"> <p>Below are your generated metatags!</p> <p>See below for instructions on how to install these within you +r website!</p> </td> </tr> <tr> <td bgcolor="#00CCCC"> EOH ; my $tags =''; if ($form{'abstract'} ne "") { $tags .= qq(<meta name="abstract" content="$form{'abstract'}"><br +>\n); } if ($form{'author'} ne "") { $tags .= qq(<meta name="author" content="$form{'author'}"><br>\n) +; } if ($form{'distributor'} ne "") { $tags .= qq(<meta name="distributor" content="$form{'distributor' +}"><br>\n); } if ($form{'copyright'} ne "") { $tags .= qq(<meta name="copyright" content="$form{'copyright'}">< +br>\n); } if ($form{'keywords'} ne "") { $tags .= qq(<meta name="keywords" content="$form{'keywords'}"><br +>\n); } if ($form{'description'} ne "") { $tags .= qq(<meta name="description" content="$form{'description' +}"><br>\n); } $tags .= qq(<meta name="generator" content="SpyderTagV1.0!"><br>\n); if ($form{'robots'} ne "") { $tags .= qq(<meta name="robots" content="$form{'robots'}"><br>\n) +; } if ($form{'language'} ne "") { $tags .= qq(<meta name="language" content="$form{'language'}"><br +>\n); } if ($form{'distribution'} ne "") { $tags .= qq(<meta name="distribution" content="$form{'distributio +n'}"><br>\n); } if ($form{'rating'} ne "") { $tags .= qq(<meta name="rating" content="$form{'rating'}"><br>\n) +; } print my $cgi->escapeHTML( $tags); print <<"EOF" </td> </tr> <tr> <td> <p>Copy the above tags within your HEAD document.</p> <p><i><font size="2" color="#0000FF"><i><HTML></font><br> <i><font size="2" color="#0000FF"><HEAD></font><br> <i><font size="2" color="#0000FF"><TITLE></TITLE></font><br> <font size="2" color="#FF3300">#---- insert tags here</font><br> <font size="2" color="#0000FF"><HEAD></font></i></p> </td> </tr> EOF ; ################################################# BEGIN EMAIL if ($form{'request'} eq "yes") { open (MAIL, '|-', "$sendmail -t") or die $!; print MAIL "To: $form{'useremail'}\n"; print MAIL "From: $adminmail\n"; print MAIL "Subject: SpyderTags Results:\n\n"; print MAIL "$tags\n"; close (MAIL); } ################################################ END EMAIL open(FOOTER, "footer.txt") or die("Cannot open file: $!\n"); while (<FOOTER>) { print "$_"; } close(FOOTER);

Replies are listed 'Best First'.
Re: Problems printing to browser
by tachyon (Chancellor) on Nov 29, 2002 at 10:21 UTC

    The problem is probably that your script dies due to the typo here when you call a function on an undefined value

    print my $cgi->escapeHTML( $tags); # should be $query not $cgi print $query->escapeHTML( $tags );

    Your CGI object is called $query not $cgi. Checking the error logs would have revealed this and avoided your frustration.... The script compiles OK because this is a runtime error.

    If this fails then check what the script is actually getting from your form:

    use CGI; my $query = CGI->new; print $query->header; %form = $query->Vars; use Data::Dumper; print $query->escapeHTML(Dumper(\%form));

    If that is not usefull see the CGI Help Guide I wrote.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Ok, you were right, that is what the problem was. I didn't know print $cgi was deaing with print $query, never would have guessed that.

      I have another problem. The script runs fine. In the browser it prints as per you'd expect, but in the email it prints something like:

      <meta name="author" content="Aaron"><br> <meta name="keywords" content="Apples"><br>
      It literally prints <br> in the email. Do you know of a way I can make it print to broswer one tag per line but somehow only print the tags in the email?

      Thanks so much!

      sulfericacid

        Sure just use a s/// regex to remove the <br> tags.

        my $text =<<TEXT; foo<br> bar<br> baz<br> TEXT print $text; # to browser $text =~ s/<br>//g; print $text; # to email

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Problems printing to browser
by frankus (Priest) on Nov 29, 2002 at 11:42 UTC
    Just a point of preference but:
    Try replacing:
    my $tags =''; if ($form{'abstract'} ne "") { $tags .= qq(<meta name="abstract" content="$form{'abstract'}"><br +>\n); } if ($form{'author'} ne "") { $tags .= qq(<meta name="author" content="$form{'author'}"><br>\n) +; } if ($form{'distributor'} ne "") { $tags .= qq(<meta name="distributor" content="$form{'distributor' +}"><br>\n); } if ($form{'copyright'} ne "") { $tags .= qq(<meta name="copyright" content="$form{'copyright'}">< +br>\n); } if ($form{'keywords'} ne "") { $tags .= qq(<meta name="keywords" content="$form{'keywords'}"><br +>\n); } if ($form{'description'} ne "") { $tags .= qq(<meta name="description" content="$form{'description' +}"><br>\n); } $tags .= qq(<meta name="generator" content="SpyderTagV1.0!"><br>\n); if ($form{'robots'} ne "") { $tags .= qq(<meta name="robots" content="$form{'robots'}"><br>\n) +; } if ($form{'language'} ne "") { $tags .= qq(<meta name="language" content="$form{'language'}"><br +>\n); } if ($form{'distribution'} ne "") { $tags .= qq(<meta name="distribution" content="$form{'distributio +n'}"><br>\n); } if ($form{'rating'} ne "") { $tags .= qq(<meta name="rating" content="$form{'rating'}"><br>\n) +; } print my $cgi->escapeHTML( $tags);

    With:
    print my $cgi->escapeHTML( join '', ( map{ defined($form{$_}) ? qq(<meta name="$_" content="$form{$_}"><br> +\n):'' } qw ( abstract author distributor copyright description robots language distribution rating ) ),'<meta name="generator" content="SpyderTagV1.0!"><br>\n' );

    Explanation This uses the functional programming idiom, so start from the inner-most loop and work back.

    • Use the qw function to produce an array of string items for everything in brackets. In this case a list of all the names of the elements from the %form hash
    • Use the map function to process each element of the array produced by 'qw'. Map makes a list of all the results in order.
    • Using a ternary operator to return a value: the string if the form item is defined, no text if not.
    • Join all the array elements from the 'map' function. Also join the string outside the brackets for the map function.
    • Finally print the escapeHTML'd version of the returned string from the join.
    Other "useful" notes:
    • Use single quotes where possible, ie where variables don't need to be interpolated.

    One of the first things I learnt in Perl was to recognise repetitive patterns and then get rid of them.
    I'm lazy.
    A by product is if you want to add more meta fields it's easier now :)

    --

    Brother Frankus.

    ¤

Re: Problems printing to browser
by fruiture (Curate) on Nov 29, 2002 at 10:43 UTC

    In addition to tachyon, who found the problem (++),a note on diagnostics for the future:

    Put 'use strict;' and 'use warnings;' somewhere in the first few lines of your code. Then run your script on the commandline, CGI.pm allows you to specify CGI parameters as commandline options. This way, you're able to use the debugger.

    That method would have alerted you this error. However, if something works on the commandline but not via CGI, see you server's errorlog and consider using CGI::Carp.

    --
    http://fruiture.de
      I am using strict actually, I just didn't post the top part of my script. I am also using -w :) and it still never showed anything, which is really odd.

      Thanks

      sulfericacid

        Then have a look into the error log og your server.

        --
        http://fruiture.de