Basic CGI.pm question

R

Robert Sedlacek

Will said:
Can someone perhaps nudge me in the right direction on what I'm doing
wrong?

Sorry, no 100 percent sure answer here, but have you tried the -debug
pragma, as mentioned in perldoc CGI?

g,
Robert
 
W

Will Hartung

Hi all,

I was trying to write a simple CGI script last night and ran in to some
issues.

use CGI;

my $query = new CGI;
my $vars = $query->Vars;
my $elements;
my $combined;

$elements{abc} = "123";
$elements{xyz} = "qed";

%combined = (%elements, %vars);

print %combined;

When I was first playing with this, I was using Perl 5.005 that was stock
installed in /usr/bin/perl on my Solaris 8 machine. When it started, it
would tell that I was in "standalone mode" or somesuch and to enter in
"name=value" pairs. Later, I discovered that it the CGI.pm version it had
installed didn't support the $query->Vars routine.

So, I upgraded to 5.8 using www.blastwave.com. At first, it didn't work but
then I moved /opt/csw/bin on to the from of my PATH and then it found the
new perl.

Now, CGI no longer prompts me for the name=value pairs. I read some
documentation that said that it should, or that I could add pairs by doing
"x.pl abc=123" on the command line. But that isn't working either.

Now, this quick and dirty script has collapsed into mad, time wasting
struggle. As may be apparent, I'm not really up to speed on Perl (having
last dabbled with Perl 4), but "how hard could this be".

My hope was to basiclly develop, debug, and test the script as much as I
could from the command line before I dumped it onto the web server, but that
seems to be a hope that has been frustrated.

I was thinking this was a installation problem with my machine, but when I
tried in on Cygwin (which is running 5.8.0), I got similar behaviors.

Can someone perhaps nudge me in the right direction on what I'm doing wrong?
Apparently the documenation I've been using is wrong or out of date.

Thanx!

Regards,

Will Hartung
([email protected])
 
A

Alan J. Flavell

Now, CGI no longer prompts me for the name=value pairs.

The default behaviour changed, relative to early versions of the
module. Now, if you want this behaviour, you need to ask for it.

Here's the author's own documentation (for the latest version):

http://stein.cshl.org/WWW/software/CGI/#debugging

You should find esentially the same information, albeit in a different
format, via "perldoc CGI.pm" on your system, and this will be for the
version that you actually installed, where there are any differences.
Now, this quick and dirty script has collapsed into mad, time
wasting struggle. As may be apparent, I'm not really up to speed on
Perl (having last dabbled with Perl 4), but "how hard could this
be".

Well, finding and using the documentation is the key to this, IMHO.

When you're doing CGI, I strongly recommend not only the precautions
mentioned in this group's posting guidelines (i.e use strict and use
warnings) to get all the help that Perl can offer, but also enabling
taint checking (-T) so that there's at least some chance of getting
notified if you dig any large security holes.

It may be unfortunate that the CGI.pm author doesn't tend to include
these in his worked examples.

Google also suggests
http://perlmonks.thepen.com/use strict warnings and diagnostics or die.html
for further consideration.
 
W

Will Hartung

Robert Sedlacek said:
Sorry, no 100 percent sure answer here, but have you tried the -debug
pragma, as mentioned in perldoc CGI?

Yea, I saw that reference. I just didn't know what that meant. Well, I
should say I DO know what it means, I just don't know how to use it. The
"-debug pragma" is obivously some kind of switch that's thrown someplace and
associated with CGI, but the detail on how to do that is missing (I should
say missing in me). It's one of those assumed undercurrents thats rocketing
me deeper into the depths of Perl than where I am, but it's still dark and
my flashlight is on the blink.

But thanks for the suggestion! I appreciate it!

Regards,

Will Hartung
([email protected])
 
W

Will Hartung

The default behaviour changed, relative to early versions of the
module. Now, if you want this behaviour, you need to ask for it.

Here's the author's own documentation (for the latest version):

http://stein.cshl.org/WWW/software/CGI/#debugging

You should find esentially the same information, albeit in a different
format, via "perldoc CGI.pm" on your system, and this will be for the
version that you actually installed, where there are any differences.

Yeah, I read that. That's why I was trying the "x.pl abc=123", but "nothing
was happening". Specifically:
my $vars = $query->Vars;
seemed to net me an empty hash, and it was my understanding that this would
get me all of the parameters (notably abc = 123).

Specifically from the document you linked to (which I assume is the same one
that I had read, it was certainly close):
<quote>
Fetching the Parameter List as a Hash

$params = $q->Vars;
print $params->{'address'};
@foo = split("\0",$params->{'foo'});
%params = $q->Vars;

use CGI ':cgi-lib';
$params = Vars;

Many people want to fetch the entire parameter list as a hash in which the
keys are the names of the CGI parameters, and the values are the parameters'
values. The Vars() method does this. Called in a scalar context, it returns
the parameter list as a tied hash reference. Changing a key changes the
value of the parameter in the underlying CGI parameter list. Called in an
list context, it returns the parameter list as an ordinary hash. This allows
you to read the contents of the parameter list, but not to change it.
</quote>

Since I wasn't planning to change the parameter list, I really didn't care
which version I used, so I used $vars = $query->Vars;

Well, finding and using the documentation is the key to this, IMHO.

Yup! I'm with you there!
When you're doing CGI, I strongly recommend not only the precautions
mentioned in this group's posting guidelines (i.e use strict and use
warnings) to get all the help that Perl can offer, but also enabling
taint checking (-T) so that there's at least some chance of getting
notified if you dig any large security holes.

It may be unfortunate that the CGI.pm author doesn't tend to include
these in his worked examples.

Google also suggests
http://perlmonks.thepen.com/use strict warnings and diagnostics or
%20die.html
for further consideration.

Ah! This is very helpful, I'll toss that in as well. I assume the -T goes on
the command line or the first line of the script?

#!/usr/bin/perl -T ?

I admit that "taint" is something I never dealt with at all 10 years ago
when I last worked with Perl.

Thanx for your help.

Regards,

Will Hartung
([email protected])
 
W

Will Hartung

Jim Gibson said:
What means "isn't working"?

Meaning I was unable to populate the parameters for the CGI request either
from the command line or from stdin, as it mentioned in the debug section.
Have you looked at the documentation that comes with your perl
installation? Executing 'perldoc CGI' will get you a lengthy document
describing CGI. Search for the "DEBUGGING" section.

I wasn't aware of "perldoc CGI", I'll try that as well.
I test simple CGI scripts by putting, for example,

if( $test ) {
$ENV{QUERY_STRING} = 'abc=123' unless defined $ENV{QUERY_STRING};
$ENV{REQUEST_METHOD] = 'GET' unless defined $ENV{REQUEST_METHOD};
$ENV{CONTENT_LENGTH} = 0 unless defined $ENV{CONTENT_LENGTH};
}

at the begining of my program, before I create a CGI object instance.

It was my understanding that ideally I shouldn't have to do that, but I can
try that next.
Make sure you are executing the proper version of perl. Use 'perl -v'
and 'which perl' to see which version of perl and

perl -MCGI -e 'print "$CGI::VERSION\n"'

to see which version of CGI you are using.

I'll do that. Thanx!

Regards,

Will Hartung
([email protected])
 
E

Eric Schwartz

Will Hartung said:
Yea, I saw that reference. I just didn't know what that meant. Well, I
should say I DO know what it means, I just don't know how to use it. The
"-debug pragma" is obivously some kind of switch that's thrown someplace and
associated with CGI, but the detail on how to do that is missing (I should
say missing in me).

The first hit for '-debug' in 'perldoc CGI' is:

For example, the following use statement imports the standard set
of functions and enables debugging mode (pragma -debug):

use CGI qw/:standard -debug/;

You'll pardon us, I hope, for not being terribly sympathetic to your
inability to find it.
It's one of those assumed undercurrents thats rocketing me deeper
into the depths of Perl than where I am,

It's not only not assumed, it's stated blatantly. Just *read the
documentation*. For any module you use, if you don't know exactly
what it does and why, *read the documentation*. The greatest strength
of Perl, IMO, is not its DWIM, its built-in regexes, or even
necessarily CPAN. It's the fact that every single module you install
will have documentation, accessible via perldoc.

If you don't use it, you are doing yourself (and, unfortunately, the
rest of clpm) a great disservice.
but it's still dark and my flashlight is on the blink.

You're on the top of the mountain, and it's high noon, nary a cloud in
the sky, but you refuse to take off the blindfold you put on yourself.

-=Eric
 
J

Juha Laiho

Will Hartung said:
I was trying to write a simple CGI script last night and ran in to some
issues. ....
use CGI;

my $query = new CGI;
my $vars = $query->Vars; ....
Later, I discovered that it the CGI.pm version it had installed didn't
support the $query->Vars routine.

So, I upgraded to 5.8 using www.blastwave.com. ....
Now, CGI no longer prompts me for the name=value pairs. I read some
documentation that said that it should, or that I could add pairs by doing
"x.pl abc=123" on the command line. But that isn't working either. ....
Can someone perhaps nudge me in the right direction on what I'm doing wrong?
Apparently the documenation I've been using is wrong or out of date.

As for documentation, see the documentation that comes with your
installation. THat way you'll get documentation that corresponds
with your installed versions.

Then, is this doing what you're looking for:

=====
#! /usr/bin/perl -T
use strict;
use warnings;

use CGI qw/-debug/;

my $q=new CGI();

my $varref=$q->Vars();

foreach (keys %{$varref}) {
print join('=',$_,$varref->{$_}),"\n";
}
=====

So, the qw/-debug/ will give you back the prompt to enter name/value
pairs on stdin. You can also dige name/value pairs on the command
line. Also, the use of Vars seems to work for me (perl 5.8.0, CGI.pm
2.89).
 
A

A. Sinan Unur

I was trying to write a simple CGI script last night and ran in to
some issues.

You know your script has issues, right?
use CGI;

my $query = new CGI;
my $vars = $query->Vars;
my $elements;

Here you declare $elements as a scalar.
my $combined;

$elements{abc} = "123";
$elements{xyz} = "qed";

Here you are accessing the elements of an undeclared hash.
%combined = (%elements, %vars);

print %combined;

C:\Home> perl -c t4.pl
Global symbol "%elements" requires explicit package name at t4.pl line
11.
Global symbol "%elements" requires explicit package name at t4.pl line
12.
Global symbol "%combined" requires explicit package name at t4.pl line
14.
Global symbol "%elements" requires explicit package name at t4.pl line
14.
Global symbol "%vars" requires explicit package name at t4.pl line 14.
Global symbol "%combined" requires explicit package name at t4.pl line
16.
t4.pl had compilation errors.

use warnings;
use strict;

use CGI(-debug);

my $cgi = CGI->new;

use Data::Dumper;
print Dumper($cgi->Vars);

C:\Home> perl t4.pl
(offline mode: enter name=value pairs on standard input; press ^D or ^Z
when done)
5
^Z
keywords
$VAR2 = 5
 
M

Matt Garrish

Will Hartung said:
Hi all,

I was trying to write a simple CGI script last night and ran in to some
issues.

use CGI;

my $query = new CGI;
my $vars = $query->Vars;
my $elements;
my $combined;

$elements{abc} = "123";
$elements{xyz} = "qed";

%combined = (%elements, %vars);

print %combined;

Your issue doesn't appear to be with CGI.pm, but with a lack of
understanding of Perl data structures. It's not surprising that you're
getting nothing from the command line, since you store the reference to
those variables in $vars, but then try and access %vars.

This is exactly the reason why you should start all scripts with:

use strict;
use warnings;

Matt
 
E

Eric Bohlman

I test simple CGI scripts by putting, for example,

if( $test ) {
$ENV{QUERY_STRING} = 'abc=123' unless defined $ENV{QUERY_STRING};
$ENV{REQUEST_METHOD] = 'GET' unless defined $ENV{REQUEST_METHOD};
$ENV{CONTENT_LENGTH} = 0 unless defined $ENV{CONTENT_LENGTH};
}

at the begining of my program, before I create a CGI object instance.

Since the case where those variables are defined but empty is
uninteresting, the neuromuscular structures in your wrists would probably
appreciate you writing

$ENV{QUERY_STRING} ||= 'abc=123';

,etc. instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top