How to Join Member's Area Private Library Search Today's Topics p Login
Main Forums Discussion Tech Talk Mature Content Archives
   Nav Win
 Tech Talk
 Beyond the Basics
 My First CGI Program!!!
 1 2 3 4 5 6 7
Follow us on Facebook

 Moderated by: Ron   (Admins )

 
User Options
Format for Better Printing EMail to a Friend Not Available
Admin Print Send ECard
Passions in Poetry

My First CGI Program!!!

 Post A Reply Post New Topic   Go to the Next Oldest/Previous Topic Return to Topic Page Go to the Next Newest Topic 
Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


0 posted 05-19-2001 02:09 AM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

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).]

Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


1 posted 05-19-2001 02:14 AM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

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 Empyrean
since 05-26-99
Posts 25869
Hurricane Alley


2 posted 05-19-2001 11:34 AM       View Profile for Poet deVine   Email Poet deVine   Edit/Delete Message      Find Poems  View IP for Poet deVine

I had to test it..you know me...need to find out if it works!!!    
Kit McCallum
Administrator
Member Laureate
since 04-30-2000
Posts 16920
Ontario, Canada


3 posted 05-19-2001 11:43 AM       View Profile for Kit McCallum   Email Kit McCallum   Edit/Delete Message      Find Poems  View IP for Kit McCallum

Me too, looks good Chris!  
Ron
Administrator
Member Rara Avis
since 05-19-99
Posts 9708
Michigan, US


4 posted 05-19-2001 05:39 PM       View Profile for Ron   Email Ron   Edit/Delete Message      Find Poems   Click to visit Ron's Home Page   View IP for Ron

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 Empyrean
since 05-26-99
Posts 25869
Hurricane Alley


5 posted 05-19-2001 08:09 PM       View Profile for Poet deVine   Email Poet deVine   Edit/Delete Message      Find Poems  View IP for Poet deVine

Ron, I have no idea what you just said, but it sounds great!!!  
Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


6 posted 05-23-2001 02:56 AM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

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 08-02-99
Posts 9130
Purgatorial Incarceration


7 posted 12-24-2002 07:39 PM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

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   Go to the Next Oldest/Previous Topic Return to Topic Page Go to the Next Newest Topic 
All times are ET (US) Top
  User Options
>> Tech Talk >> Beyond the Basics >> My First CGI Program!!! Format for Better Printing EMail to a Friend Not Available
Print Send ECard

 

pipTalk Home Page | Main Poetry Forums

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



© Passions in Poetry and netpoets.com 1998-2013
All Poetry and Prose is copyrighted by the individual authors