CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need some help on a script, please help

 



supaafly
stranger

Jul 16, 2001, 12:16 AM

Post #1 of 6 (1061 views)
Need some help on a script, please help Can't Post

Hi, im kind of new at this and i was just testing this script query string article, and i tried making a little script out of it but for some reason its not working. Here is the script, its not too big, but it will probably get bigger once i find out about it:

#!/usr/bin/perl

print "Content-type: text/html\n";
# This checks what the query string is.

if (length ($ENV{'QUERY_STRING'}) > 0){
$buffer = $ENV{'QUERY_STRING'};
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$in{$name} = $value;
}
} else {
print "What?\n";
print "No query string?\n";
print "Do you expect me to read your mind or something?\n";
}

# This tells the script what the variables are.

$text1 = $in{'text1'};
$text2 = $in{'text2'};
$text3 = $in{'text3'};

# This prints the info inserted into a temporary document.

if ($text1 > 0){
print " $text1 \n";
print " $text3 \n";
print " $text2 \n";
}

i have that in a file called text.cgi and i chmod it 777. I dont know why but i keep getting an error 500, but it doesnt say what is wrong. Please help me if you can. Thanks.





abstracts
Novice

Jul 16, 2001, 1:06 AM

Post #2 of 6 (1058 views)
Re: Need some help on a script, please help [In reply to] Can't Post

Hello,
A few problems in your program:
1. First 2 lines should read

Code
#!/usr/bin/perl -w 
use strict;

2. The proper header should have 2 "\n"s.

Code
print "Content-type: text/html\n\n";

3. If $text1 <= 0, nothing will be printed.

4. Why are you parsing the query string and not using CGI.pm? Look for CGI.pm tutorials elsewhere, perldoc CGI, then ask if you get stuck.

Hope this helps,,,

Aziz,,,



abstracts
Novice

Jul 16, 2001, 1:15 AM

Post #3 of 6 (1057 views)
Re: Need some help on a script, please help [In reply to] Can't Post

Oh yeah, I forgot to mention 2 things:

5. You should never make a script mode 777. Change that to 755. There are no excuses.

6. Look for the server log for possible errors. Most probably the error you had was something like "Incomplete headers" or "Malformed headers". You could've also gotten a security warning about the file permissions.

Hope this helps,,,

Aziz,,,,



mhx
Enthusiast / Moderator

Jul 16, 2001, 1:56 AM

Post #4 of 6 (1056 views)
Re: Need some help on a script, please help [In reply to] Can't Post

Hi!

Three rules to the beginner:

1. Use -wT
2. Use strict
3. Use CGI.pm

A good starting point could be

Code
#!/usr/bin/perl -wT 
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use strict;

print header, start_html('Page Title');

if( param() ) {
print p("You gave me this input:"),
map p("$_ => ", join ', ', param($_)), param();
}
else {
print start_form(),
checkbox( {name => 'box', label => 'Check me!', value => 'checked'} ),
textfield( {name => 'text'} ),
submit('GO!'),
end_form();
}

print end_html;

This will create an HTML-Form with a checkbox, a text field and a submit button, and will print the form parameters after you hit the submit button.
There's a lot of documentation for CGI.pm available.

And you should always set your permissions to 755. A 500 Server Error has mostly to do with either permissions or the first line in the Perl script that tells the webserver where to find perl.
Mostly all other errors and warnings will be displayed in your browser if you use CGI::Carp as in the code above.
Hope this will help.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



supaafly
stranger

Jul 16, 2001, 12:53 PM

Post #5 of 6 (1047 views)
Re: Need some help on a script, please help [In reply to] Can't Post

Hi, thanks for all the help, but when i run the script i get a compilation error, and here is what it says:

[Mon Jul 16 13:08:37 2001] text.cgi: Global symbol "$buffer" requires explicit package name at /home/cgi-bin/text.cgi line 10.
[Mon Jul 16 13:08:37 2001] text.cgi: Global symbol "@pairs" requires explicit package name at /home/cgi-bin/text.cgi line 11.
[Mon Jul 16 13:08:37 2001] text.cgi: Global symbol "$pair" requires explicit package name at /home/cgi-bin/text.cgi line 12.
[Mon Jul 16 13:08:37 2001] text.cgi: Global symbol "$name" requires explicit package name at /home/cgi-bin/text.cgi line 13.
[Mon Jul 16 13:08:37 2001] text.cgi: Global symbol "$value" requires explicit package name at /home/cgi-bin/text.cgi line 13.
[Mon Jul 16 13:08:37 2001] text.cgi: Global symbol "%in" requires explicit package name at /home/cgi-bin/text.cgi line 15.
[Mon Jul 16 13:08:37 2001] text.cgi: Global symbol "$text1" requires explicit package name at /home/cgi-bin/text.cgi line 25.
[Mon Jul 16 13:08:37 2001] text.cgi: Global symbol "$text2" requires explicit package name at /home/cgi-bin/text.cgi line 26.
[Mon Jul 16 13:08:37 2001] text.cgi: Global symbol "$text3" requires explicit package name at /home/cgi-bin/text.cgi line 27.
[Mon Jul 16 13:08:37 2001] text.cgi: Execution of /home/cgi-bin/text.cgi aborted due to compilation errors.

I have changed the script but its still not working, here is what i have now:

#!/usr/local/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use strict;

print "Content-type: text/html\n\n";
# This checks what the query string is.

if (length ($ENV{'QUERY_STRING'}) > 0){
$buffer = $ENV{'QUERY_STRING'};
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$in{$name} = $value;
}
} else {
print "What?\n";
print "No query string?\n";
print "Do you expect me to read your mind or something?\n";
}

# This tells the script what the variables are.

$text1 = $in{'text1'};
$text2 = $in{'text2'};
$text3 = $in{'text3'};

# This prints the info inserted into a temporary document.

if ($text1 > 0){
print " $text1 \n";
print " $text3 \n";
print " $text2 \n";
}

Please if anybody can help, please do! Thanks.





mhx
Enthusiast / Moderator

Jul 18, 2001, 1:46 AM

Post #6 of 6 (1011 views)
Re: Need some help on a script, please help [In reply to] Can't Post

Your script doesn't work for various reasons. The main problem is that you try to parse the query string, which you don't need, since the CGI module already did it for you. So throw out that query string parsing code! CGI.pm is better, easier to use, safer, very well documented. You just have to type perldoc CGI, and you can read several dozens of manual pages that describe in every detail how you can use CGI.pm. And it's really worth reading it, since CGI.pm makes your life so much easier.
Next thing the CGI module does for you: It writes correct headers (Content-type, etc), so you don't have to do this either. You can write complete HTML documents with the built-in functions of the CGI module.
All those compilation erros you get are due to the use strict; pragma at the top of the script. It forces you to declare all your variables with my. Since you just pasted your parsing code in, and it's actually so bad that it isn't use-strict-safe, your program fails. If you need to know more about strict, type perldoc strict.
I'd say, try to read the documentation for CGI.pm. If you don't understand something in the docs, ask here. Then try to understand the piece of code I posted above. If you should still have problems understanding that code, ask here. But please don't paste code you don't understand completely into other code you don't understand completely, expect this code to work, and ask why it fails.
If you think that lack of Perl knowledge is your problem, try one of the many Perl tutorials there are on the web. One good Perl tutorial is Rob's Perl tutorial. It's always better to have some basic knowledge before starting with special applications of a language (i.e. CGI programming). That often means fighting two battles at the same time.
I hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives