Getting values from a CGI call

N

Nick Wedd

I am writing a cgi script, and I want to get at the values which were
passed to it as arguments. I finally found a way to do this, at
http://www.perldoc.com/perl5.8.0/lib/CGI.html. You say
use CGI qw/:standard/;
my $edges = param('foo');
and it works.

But before I found this web page, I tried "Learning Perl", which
recommends (page 187)
use CGI qw:)standard);
my $edges = param{"foo"};
This does not work. Instead it sets $edges to "".

Then I tried "Perl in a Nutshell", which recommends (page 330)
use CGI;
my $edges = $query->param("foo");
This is worse. My script stops at the second of the above lines.

Now I can't believe that these books are actually wrong about something
so basic. Yet what they recommend does not work on my system. Can
anyone explain what is going on here?

Nick
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

But before I found this web page, I tried "Learning Perl", which
recommends (page 187)
use CGI qw:)standard);
my $edges = param{"foo"};
This does not work. Instead it sets $edges to "".

I doubt "Learning Perl" recommends using param {...}. Those {}'s should
be ()'s, and if the book has {}'s, then it's a typo.
Then I tried "Perl in a Nutshell", which recommends (page 330)
use CGI;
my $edges = $query->param("foo");
This is worse. My script stops at the second of the above lines.

Because you never created $query as a CGI object.

use CGI;
my $query = CGI->new;
my $edges = $query->param('foo');
 
N

Nick Wedd

In message
Jeff said:
I doubt "Learning Perl" recommends using param {...}. Those {}'s should
be ()'s, and if the book has {}'s, then it's a typo.


Because you never created $query as a CGI object.

use CGI;
my $query = CGI->new;
my $edges = $query->param('foo');

Thank you, thank you. I must learn to read more carefully.

Nick
 
J

Jeff 'japhy' Pinyan

use CGI;
$query = CGI::new();

I would advise against doing that. While the CGI.pm module goes to great
pains to make sure slip-ups like that don't wreck the program, most
object-oriented modules don't make that allowance. The primary difference
between CGI::new() and CGI->new() is that CGI::new() sends NO arguments to
the new() function in the CGI:: namespace, whereas CGI->new() sends the
argument 'CGI' to the method 'new' in CGI's hierarchy (which is CGI::new
in this case).

That is:

package Foo;
sub bar {
my $class = shift;
my @other = @_;
print "Blessing into '$class'\n";
print "Other data: (@other)\n";
bless \@other, $class;
}

If this were to be used thus:

my $obj = Foo->bar(1, 4, 9);

we'd get

Blessing into 'Foo'
Other data: (1 4 9)

However, if we made the mistake of using Foo::bar(1, 4, 9), we'd get

Blessing into '1'
Other data: (4 9)

So what it comes down to is this: it's important not to mistake a class
method call (Foo->bar) with a package function call (Foo::bar).
 

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

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top