navwin » Tech Talk » Beyond the Basics » My First CGI Program!!!
Beyond the Basics
Post A Reply Post New Topic My First CGI Program!!! Go to Previous / Newer Topic Back to Topic List Go to Next / Older Topic
Christopher
Moderator
Member Rara Avis
since 1999-08-02
Posts 8296
Purgatorial Incarceration

0 posted 2001-05-19 02:09 AM


I'm so proud of me!    

http://63.144.246.54/test/testform.htm

code for the cgi:

*******************


#!/usr/local/bin/perl

use CGI qw(:standard);

print header();

print "<html>\n<head>\n<title>Form Parameters</title>\n</head>\n";
print "<body>\n";
print "<h1>Form Parameters</h1>\n";

$ReferringURL = $ENV{"HTTP_REFERER"};

if (param()) {
@keys = param();
foreach $key (@keys) {
print "<pre><b><font color='red'>", $key, "</font></b>: ", param($key), "</pre>\n";
}
}
else {
print "<p>No form was Submitted.</p>\n";
}

print "Referring URL = <a href='", $ReferringURL, "'>", $ReferringURL, "</a>";

print "</body></html>\n";

*******************



Worst part was that I couldn't get it working for a while. I have since fixed the problem and determined that semi-colons are officially EVIL!!!

C

[This message has been edited by Christopher (edited 05-19-2001).]

© Copyright 2001 C.G. Ward - All Rights Reserved
Christopher
Moderator
Member Rara Avis
since 1999-08-02
Posts 8296
Purgatorial Incarceration
1 posted 2001-05-19 02:14 AM


Which, actually, now that I'm looking it over, I remember another problem I had - which leads to a question:

The quotation marks in html tags, ie: <a href="http:/url.html">url</a> - are they necessary? I've substituted apostrophe marks to get this to work... will that be a problem with certain browsers? Do you even need to have them at all?

I noticed this problem with javascript as well.

Peace,

C

Poet deVine
Administrator
Member Seraphic
since 1999-05-26
Posts 22612
Hurricane Alley
2 posted 2001-05-19 11:34 AM


I had to test it..you know me...need to find out if it works!!!    
Kit McCallum
Administrator
Member Laureate
since 2000-04-30
Posts 14774
Ontario, Canada
3 posted 2001-05-19 11:43 AM


Me too, looks good Chris!  
Ron
Administrator
Member Rara Avis
since 1999-05-19
Posts 8669
Michigan, US
4 posted 2001-05-19 05:39 PM


You want to be very careful mixing quotation marks and apostrophes in Perl, Chris, because they have slightly different results. The quotation marks tell the interpreter to "expand" this string (including variables or function calls), while the apostrophe tells it to use the string EXACTLY as typed, without any expansion.

Should you try a few tests, Chris, I think you'll discover that quotation marks in HTML are only "necessary" when they enclose multiple words with spaces between each. Every browser I've tried will correctly display color=#ff0000, but will choke over alt=this is a test. Which makes sense, I think. When I'm debugging Perl scripts I often include temporary lines to print a variable's value at various stages, and I rarely bother with quotation marks when doing so.

However, when I'm writing production code, I always include the quotation marks, even though I know current browsers will work without them. The W3C standards call for the quotation marks and the next round of browsers just might enforce that standard. I'd hate to go back and change a ton of code, so feel it's safer to do it right the first time.

Fortunately, with Perl, there's several really easy solutions. You could change the pertinent line above to read:

print "Referring URL=<a href=\"$ReferringURL\">$ReferringURL</a>";

The backslash preceding the internal quotation marks is called escaping them (much as it is and does in JavaScript), and allows them to be embedded within REAL quotation marks. Note that I also didn't use the dot concatenation operator. It's completely unnecessary in this instance, as the interpreter will automatically expand variables when placed within quotation marks.

You could also use a HERE document to print the string:

print <<HTMLCODE;
Referring URL=<a href="$ReferringURL">$ReferringURL</a>
HTMLCODE

The HERE document is particularly good for printing large blocks of material. It essentially says, "start printing here" (the first HTMLCODE reference) and "stop printing here" (the second HTMLCODE reference). You can use any valid Perl name instead of HTMLCODE. By convention, programmers use all caps for HERE document names, but it's not required by Perl. Note that the first reference is terminated with the semicolon, but the second is not. The second reference also MUST start on a new line, in the left-most character position (no indenting allowed).

But, I've saved the best for last:

print qq(Referring URL=<a href="$ReferringURL">$ReferringURL</a> ) ;

The qq function simply puts the statement in quotation mark, but still allows you to use quotation marks within the string. Note, like the other method, this also does the automatic expansion of terms. I prefer this over escaping, simply because I find it MUCH easier to read. Also note that the parens in the qq function can be substituted with just about any character. That's good, because when you're outputting JavaScript code you often need to embed parenthesis characters, too. So the above line could also be written:

print qq~Referring URL=<a href="$ReferringURL">$ReferringURL</a>~;

This use of qq  utilizes the tilde character as its delimiter. I like the tilde because it's one of the few characters that is not an operator in either JavaScript or Perl, and is therefore less likely to be part of the string I'm trying to quote.

Using embedded delimiters is always a challenge. Perl has a reputation as usually providing multiple solutions to just about any problem - and I think this shows the reputation is well deserved.      


p.s. Semi-colons are only EVIL in JavaScript, where they don't do what they are supposed to do - server as an end-of-line terminator. In Perl, the interpreter completely ignores line-breaks and only knows you are finished with a statement when it sees the semi-colon. This allows us to write much more readable code, an important factor when things start getting complicated. While it's hardly necessary with your example, we could also write our qq statement on multiple lines to improve readability. Let's imagine you were printing inside a table for a minute, so you can see what I mean:

print qq(
<tr>
  <td>Referring URL=<a href="$ReferringURL">$ReferringURL</a></td>
</tr>
);

Not only does Perl still understand the qq statement, but it will also output the line-breaks just as you entered them inside the qq delimiters, meaning a "view source" is a lot easier to read, too. This is only possible because Perl knows you ain't done until the semi-colon sings (groan).

Poet deVine
Administrator
Member Seraphic
since 1999-05-26
Posts 22612
Hurricane Alley
5 posted 2001-05-19 08:09 PM


Ron, I have no idea what you just said, but it sounds great!!!  
Christopher
Moderator
Member Rara Avis
since 1999-08-02
Posts 8296
Purgatorial Incarceration
6 posted 2001-05-23 02:56 AM


oooh I like that!!!

hmm... the qq looks like a mighty good addition there, and could definitely be very handy!!!

you should seriously consider writing a book Ron... the first one I have is "Perl for Dummies." ANd while fairly easy to follow, the focus is SO much on loacal apps and so little on web use, that I'm having a hard time combining what it gives with what I want. (though of course the knowledge is handy to have and certainly pertinent to a point) The other book I have is focused on CGI, but seems to skimp mightily on some of the more basic things... which, now that I think about it, will be my next thread...

thank you for the insight!

C

Christopher
Moderator
Member Rara Avis
since 1999-08-02
Posts 8296
Purgatorial Incarceration
7 posted 2002-12-24 07:39 PM


and another thanks... i don't know (now) what I'd do without:

print qq~blah blah <a href="blah.com">blah</a>~;

i loves it!

Post A Reply Post New Topic ⇧ top of page ⇧ Go to Previous / Newer Topic Back to Topic List Go to Next / Older Topic
All times are ET (US). All dates are in Year-Month-Day format.
navwin » Tech Talk » Beyond the Basics » My First CGI Program!!!

Passions in Poetry | pipTalk Home Page | Main Poetry Forums | 100 Best Poems

How to Join | Member's Area / Help | Private Library | Search | Contact Us | Login
Discussion | Tech Talk | Archives | Sanctuary